在线等......dom 转换 xml
需求,给order.xml 中的节点RCPTNO赋值
order.xml 如下:
<?xml version="1.0" encoding="UTF-8"?>
<TRANSACTIONS>
<TRANSACTION>
<RCPTNO></RCPTNO>
</TRANSACTION>
</TRANSACTIONS>
java 代码:
public static void main(String[] args) throws Exception {
//xml转化成 dom
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("order.xml");
doc.normalize();
String s = "Hello";
Text textseg = doc.createTextNode(s);
doc.getElementsByTagName("RCPTNO").item(0).appendChild(textseg);
//dom文档 转化成 xml
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("order.xml"));
transformer.transform(source, result);
}
所报异常:
Exception in thread "main"
java.lang.AbstractMethodError: org.apache.xerces.dom.DeferredDocumentImpl.getXmlStandalone()Z
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.setDocumentInfo(DOM2TO.java:373)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:127)
at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:94)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:662)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:708)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:313)
at test2.test.main(test.java:26)
------解决方案--------------------
------解决方案--------------------
In case anyone else gets this error - I fixed it by removing
xerces.jar and replacing it with xercesImpl.jar.
http://groups.google.com/group/google-checkout-api-java/browse_thread/thread/c69a86056fd51462/c28b5fb1916c017c
你的结帖率太低了
------解决方案--------------------
http://groups.google.com/group/google-checkout-api-java/browse_thread/thread/c69a86056fd51462/c28b5fb1916c017c
在这找的,你看看去
------解决方案--------------------
2楼说了I fixed it by removing
xerces.jar and replacing it with xercesImpl.jar
替换一下看看吧 ,应该是使用了没有实现的抽象类的方法了
------解决方案--------------------
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import
javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.Transformer
ConfigurationException;
import
javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
public class TestT {
public void create(Document doc) {
// 创建元素
Element eCate = doc.createElement("catalog");
Element eProduct = doc.createElement("products");
Element eProd_id = doc.createElement("prod_id");
Element eProd_name = doc.createElement("prod_name");
Element ePrice = doc.createElement("price");