myeclipse中如何解析xml文件

2025-04-30 06:13:59
推荐回答(1个)
回答1:

package com.nikee.dom4j;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import java.io.*;
public class XmlDom4J{
public void generateDocument(){
//使用 DocumentHelper 类创建一个文档实例。 DocumentHelper 是生成 XML 文档节点的 dom4j API 工厂类
Document document = DocumentHelper.createDocument();
//使用 addElement()方法创建根元素catalog , addElement()用于向 XML 文档中增加元素
Element catalogElement = document.addElement("catalog");
//在 catalog 元素中使用 addComment() 方法添加注释"An XML catalog" catalogElement.addComment("An XML Catalog");
//在 catalog 元素中使用 addProcessingInstruction() 方法增加一个处理指令
catalogElement.addProcessingInstruction("target","text");
//在 catalog 元素中使用 addElement() 方法增加 journal 元素
Element journalElement = catalogElement.addElement("journal");
//使用 addAttribute() 方法向 journal 元素添加 title 和 publisher 属性
journalElement.addAttribute("title", "XML Zone"); journalElement.addAttribute("publisher", "IBM developerWorks"); Element articleElement=journalElement.addElement("article"); articleElement.addAttribute("level", "Intermediate"); articleElement.addAttribute("date", "December-2001");
Element titleElement=articleElement.addElement("title"); titleElement.setText("Java configuration with XML Schema"); Element authorElement=articleElement.addElement("author");
Element firstNameElement=authorElement.addElement("firstname"); firstNameElement.setText("Marcello");
Element lastNameElement=authorElement.addElement("lastname"); lastNameElement.setText("Vitaletti");
//可以使用 addDocType() 方法添加文档类型说明
//这样就向 XML 文档中增加文档类型说明:
document.addDocType("catalog","nikee","file://c:/Dtds/catalog.dtd");
try{
//XMLWriter output = new XMLWriter(new FileWriter( new File("D:/eclipse3.2/workspace/WebServices/src/com/nikee/dom4j/catalog.xml")));
FileOutputStream fos=new FileOutputStream("D:/eclipse3.2/workspace/WebServices/src/com/nikee/dom4j/catalog.xml");
OutputFormat of=new OutputFormat(" ", true);
XMLWriter xw=new XMLWriter(fos, of);
xw.write( document );
xw.close();
}
catch(IOException e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] argv){
XmlDom4J dom4j=new XmlDom4J();
dom4j.generateDocument();
} }

xml 代码
xml version="1.0" encoding="UTF-8"?>
>
target text?>

