Tip:节点对象
v1.获取文档的根节点.
Element root = document.getRootElement();
v2.取得某个节点的子节点.
Element element=node.element(“书名");
v3.取得节点的文字
String text=node.getText();
v4.取得某节点下所有名为
“member
”的子节点,并进行遍历.
List nodes = rootElm.elements("member");
for (Iterator it = nodes.iterator(); it.hasNext();) {
Element elm = (Element) it.next();
// do something
}
List nodes = rootElm.elements("member");
for (Iterator it = nodes.iterator(); it.hasNext();) {
Element elm = (Element) it.next();
// do something
}
v5.对某节点下的所有子节点进行遍历.
for(Iterator it=root.elementIterator();it.hasNext();){
Element element = (Element) it.next();
// do something
}
for(Iterator it=root.elementIterator();it.hasNext();){
Element element = (Element) it.next();
// do something
}
v6.在某节点下添加子节点.
Element ageElm = newMemberElm.addElement("age");
Element ageElm = newMemberElm.addElement("age");
v7.设置节点文字.
element.setText("29");
element.setText("29");
v8.删除某节点.
//childElm是待删除的节点,parentElm是其父节点
//childElm是待删除的节点,parentElm是其父节点
parentElm.remove(childElm);
v9.添加一个CDATA节点.
Element contentElm = infoElm.addElement("content");
contentElm.addCDATA(diary.getContent());
Element contentElm = infoElm.addElement("content");
contentElm.addCDATA(diary.getContent());
节点对象属性
v1.取得某节点下的某属性
Element root=document.getRootElement();
//属性名name
Element root=document.getRootElement();
//属性名name
Attribute attribute=root.attribute("size");
v2.取得属性的文字
String text=attribute.getText();
String text=attribute.getText();
v 3.删除某属性
Attribute attribute=root.attribute("size");
root.remove(attribute);
Attribute attribute=root.attribute("size");
root.remove(attribute);
v3.遍历某节点的所有属性
Element root=document.getRootElement();
for(Iterator it=root.attributeIterator();it.hasNext();){
Attribute attribute = (Attribute) it.next();
String text=attribute.getText();
System.out.println(text);
}
Element root=document.getRootElement();
for(Iterator it=root.attributeIterator();it.hasNext();){
Attribute attribute = (Attribute) it.next();
String text=attribute.getText();
System.out.println(text);
}
v4.设置某节点的属性和文字.
newMemberElm.addAttribute("name", "sitinspring");
newMemberElm.addAttribute("name", "sitinspring");
v5.设置属性的文字
Attribute attribute=root.attribute("name");
attribute.setText("sitinspring");
Attribute attribute=root.attribute("name");
attribute.setText("sitinspring");