xml格式:
<root>
<divice type="people">
<productClass name="jack">
<age>20</age>
</productClass>
</divice>
</root>
//java代码:
public void getSeXmlDate(String str,String age){
File file =new File("d:\abc.xml");
Document doc = null;
try {
SAXBuilder sb = new SAXBuilder();
doc = sb.build(file.getAbsolutePath());
} catch (Exception ex) {
LOGGER.error(ex);
}
Element root = doc.getRootElement();
for (int i = 0; i < root.getChildren().size(); i++)
{
Element ee = (Element) root.getChildren().get(i);
if (ee.getAttributeValue("type").equalsIgnoreCase("people"))
{
for (int j = 0; j < ee.getChildren().size(); j++)
{
Element el = (Element) ee.getChildren().get(j);
if (el.getAttributeValue("name").equalsIgnoreCase("jack"))
{
Element ele = el.getChild("age");//得到需要读写的节点
try
{
if(str.equals("get"))
{
refreshDate = Integer.parseInt(ele.getTextTrim());//读出节点值
}else if(str.equals("set"))
{
ele.setText(age);//修改节点值
XMLOutputter outputter=new XMLOutputter();
outputter.output(doc,new FileOutputStream(d:\abc.xml));//写入文件流,修改xml内容
}
} catch (Exception es)
{
LOGGER.error(es);
}
}
}
}
}
}