JavaDOM4J解析器-创建XML文档

翻译 ww9992019com 发表于 5 天前

www.lyjust.net开户请拨18288069766泰国坠崖孕妇已清醒杭州上城区2iTamW<div style="width:100%;float:left;" class="article-content">

                <input type="hidden" name="article_id" id="article_id" value="6196">
                <input type="hidden" name="catid" id="catid" value="0">

                <h2>

演示示例</h2>

<p>

这是我们需要创建的XML文档:</p>

<pre class="prettyprint prettyprinted" courier new monospace border-top-left-radius: border-top-right-radius: border-bottom-right-radius: border-bottom-left-radius: width: line-height: font-size: overflow: auto color: rgb>

<span class="pun">&lt;?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">"1.0"</span><span class="pln"> encoding</span><span class="pun">=</span><span class="str">"UTF-8"</span><span class="pun">?&gt;</span><span class="pln">

</span><span class="tag">&lt;cars&gt;</span><span class="pln">

</span><span class="tag">&lt;supercars</span><span class="pln"> </span><span class="atn">company</span><span class="pun">=</span><span class="atv">"Ferrari"</span><span class="tag">&gt;</span><span class="pln">

</span><span class="tag">&lt;carname</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"formula one"</span><span class="tag">&gt;</span><span class="pln">Ferrari 101</span><span class="tag">&lt;/carname&gt;</span><span class="pln">

</span><span class="tag">&lt;carname</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"sports"</span><span class="tag">&gt;</span><span class="pln">Ferrari 202</span><span class="tag">&lt;/carname&gt;</span><span class="pln">

</span><span class="tag">&lt;/supercars&gt;</span><span class="pln">

</span><span class="tag">&lt;/cars&gt;</span></pre>

<p>

 </p>

<p>

<strong>演示示例:</strong></p>

<p>

<i>CreateXmlFileDemo.java</i></p>

<pre class="prettyprint tryit prettyprinted" courier new monospace border-top-left-radius: border-top-right-radius: border-bottom-right-radius: border-bottom-left-radius: width: line-height: font-size: overflow: auto color: rgb cursor: default background-image: url background-attachment: initial background-size: background-origin: background-clip: background-position: background-repeat: no-repeat>

<span class="kwd">package</span><span class="pln"> com</span><span class="pun">.</span><span class="pln">yiibai</span><span class="pun">.</span><span class="pln">xml</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">io</span><span class="pun">.</span><span class="typ">IOException</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">io</span><span class="pun">.</span><span class="typ">UnsupportedEncodingException</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">dom4j</span><span class="pun">.</span><span class="typ">Document</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">dom4j</span><span class="pun">.</span><span class="typ">DocumentHelper</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">dom4j</span><span class="pun">.</span><span class="typ">Element</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">dom4j</span><span class="pun">.</span><span class="pln">io</span><span class="pun">.</span><span class="typ">OutputFormat</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">dom4j</span><span class="pun">.</span><span class="pln">io</span><span class="pun">.</span><span class="typ">XMLWriter</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> DOM4JCreateXMLDemo </span><span class="pun">{</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> main</span><span class="pun">(</span><span class="typ">String</span><span class="pun">[]</span><span class="pln"> args</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

  </span><span class="kwd">try</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

     </span><span class="typ">Document</span><span class="pln"> document </span><span class="pun">=</span><span class="pln"> </span><span class="typ">DocumentHelper</span><span class="pun">.</span><span class="pln">createDocument</span><span class="pun">();</span><span class="pln">

     </span><span class="typ">Element</span><span class="pln"> root </span><span class="pun">=</span><span class="pln"> document</span><span class="pun">.</span><span class="pln">addElement</span><span class="pun">(</span><span class="pln"> </span><span class="str">"cars"</span><span class="pln"> </span><span class="pun">);</span><span class="pln">

     </span><span class="typ">Element</span><span class="pln"> supercarElement</span><span class="pun">=</span><span class="pln"> root</span><span class="pun">.</span><span class="pln">addElement</span><span class="pun">(</span><span class="str">"supercars"</span><span class="pun">)</span><span class="pln">

        </span><span class="pun">.</span><span class="pln">addAttribute</span><span class="pun">(</span><span class="str">"company"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"Ferrai"</span><span class="pun">);</span><span class="pln">



     supercarElement</span><span class="pun">.</span><span class="pln">addElement</span><span class="pun">(</span><span class="str">"carname"</span><span class="pun">)</span><span class="pln">

        </span><span class="pun">.</span><span class="pln">addAttribute</span><span class="pun">(</span><span class="str">"type"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"Ferrari 101"</span><span class="pun">)</span><span class="pln">

        </span><span class="pun">.</span><span class="pln">addText</span><span class="pun">(</span><span class="str">"Ferrari 101"</span><span class="pun">);</span><span class="pln">



     supercarElement</span><span class="pun">.</span><span class="pln">addElement</span><span class="pun">(</span><span class="str">"carname"</span><span class="pun">)</span><span class="pln">

        </span><span class="pun">.</span><span class="pln">addAttribute</span><span class="pun">(</span><span class="str">"type"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"sports"</span><span class="pun">)</span><span class="pln">

        </span><span class="pun">.</span><span class="pln">addText</span><span class="pun">(</span><span class="str">"Ferrari 202"</span><span class="pun">);</span><span class="pln">



     </span><span class="com">// Pretty print the document to System.out</span><span class="pln">

     </span><span class="typ">OutputFormat</span><span class="pln"> format </span><span class="pun">=</span><span class="pln"> </span><span class="typ">OutputFormat</span><span class="pun">.</span><span class="pln">createPrettyPrint</span><span class="pun">();</span><span class="pln">

     </span><span class="typ">XMLWriter</span><span class="pln"> writer</span><span class="pun">;</span><span class="pln">

     writer </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">XMLWriter</span><span class="pun">(</span><span class="pln"> </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">,</span><span class="pln"> format </span><span class="pun">);</span><span class="pln">

     writer</span><span class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span class="pln"> document </span><span class="pun">);</span><span class="pln">

  </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">catch</span><span class="pln"> </span><span class="pun">(</span><span class="typ">UnsupportedEncodingException</span><span class="pln"> e</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

     e</span><span class="pun">.</span><span class="pln">printStackTrace</span><span class="pun">();</span><span class="pln">

  </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">catch</span><span class="pln"> </span><span class="pun">(</span><span class="typ">IOException</span><span class="pln"> e</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

     e</span><span class="pun">.</span><span class="pln">printStackTrace</span><span class="pun">();</span><span class="pln">

  </span><span class="pun">}</span><span class="pln">