Java configuration with XML Schematitle> <br><author> <br><firstname>Marcellofirstname> <lastname>Vitalettilastname> <br><author> <article> <journal> <catalog> <br><br>这是另外一种:<br>package org.lovo.xml;<br><br>import java.io.File;<br>import java.io.IOException;<br>import java.util.List;<br><br>import org.jdom.Document;<br>import org.jdom.Element;<br>import org.jdom.JDOMException;<br>import org.jdom.input.SAXBuilder;<br><br>public class TestXml{<br> <br> public String gjd;<br> public String zjd;<br> public String jdname;<br> public String content;<br> <br> public TestXml(){<br> <br> File file = new File("Test.xml");<br> SAXBuilder builder = new SAXBuilder();<br> <br> try {<br> Document doc = builder.build(file);<br> Element root = doc.getRootElement();<br> gjd = root.getName();<br> <br> Element stu = root.getChild("student");<br> zjd = stu.getName();<br> <br> Element name = stu.getChild("name");<br> jdname = name.toString();<br> content = name.getName();<br> List<Element> list = root.getChildren();<br> <br> for(int i=0;i<list.size();i++){<br> Element el = list.get(i);<br> System.out.println(el.getValue());<br> }<br> } catch (JDOMException e) {<br> // TODO Auto-generated catch block<br> e.printStackTrace();<br> } catch (IOException e) {<br> // TODO Auto-generated catch block<br> e.printStackTrace();<br> }<br> }<br> <br> public static void main(String[] args){<br> new TestXml();<br> }<br>}</p> </div> </div> <div class="clear"></div> </div> </div> </div> <div class="wendaright"> <div class="wdluluerwema"> <div class="wdxgwttop">相关问答</div> <div class="wdxgwtnr"> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://13l.net/index.php/l/522243836776618285.html" title="myeclipse怎么创建xml文件">myeclipse怎么创建xml文件</a></div> <div class="wdxgfbxinxi pcnone "></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://13l.net/index.php/l/198869450129209565.html" title="Myeclipse 中 如何编辑xml以及运行xml">Myeclipse 中 如何编辑xml以及运行xml</a></div> <div class="wdxgfbxinxi pcnone "></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://13l.net/index.php/l/208860383.html" title="在myeclipse中 如何用dom4j解析xml文件,流程...">在myeclipse中 如何用dom4j解析xml文件,流程...</a></div> <div class="wdxgfbxinxi pcnone "></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://13l.net/index.php/l/485158789.html" title="怎样使用myeclipse编写xml?">怎样使用myeclipse编写xml?</a></div> <div class="wdxgfbxinxi pcnone "></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://13l.net/index.php/l/130742281.html" title="如何更改MyEclipse中XML文件的字体?">如何更改MyEclipse中XML文件的字体?</a></div> <div class="wdxgfbxinxi pcnone "></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://13l.net/index.php/l/983358686032845019.html" title="在myeclipse中,对于某个xml文件,怎么查找哪些程序...">在myeclipse中,对于某个xml文件,怎么查找哪些程序...</a></div> <div class="wdxgfbxinxi pcnone "></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://13l.net/index.php/l/241424732.html" title="打开myeclipse 的xml文件,应该怎么做?">打开myeclipse 的xml文件,应该怎么做?</a></div> <div class="wdxgfbxinxi pcnone "></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://13l.net/index.php/l/110686111.html" title="myeclipse中xml解析怎么做,就是开始-新建……那些...">myeclipse中xml解析怎么做,就是开始-新建……那些...</a></div> <div class="wdxgfbxinxi pcnone "></div> </div> </div> <div class="clear"></div> </div> <!-- 其他随机问答['id'=>alphaID($like['zid'])] --> <div class="wdluluerwema"> <div class="wdxgwttop">最新问答</div> <div class="wdxgwtnr"> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://13l.net/index.php/l/1668272175745978227.html">仓鼠一天吃多少,喂几顿啊拜托各位了 3Q</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://13l.net/index.php/l/490660783.html">辉渠小米是哪里产的,怎么样?是有机小米吗?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://13l.net/index.php/l/141570183983908725.html">2017年小学毕业,学籍还是在小学毕业状态,开学能直接到初中初三去吗?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://13l.net/index.php/l/1370623660058224939.html">艺星郭君口碑怎么样,我发现我快呆了。问下关于,麻烦各位了哦上点心</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://13l.net/index.php/l/1697118013124251228.html">上海足矣电子科技有限公司怎么样?</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://13l.net/index.php/l/591855633.html">大家来帮忙想想办法,我有一个朋友他被骗去搞传销了,进入的时间只有4天,如何才能帮助他逃出来。 她是</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://13l.net/index.php/l/1049565783111853619.html">从东直门到海定区西三旗怎么做地铁</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://13l.net/index.php/l/624875059849079004.html">guess what we✀re doing?是什么意思</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://13l.net/index.php/l/245743005539846364.html">紧急!外地远程教育本科学历对上海居住证积分是否是影响?最近在户籍地报考的远程教育专升本。</a></div> </div> <div class="wdxgwtcont"> <div class="wdxgtitle"><a href="https://13l.net/index.php/l/45786415.html">阅读小诗,按要求写作&lt;急哦.帮个忙诶`3Q&gt;</a></div> </div> </div> </div> </div> <div class="clear"></div> <div class="footer"> <!-- 移动底部导航 --> <div class="fanhuitop"><a href="#top" ref="nofollow"><img src="https://13l.net/static/old/img/fhtop.png" alt="返回顶部" title="返回顶部"></a></div> <div class="dibu"> <div class="dibu"> </div> </div> <div class="banquan"> <p>内容全部来源于网络收集,如有侵权,请联系网站删除:QQ:24596024</p> </div> </div> </div> </div> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?de17be6dbd20544dd6483cc235b540f9"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>