<?xml version="1.0" encoding="gb2312" ?><terminalstore>
<terminal number="A2" data="" sn="00000000" />
</terminalstore>请问我要读出sn的值然后修改再写入新的sn该如何???对xml方面不熟悉
<terminal number="A2" data="" sn="00000000" />
</terminalstore>请问我要读出sn的值然后修改再写入新的sn该如何???对xml方面不熟悉
首先你得选择其中的一种(如果文件很大,推荐SAX,如果文件小,且需要一次性读入内存,建议用DOM)。然后去官方网站下在相应的JAR包。
Java操作xml这一块知识很大,不可能在贴子里给你讲明白!
但是可以推荐你一些网站:
DOM入门:http://java.ccidnet.com/art/3539/20060310/474525_1.html
SAX入门:http://bbs.xml.org.cn/dispbbs.asp?boardID=11&ID=7965
首先你得选择其中的一种(如果文件很大,推荐SAX,如果文件小,且需要一次性读入内存,建议用DOM)。然后去官方网站下在相应的JAR包。
Java操作xml这一块知识很大,不可能在贴子里给你讲明白!
但是可以推荐你一些网站:
DOM入门:http://java.ccidnet.com/art/3539/20060310/474525_1.html
SAX入门:http://bbs.xml.org.cn/dispbbs.asp?boardID=11&ID=7965
Document document = null;
try {
SAXReader saxReader = new SAXReader();
document = saxReader.read(new File(filename));
} catch (Exception ex) {
ex.printStackTrace();
}
return document;
} public static void saveDoc(Document doc, String savefilename) throws
IOException {
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileOutputStream(savefilename),
format);
writer.write(doc);
writer.close();
} Document doc = loadFile("文件名.xml");
/** 直接取得 node 的值 */
List list = doc.selectNodes("terminalstore/terminal/@sn");
Iterator it = list.iterator();
if (it.hasNext()) {
Attribute attribute = (Attribute) it.next();
result = attribute.getValue();
//修改node的值
try {
attribute.setValue(modvalue);
saveDoc(doc, "文件名.xml");
} catch (Exception e) {
e.printStackTrace();
}
}
// e510 The Quintessential Program to Create a DOM Document from an XML File
Document doc = parseXmlFile("infilename.xml", false);
// Obtain an element
Element element = doc.getElementById("key1");
// Determine the presence of an attribute
boolean has = element.hasAttribute("value"); // true
// Get an attribute value; returns null if not present
String attrValue = element.getAttribute("value"); // value1
// Change the value of an attribute
element.setAttribute("value", "newValue1");
// If an attribute is not specified in the XML but has a
// default value, the attribute is automatically created
element = doc.getElementById("key2");
has = element.hasAttribute("value"); // true
attrValue = element.getAttribute("value"); // value2
// If the attribute value contains special characters,
// they are automatically converted to entities when the
// document in dumped
element.setAttribute("value", "a<\"'&>z");This is the sample input for the example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE map [ <!ELEMENT map (entry*) >
<!ELEMENT entry EMPTY >
<!ATTLIST entry key ID #REQUIRED
value CDATA "mydefault"> ]>
<map>
<entry key="key1" value="value1"/>
<entry key="key2" />
</map>The resulting XML from running the example is:
<?xml version="1.0" encoding="UTF-8"?>
<map>
<entry key="key1" value="newValue1"/>
<entry key="key2" value="a<"'&>z"/>
</map>
at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230)
at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207)
at org.dom4j.tree.AbstractNode.selectNodes(AbstractNode.java:164)
at test.main(test.java:42)
报“java.lang.NoClassDefFoundError: org/jaxen/JaxenException ”是缺少jaxen包