给你个Class,传给它一个PrintWriter,然后再调用一下write方法(参数是利用DOM生成的Node对象)
就OK了
import org.w3c.dom.*;import java.io.*;public class XMLDocumentWriter { PrintWriter out; public XMLDocumentWriter(PrintWriter out) {
this.out = out;
} public void close() {
out.close();
} public void write(Node node) {
write(node, "");
} public void write(Node node, String indent) {
switch (node.getNodeType()) {
case Node.DOCUMENT_NODE: {
Document doc = (Document) node;
out.println(indent + "<?xml version='1.0'?>");
Node child = doc.getFirstChild();
while (child != null) {
write(child, indent);
child = child.getNextSibling();
}
break;
} case Node.DOCUMENT_TYPE_NODE: {
DocumentType doctype = (DocumentType) node; out.println("!DOCTYPE " + doctype.getName() + ">");
break;
}
case Node.ELEMENT_NODE: {
Element elt = (Element) node;
out.print(indent + "<" + elt.getTagName());
NamedNodeMap attrs = elt.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
Node a = attrs.item(i);
out.print(" " + a.getNodeName() + "='"
+ fixup(a.getNodeValue()) + "'"); }
out.println(">");
String newindent = indent + " ";
Node child = elt.getFirstChild();
while (child != null) {
write(child, newindent);
child = child.getNextSibling();
}
out.println(indent + "</" + elt.getTagName() + ">");
break;
}
case Node.TEXT_NODE: {
Text textNode = (Text) node;
String text = textNode.getData().trim();
if ((text != null) && text.length() > 0)
out.println(indent + fixup(text));
break;
}
case Node.PROCESSING_INSTRUCTION_NODE: {
ProcessingInstruction pi = (ProcessingInstruction) node;
out.println(indent + "<?" + pi.getTarget() + " " + pi.getData()
+ "?>");
break;
}
case Node.CDATA_SECTION_NODE: {
CDATASection cdata = (CDATASection) node;
out.println(indent + "<" + "![CDATA[" + cdata.getData() + "]]"
+ ">");
break;
}
case Node.COMMENT_NODE: {
Comment c = (Comment) node;
out.println(indent + "<!-- " + c.getData() + "-->");
break;
}
default:
System.out.println("Ignoring node: " + node.getClass().getName());
break;
} } String fixup(String s) {
StringBuffer sb = new StringBuffer();
int len = s.length();
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
switch (c) {
default:
sb.append(c);
break;
case '<':
sb.append("<");
break;
case '>':
sb.append(">");
break;
case '&':
sb.append("&");
break;
case '"':
sb.append(""");
break;
case '\'':
sb.append("'");
break; } }
return sb.toString();
}}
就OK了
import org.w3c.dom.*;import java.io.*;public class XMLDocumentWriter { PrintWriter out; public XMLDocumentWriter(PrintWriter out) {
this.out = out;
} public void close() {
out.close();
} public void write(Node node) {
write(node, "");
} public void write(Node node, String indent) {
switch (node.getNodeType()) {
case Node.DOCUMENT_NODE: {
Document doc = (Document) node;
out.println(indent + "<?xml version='1.0'?>");
Node child = doc.getFirstChild();
while (child != null) {
write(child, indent);
child = child.getNextSibling();
}
break;
} case Node.DOCUMENT_TYPE_NODE: {
DocumentType doctype = (DocumentType) node; out.println("!DOCTYPE " + doctype.getName() + ">");
break;
}
case Node.ELEMENT_NODE: {
Element elt = (Element) node;
out.print(indent + "<" + elt.getTagName());
NamedNodeMap attrs = elt.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
Node a = attrs.item(i);
out.print(" " + a.getNodeName() + "='"
+ fixup(a.getNodeValue()) + "'"); }
out.println(">");
String newindent = indent + " ";
Node child = elt.getFirstChild();
while (child != null) {
write(child, newindent);
child = child.getNextSibling();
}
out.println(indent + "</" + elt.getTagName() + ">");
break;
}
case Node.TEXT_NODE: {
Text textNode = (Text) node;
String text = textNode.getData().trim();
if ((text != null) && text.length() > 0)
out.println(indent + fixup(text));
break;
}
case Node.PROCESSING_INSTRUCTION_NODE: {
ProcessingInstruction pi = (ProcessingInstruction) node;
out.println(indent + "<?" + pi.getTarget() + " " + pi.getData()
+ "?>");
break;
}
case Node.CDATA_SECTION_NODE: {
CDATASection cdata = (CDATASection) node;
out.println(indent + "<" + "![CDATA[" + cdata.getData() + "]]"
+ ">");
break;
}
case Node.COMMENT_NODE: {
Comment c = (Comment) node;
out.println(indent + "<!-- " + c.getData() + "-->");
break;
}
default:
System.out.println("Ignoring node: " + node.getClass().getName());
break;
} } String fixup(String s) {
StringBuffer sb = new StringBuffer();
int len = s.length();
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
switch (c) {
default:
sb.append(c);
break;
case '<':
sb.append("<");
break;
case '>':
sb.append(">");
break;
case '&':
sb.append("&");
break;
case '"':
sb.append(""");
break;
case '\'':
sb.append("'");
break; } }
return sb.toString();
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货