oracle xdk我就没有用过。
用xerces's dom:
================================
import java.io.*;
import org.w3c.dom.*;
import org.apache.xerces.dom.*;
import org.apache.xml.serialize.*;
public class HelloDOM
{
public static void main(String[] args)
{
try
{
DOMImplementationImpl impl = (DOMImplementationImpl) // like the build factory of sun
DOMImplementationImpl.getDOMImplementation(); DocumentType type = impl.createDocumentType("HelloWorld", null, null); // type is supposed to be able to be null,
// but in practice that didn't work
DocumentImpl hello = (DocumentImpl) impl.createDocument(null, "HelloWorld", type); Element root = hello.createElement("GREETING"); // We can't use a raw string. Instead we have to first create
// a text node.
Text text = hello.createTextNode("Hello DOM!");
root.appendChild(text);
Element element = hello.createElement("Hello");
text = hello.createTextNode("world");
element.appendChild(text);
root.appendChild(element); // Now that the document is created we need to *serialize* it
try {
OutputFormat format = new OutputFormat(hello,"ISO-8859-1",false);
XMLSerializer serializer = new XMLSerializer(System.out, format);
serializer.serialize(root);
}
catch (IOException e) {
System.err.println(e);
}
}
catch (DOMException e) {
e.printStackTrace();
}
}
}
用xerces's dom:
================================
import java.io.*;
import org.w3c.dom.*;
import org.apache.xerces.dom.*;
import org.apache.xml.serialize.*;
public class HelloDOM
{
public static void main(String[] args)
{
try
{
DOMImplementationImpl impl = (DOMImplementationImpl) // like the build factory of sun
DOMImplementationImpl.getDOMImplementation(); DocumentType type = impl.createDocumentType("HelloWorld", null, null); // type is supposed to be able to be null,
// but in practice that didn't work
DocumentImpl hello = (DocumentImpl) impl.createDocument(null, "HelloWorld", type); Element root = hello.createElement("GREETING"); // We can't use a raw string. Instead we have to first create
// a text node.
Text text = hello.createTextNode("Hello DOM!");
root.appendChild(text);
Element element = hello.createElement("Hello");
text = hello.createTextNode("world");
element.appendChild(text);
root.appendChild(element); // Now that the document is created we need to *serialize* it
try {
OutputFormat format = new OutputFormat(hello,"ISO-8859-1",false);
XMLSerializer serializer = new XMLSerializer(System.out, format);
serializer.serialize(root);
}
catch (IOException e) {
System.err.println(e);
}
}
catch (DOMException e) {
e.printStackTrace();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货