Document xml文档的整体
Element xml文档中的各个节点<element>element的Text</element>
Attribute 节点Element中的属性 <item 属性1=值1 属性2=值2>Text</item>
**************************************** 分割 ****************************************
****************************************Document对象创建:****************************************
1、从文档中读取
SAXReader reader = new SAXReader();
Document document = reader.read(new File("mytest.xml"));
2、xml字符串
String s = "<Element></Element>";
Document document = DocumentHelper.parseText(s);
3、直接创建
Document document = DocumentHelper.createDocument();//创建根节点
Element root = document.addElement("root");
root.addElement("element1");
root.addElement("element2");
Element e3 = root.addElement("element3");
Element newE = e3.addElement("newElement");
newE.addAttribute("id","testId")
<root>
<element1>
</element1>
<element2>
</element2>
<element3>
<newElement id="testId">
</newElement>
</element3>
</root>
**************************************** 重要的3个类。 ****************************************
**************************************** Document: ****************************************
document.getRootElement(); //获取根节点
**************************************** Element: ****************************************
element.element("testE");//获取element节点下的testE节点,返回Element
element.addElement("testE");//在Element节点下增加testE节点,返回该增加的Element
element.getText();//获取Element节点的内容 返回内容
element.setText("text");//Element节点中增加text内容
element.addCDATA("text");//Element节点中增加cdata数据
element.addAttribute("id","testId");//Element节点中增加id属性以及值testI的
element.attribute("id");//获取id属性对象,返回Attribute对象。
element.attributeValue("id");//获取id属性的值。
Element的遍历
element.elementIterator();获取element下的所有element,返回Iterator迭代器对象
注:迭代器是种设计模式,用于遍历并选择序列中的对象。java.lang.Iterable接口,被Collection继承。Set,List,接口都继承于Collection
for(Iterator it=element.elementIterator();it.hasNext();){
Element element = (Element) it.next();
//……一系列处理
}
Iterator it = element.elementIterator();
while(it.hasNext()){
Element element = (Element) it.next();
//……一系列处理
}
***************************************** Attribute: ***************************************
element.attribute("id"); //获得element的id属性,返回Attribute对象。
attribute.getText(); //获取属性的值。
Attribute的遍历
element.attributeIterator();//获得element对象的所有属性,返回Iterator迭代器对象
package xyz.jangle.dom4j.test;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class Dom4jTest {
public static void main(String[] args) {
String s = "<Element></Element>";
try {
//创建xml文档对象
Document document = DocumentHelper.parseText(s);
//增加节点
Element rootElement = document.getRootElement();
Element e2 = rootElement.addElement("e2");
Element e3 = rootElement.addElement("e3");
e3.addAttribute("id", "2");
//节点增加文本内容
e2.addText("这里是文本内容Text");
//节点增加属性
e2.addAttribute("这里是属性名称", "这里是属性值Value");
e2.addAttribute("id", "1");
//xml转String
String asXML = document.asXML();
System.out.println(asXML);
//遍历节点
Iterator<?> elementIterator = rootElement.elementIterator();
while(elementIterator.hasNext()){
Element element = (Element)elementIterator.next();
System.out.println(element.getName());
System.out.println(element.getText());
}
@SuppressWarnings("unchecked")
List<Element> list = rootElement.elements();
for(Element e : list){
System.out.println(e.getName());
System.out.println(e.getText());
}
//遍历属性
Iterator<?> attributeIterator = e2.attributeIterator();
while(attributeIterator.hasNext()){
Attribute a = (Attribute)attributeIterator.next();
System.out.println(a.getName()+" "+a.getValue());
}
@SuppressWarnings("unchecked")
List<Attribute> attributes = e2.attributes();
for(Attribute a : attributes){
System.out.println(a.getName());
System.out.println(a.getValue());
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
本文详细介绍了如何使用Java的Dom4j库解析和操作XML文档,包括文档创建、读取、元素及属性的增删改查等核心操作。通过实例演示了如何增加节点、设置文本内容、添加属性以及遍历节点和属性,提供了完整的代码示例。
504

被折叠的 条评论
为什么被折叠?



