dom4j解析xml
importorg.dom4j.Document;
importorg.dom4j.DocumentException;
importorg.dom4j.Element;
import org.dom4j.Node;importjava.util.Iterator;
importjava.util.List;
import java.io.*;SAXReaderreader=newSAXReader();
Document document = reader.read(new File("input.xml"));Stringtext="<members></members>";
Documentdocument=DocumentHelper.parseText(text);
3.主动创建document对象.
Documentdocument=DocumentHelper.createDocument();
Element root = document.addElement("members");// 创建根节点ElementrootElm=document.getRootElement();
ElementmemberElm=root.element("member");//"member"是节点名
Stringtext=memberElm.getText();
也可以用:
String text=root.elementText("name");这个是取得根节点下的name字节点的文字.Elementelm=(Element)it.next();
//dosomething
}
for(Iteratorit=root.elementIterator();it.hasNext();){
Elementelement=(Element)it.next();
//dosomething
}
ElementageElm=newMemberElm.addElement("age");
ageElm.setText("29");
三.属性相关
1.取得某节点下的某属性
Elementroot=document.getRootElement();
Attribute attribute=root.attribute("size");// 属性名name2.取得属性的文字
Stringtext=attribute.getText();
也可以用:
String text2=root.element("name").attributue("firstname");这个是取得根节点下name字节点的属性firstname的值.Elementroot=document.getRootElement();
for(Iteratorit=root.attributeIterator();it.hasNext();){
Attributeattribute=(Attribute)it.next();
Stringtext=attribute.getText();
System.out.println(text);
}4.设置某节点的属性和文字.
newMemberElm.addAttribute("name", "sitinspring");5.设置属性的文字
Attributeattribute=root.attribute("name");
attribute.setText("sitinspring");6.删除某属性
Attributeattribute=root.attribute("size");//属性名name
root.remove(attribute);四.将文档写入XML文件
1.文档中全为英文,不设置编码,直接写入的形式.
XMLWriterwriter=newXMLWriter(newFileWriter("output.xml"));
writer.write(document);
writer.close();2.文档中含有中文,设置编码格式写入的形式.
OutputFormatformat=OutputFormat.createPrettyPrint();
format.setEncoding("GBK");//指定XML编码
XMLWriterwriter=newXMLWriter(newFileWriter("output.xml"),format);
writer.write(document);
writer.close();五.字符串与XML的转换
1.将字符串转化为XML
Stringtext="<members><member>sitinspring</member></members>";
Document document = DocumentHelper.parseText(text);2.将文档或节点的XML转化为字符串.
SAXReaderreader=newSAXReader();
Documentdocument=reader.read(newFile("input.xml"));
Elementroot=document.getRootElement();
StringdocXmlText=document.asXML();
StringrootXmlText=root.asXML();
ElementmemberElm=root.element("member");
String memberXmlText=memberElm.asXML();六.使用XPath快速找到节点
读取的XML文档示例
<?xmlversion="1.0"encoding="UTF-8"?>
<projectDescription>
<name>MemberManagement</name>
<comment></comment>
<projects>
<project>PRJ1</project>
<project>PRJ2</project>
<project>PRJ3</project>
<project>PRJ4</project>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>publicstaticvoidmain(String[]args){
SAXReaderreader=newSAXReader();
try{
Documentdoc=reader.read(newFile("sample.xml"));
Listprojects=doc.selectNodes("/projectDescription/projects/project");
Iteratorit=projects.iterator();
while(it.hasNext()){
Elementelm=(Element)it.next();
System.out.println(elm.getText());
}
}
catch(Exceptionex){
ex.printStackTrace();
}
}