新手求救——关于DOM
说好不能打脸
Java领域优质创作者
博客专家认证 2005-03-03 08:33:59 最近刚开始学XML,看得一头雾水,小弟写了一段代码,大致如下(只保留了关键部分)
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.io.FileNotFoundException;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.DOMException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
import org.apache.xerces.dom.DOMImplementationImpl;//解析器
public class Test6 {
private Attr myAttr;//元素属性
private Document xmlDoc;//文档对象
private Element myElement,tempElement;//文档元素
private Node myNode;//节点对象
private Text myText;//该对象表示文档中的元素的内容
public Test6(){
/*初始化新建立的xml文档*/
DOMImplementationImpl domParser = new DOMImplementationImpl();
/**
* 以下代码建立一个新的xml树
* */
xmlDoc = domParser.createDocument(null,"table",null);
myElement = xmlDoc.getDocumentElement();//返回当前指向节点
myAttr = this.xmlDoc.createAttributeNS("id","1");
myElement.setAttributeNode(myAttr);//这两句设置了元素的一个属性
tempElement = xmlDoc.createElement("tr");
myText = this.xmlDoc.createTextNode("rktkreto");
this.myElement.appendChild(tempElement);
}
public static void main(String[] src){
new Test6();
}
}
用到的解析器是:org.apache.xerces.dom.DOMImplementationImpl;
在"DOMImplementationImpl domParser = new DOMImplementationImpl();"报错,错误提示:
java.lang.NoClassDefFoundError: org/apache/xerces/dom/DOMImplementationImpl
at xml.Test6.<init>(Test6.java:39)
at xml.Test6.main(Test6.java:66)
Exception in thread "main"
那位大哥救救我,还有一个问题:如何将一个修改好的dom树生成一个xml文档,好像要用到串行化,但怎么用呢?