标签:elements throw user row std string getname next lis
Element root = document.getRootElement();
Element db_element = root.element("db");
List<Element> db_list = db_element.elements();//所有子元素
element.attributeValue("name");
element.getText();
element.elements("user");//指定名称的子元素集合
<users> <user no="2017"> <name>狗蛋</name> <age>17</age> </user> <user no="2018"> <name>李狗蛋</name> <age>19</age> </user> </users>
1、导入dom4j的jar包,遍历
public void test4j() throws DocumentException { //创建解析器 SAXReader reader = new SAXReader(); org.dom4j.Document document = reader.read(new File("src/userInfo.xml")); org.dom4j.Element root = document.getRootElement(); List<org.dom4j.Element> list = root.elements(); for(org.dom4j.Element e:list){ //获取属性值 String no = e.attributeValue("no"); String name = e.element("name").getText(); String age = e.element("age").getText(); System.out.println(no+name+age); } }
List<Node> list= document.selectNodes("users/user/name");
node.getName();
node.getText();
node.valuesOf("@no");
public void testdom4j_xpath() throws DocumentException {
SAXReader reader = new SAXReader();
org.dom4j.Document document = reader.read(new File("src/userInfo.xml"));
List<Node> list= document.selectNodes("users/user/name");
for(Node node:list){
String qname = node.getName();
String name = node.getText();
System.out.println(qname+"--"+name);
}
List<org.dom4j.Node> list1 = document.selectNodes("users/user");
for(Node node:list1){
String no = node.valueOf("@no");
System.out.println(no);
}
}
Document document = DocumentHelper.createDocument();
Element root = document.addElement("users");
Element e = element.addElement("name");
Element e = element.setText("张三");
e.remove(element);
Attribute attribute=element.attribute("id");
String text=attribute.getText();
Attribute attribute=root.attribute("size"); root.remove(attribute);
Element root=document.getRootElement();
for(Iterator it=root.attributeIterator();it.hasNext();){
Attribute attribute = (Attribute) it.next();
String text=attribute.getText();
System.out.println(text);
}
newMemberElm.addAttribute("name", "sitinspring");
Attribute attribute=root.attribute("name");
attribute.setText("csdn");
标签:elements throw user row std string getname next lis
原文地址:https://www.cnblogs.com/alsf/p/9278703.html