</span><span class="pun">}</span><span class="pln">

</span><span class="pun">}</span></pre>

<p>

这将产生以下结果:</p>

<pre class="result" courier new monospace margin-bottom: border-top-left-radius: border-top-right-radius: border-bottom-right-radius: border-bottom-left-radius: width: font-size: overflow: auto color: rgb line-height: background-color:>

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;cars&gt;

&lt;supercars company="Ferrari"&gt;

&lt;carname type="formula one"&gt;Ferrari 101&lt;/carname&gt;

&lt;carname type="sports"&gt;Ferrari 202&lt;/carname&gt;

&lt;/supercars&gt;

&lt;/cars&gt;</pre>

                <br>
                <p style="text-align:center;float:left;width:100%;margin-top:24px;margin-bottom:18px;">
                        <button class="layui-btn layui-btn-danger" id="btn-reward"> ¥ 我要打赏 </button>  
                        <button class="layui-btn" id="article-perfect">
                            <i class="layui-icon layui-icon-edit"></i>教程纠错
                        </button>
                        <button class="layui-btn layui-btn-normal" id="action-collection">
                            <i class="layui-icon layui-icon-rate"></i>收藏
                        </button>
                </p>



                <div style="width:100%;margin-bottom: 16px; height: 32px;margin-top:18px;float:left;">

                    <span style="float:left;">上一篇:<a href="http://f2e.im/u/ww9992019com" title="Java DOM4J解析器 - 查询XML文档"><i class="layui-icon"></i>
                            Java DOM4J解析器 - 查询XML文档</a></span>
                                            <span style="float:right;">下一篇:<a href="http://f2e.im/u/ww9992019com" style="float:right;" title="下一篇:Java DOM4J解析器 - 修改XML文档"><i class="layui-icon"></i>
                            Java DOM4J解析器 - 修改XML文档</a></span>
                                            <div id="googlead" style="float:left;margin-top:8px;">
                        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                        <!-- yiibai下方 -->
                        <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-1090193214637198" data-ad-slot="6494738921"></ins>
                        <script>
                            (adsbygoogle = window.adsbygoogle || []).push({});
                        </script>
                    </div>
                </div>

                <div style="width:100%;margin-top:18px;float:left;">


            </div>
暂无回复,说出你的观点吧
登录后即可参与回复