java xml节点添加修改属性
<first><second><hereid="a"name="b"><."还有一些子节点".></here><hereid="c"name="d"><...></her...
<first>
<second>
<here id="a" name="b">
<."还有一些子节点".>
</here>
<here id="c" name="d">
<...>
</here>
</second>
</first>
就是让<here>里面加个东西变成<here id="a" name="b" color="q">...
用的是
import org.xml.sax.SAXException;
dom4j 展开
<second>
<here id="a" name="b">
<."还有一些子节点".>
</here>
<here id="c" name="d">
<...>
</here>
</second>
</first>
就是让<here>里面加个东西变成<here id="a" name="b" color="q">...
用的是
import org.xml.sax.SAXException;
dom4j 展开
1个回答
展开全部
SAXReader reader = new SAXReader();
Document doc = reader.read(new FileInputStream("d.xml"));
Element root = doc.getRootElement();
List<Element> list = root.selectNodes("//here");
for (Element e : list) {
System.out.println(e);
e.addAttribute("color", "q");
}
// 保存
XMLWriter writer = new XMLWriter(new FileWriter("output.xml"));
writer.write(doc);
writer.close();
Document doc = reader.read(new FileInputStream("d.xml"));
Element root = doc.getRootElement();
List<Element> list = root.selectNodes("//here");
for (Element e : list) {
System.out.println(e);
e.addAttribute("color", "q");
}
// 保存
XMLWriter writer = new XMLWriter(new FileWriter("output.xml"));
writer.write(doc);
writer.close();
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询