import java.io.OutputStream;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;
public class SerializerDemo {
public static void output(Document doc,OutputStream out){
try {
OutputFormat format = new OutputFormat(doc);
format.setEncoding("gb2312");
XMLSerializer serializer = new XMLSerializer(out,format);
serializer.serialize(doc);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Attr;
import org.w3c.dom.CDATASection;
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 DomparserTest5 {
public static void main(String[] args) {
DOMParser parser = new DOMParser();
try {
parser.parse("aaa.xml");
Document doc = parser.getDocument();
Node root = doc.getDocumentElement();
Element e = doc.createElement("aaa");
Attr a = doc.createAttribute("bbb");
Text t = doc.createTextNode("Hello World !");
a.setValue("cccc");
e.appendChild(t);
e.setAttributeNode(a);
root.insertBefore(e, root.getFirstChild());
SerializerDemo.output(doc, System.out);
} catch (Exception e) {
e.printStackTrace();
}
}
}