首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

.dom 转换 xml

2012-02-12 
在线等......dom 转换 xml需求,给order.xml 中的节点RCPTNO赋值order.xml 如下:?xml version1.0 encod

在线等......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.TransformerConfigurationException;
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");


// 创建文本节点
Text tProd_id = doc.createTextNode("t1");
Text tProd_name = doc.createTextNode("手机");
Text tPrice = doc.createTextNode("1000元");
// 附加

eProd_id.appendChild(tProd_id);
eProd_name.appendChild(tProd_name);
ePrice.appendChild(tPrice);

eProduct.appendChild(eProd_id);
eProduct.appendChild(eProd_name);
eProduct.appendChild(ePrice);

eCate.appendChild(eProduct);
doc.appendChild(eCate);

}

// 添加
public void add(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");
// 创建文本节点
Text tProd_id = doc.createTextNode("t2");
Text tProd_name = doc.createTextNode("脚机");
Text tPrice = doc.createTextNode("500");
// 附加
eProd_id.appendChild(tProd_id);
eProd_name.appendChild(tProd_name);
ePrice.appendChild(tPrice);

eProduct.appendChild(eProd_id);
eProduct.appendChild(eProd_name);
eProduct.appendChild(ePrice);
eCate.appendChild(eProduct);
Element root = doc.getDocumentElement();
root.appendChild(eCate);

}

// 生成xml文件
public void cheng(Document doc) {
TransformerFactory tran = TransformerFactory.newInstance();
try {
Transformer transformer = tran.newTransformer();
DOMSource soucre = new DOMSource(doc);
StreamResult res = new StreamResult("cate.xml");
transformer.setOutputProperty("encoding", "gb2312");
transformer.transform(soucre, res);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}

// 修改
public void modify(Document doc) {
NodeList list = doc.getElementsByTagName("products");
for (int i = 0; i < list.getLength(); i++) {
Element e = (Element) list.item(i);
Node nName = e.getElementsByTagName("prod_name").item(0);
nName.getFirstChild().setNodeValue("中国");
}
}

// 替换
public void replace(Document doc) {
NodeList list = doc.getElementsByTagName("products");
for (int i = 0; i < list.getLength(); i++) {
Element e = (Element) list.item(i);
Node nId = e.getElementsByTagName("prod_id").item(0);
Node nName = e.getElementsByTagName("prod_name").item(0);
Node ele = (Node) doc.createElement("repalce");
Text rep = doc.createTextNode("替换");
ele.appendChild(rep);
e.replaceChild(ele, nName);

}

}

// 删除
public void delete(Document doc) {
NodeList list = doc.getElementsByTagName("products");
for (int i = 0; i < list.getLength(); i++) {
Element e = (Element) list.item(i);
Node nId = e.getElementsByTagName("prod_id").item(0);
e.removeChild(nId);
}
}

public static void main(String[] args) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
TestT t = new TestT();
t.create(doc);
t.add(doc);
t.modify(doc);
t.replace(doc);
t.delete(doc);
t.cheng(doc);

} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

热点排行