java中怎么将输入的值和xml中根节点的属性进行匹配
<?xmlversion="1.0"encoding="UTF8"?><Allmember><memberid="1"name="张三"department="事业部"p...
<?xml version="1.0" encoding="UTF8"?>
<Allmember>
<member id ="1" name="张三" department="事业部" password="1" arrive="false">
</member>
<member id ="2" name="张四" department="事业部" password="2" arrive="false">
</member>
<member id ="3" name="李四" department="工程部" password="3" arrive="false">
</member>
<member id ="4" name="李五" department="工程部" password="4" arrive="false">
</member>
<member id ="0" name="管理员" department="管理" password="5" arrive="false">
</member>
</Allmember>
谢谢 展开
<Allmember>
<member id ="1" name="张三" department="事业部" password="1" arrive="false">
</member>
<member id ="2" name="张四" department="事业部" password="2" arrive="false">
</member>
<member id ="3" name="李四" department="工程部" password="3" arrive="false">
</member>
<member id ="4" name="李五" department="工程部" password="4" arrive="false">
</member>
<member id ="0" name="管理员" department="管理" password="5" arrive="false">
</member>
</Allmember>
谢谢 展开
2个回答
展开全部
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
需要dom4j.jar
Document document = DocumentHelper.parseText(xml字符串);
Element Allmember= document.getRootElement();
List<Element> list = Allmember.elements("member");
for(int i=0,j=list.size();i<j;j++){
Element ele = list.get(i);
String name = ele.attribute("name ");//得到name 属性
}
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
需要dom4j.jar
Document document = DocumentHelper.parseText(xml字符串);
Element Allmember= document.getRootElement();
List<Element> list = Allmember.elements("member");
for(int i=0,j=list.size();i<j;j++){
Element ele = list.get(i);
String name = ele.attribute("name ");//得到name 属性
}
追问
如果用dom方法呢,还有怎么更改属性中的值啊,不用遍历每个, 只更改我想改的,麻烦了!
追答
首先,不遍历找不到你想改的节点,dom4j中有修改节点属性的方法
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询