<?xml version="1.0" encoding="UTF-8" standalone="no"?><graph caption="send" decimalPrecision="0" formatNumberScale="0" showNames="1" xAxisName="比例" yAxisName="Units">
<set color="AFD8F8" name="success" value="46"/>
<set color="F6BD0F" name="false" value="10"/>
</graph>
我想修改<set color="AFD8F8" name="success" value="46"/>里的value的值改为20请各位高手帮忙
String path = "c:/test.xml";
SAXReader reader = new SAXReader();
Document document = reader.read(new File(path));
Element root = document.getRootElement(); boolean flag = false;
for (Iterator it = root.elementIterator(); it.hasNext();) {
Element element = (Element) it.next();
if ("AFD8F8".equals(element.attribute("color").getValue())
&& "success".equals(element.attribute("name").getValue())) {
element.attribute("value").setValue("20");
flag = true;
break;
}
} if (flag) {
FileWriter out = new FileWriter(path);
document.write(out);
out.flush();
out.close();
}
<?xml version="1.0" encoding="gb2312"?>
<books>
<book email="[email protected]">
<name>Java核心技术</name>
<author>Wyat</author>
<pageCount>1359</pageCount>
<printDate>2008/03/26</printDate>
<price>89.63</price>
</book>
<book email="[email protected]">
<name>三只小猪的独白</name>
<author>李诚铭</author>
<pageCount>3586</pageCount>
<printDate>2000/11/07</printDate>
<price>112.67</price>
</book>
</books>java类调用以下方法:
/**
* @desc DOM读取XML的方法
* @author Jim
* @date 2010-09-02
* @param
*/
public static void readXmlByDom(){
long lasting = System.currentTimeMillis();
try {
File file = new File("WebRoot\\test.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder docbuder = dbf.newDocumentBuilder();
Document doc = docbuder.parse(file);
NodeList nl = doc.getElementsByTagName("book");
for(int i=0;i<nl.getLength();i++){
String bookName = doc.getElementsByTagName("name").item(i).getFirstChild().getNodeValue();
String bookAuthor = doc.getElementsByTagName("author").item(i).getFirstChild().getNodeValue();
String bookPageCount = doc.getElementsByTagName("pageCount").item(i).getFirstChild().getNodeValue();
String bookPrintDate = doc.getElementsByTagName("printDate").item(i).getFirstChild().getNodeValue();
String bookPrice = doc.getElementsByTagName("price").item(i).getFirstChild().getNodeValue();
String bookEmail = "";
NamedNodeMap nnm = nl.item(i).getAttributes();
for(int j =0;j<nnm.getLength();j++){
bookEmail = nnm.item(j).getNodeValue();
}
System.out.println("");
System.out.println("书 名 :《" + bookName + "》");
System.out.println("作 者 : " + bookAuthor + "");
System.out.println("出版日期 : " + bookPrintDate + "");
System.out.println("价 格 : " + bookPrice + " 元");
System.out.println("页 数 : " + bookPageCount + " 页");
System.out.println("电子信箱 : " + bookEmail + "");
System.out.println("---------------------");
}
long ending = System.currentTimeMillis();
System.out.println(ending-lasting+"毫秒");
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }
这个方法我测试过是成功的,既然能正确拿到值,显然更改那个属性值也很简单了!