XMLConfiguration是apache工具集里的一个用于对XML文件进行读写的东东,使用起来很方便。 但 有个问题一直很茫然... 如何获取子节点的个数?
<?xml version="1.0" encoding="utf-8"?>
<tree>
<unit id="root" type="root">
<back-ground>/root/Desktop/image/4.jpg </back-ground>
<unit id="1" type="alarm">
<back-ground>/root/Desktop/image/5.jpg </back-ground>
<father>root </father>
<position>x=250:y=225 </position>
</unit>
</unit>
</tree>
如上代码 如何获取 <tree>节点下的子节点个数?
<?xml version="1.0" encoding="utf-8"?>
<tree>
<unit id="root" type="root">
<back-ground>/root/Desktop/image/4.jpg </back-ground>
<unit id="1" type="alarm">
<back-ground>/root/Desktop/image/5.jpg </back-ground>
<father>root </father>
<position>x=250:y=225 </position>
</unit>
</unit>
</tree>
如上代码 如何获取 <tree>节点下的子节点个数?
try {
File file = new File("G://imgtest//test.xml");//创建file对象
/*从输入流中获取文件
InputStream input = new FileInputStream(file);
XMLConfiguration xmlc = new XMLConfiguration();
xmlc.load(input);
*/
//直接读取
XMLConfiguration xmlc = new XMLConfiguration(file);
System.out.println(xmlc.getString("nodes[@id]"));
System.out.println(xmlc.getString("nodes.propertiy"));
xmlc.setProperty("nodes.propertiy", "lin");//修改值
xmlc.save();//保存
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
<test>
<nodes id="12s2">
<propertiy>lin</propertiy>
</nodes>
</test>