📄 xmltools.java
字号:
package com.vere.util;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.w3c.dom.Element;
import org.w3c.dom.DocumentType;
import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Comment;
import org.w3c.dom.CDATASection;
import java.io.InputStream;
public class XMLTools {
private XMLTools() {
}
public static Document inputStreamToDocument(InputStream is) {
if (is == null)
throw new IllegalArgumentException("XMLTools类的inputStreamToDocument方法:参数为null。");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document doc = null;
try {
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(is);
}catch(ParserConfigurationException e){
throw new IllegalArgumentException("XMLTools类的inputStreamToDocument方法:" + e.toString());
}catch(Exception e){
throw new IllegalArgumentException("XMLTools类的inputStreamToDocument方法:" + e.toString());
}
return doc;
}
public static Document stringToDocument(String s) {
//InputStream is = StringTools.stringToInputStream(s);
InputStream is = null;
if (is == null)
return null;
else
return inputStreamToDocument(is);
}
public static void nodeToString(Node node,StringBuffer result,StringBuffer indent) {
if (node == null) return;
switch (node.getNodeType()) {
case Node.DOCUMENT_NODE:
if (node.hasChildNodes()) {
NodeList nodeList = node.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++)
nodeToString(nodeList.item(i),result,indent);
}
break;
case Node.ATTRIBUTE_NODE:
result.append(" " + node.getNodeName() + "=\"" + node.getNodeValue() +"\"");
break;
case Node.CDATA_SECTION_NODE:
result.append(indent.toString() + "<![CDATA\n" + ((CDATASection)node).getData() + "\n" + indent.toString() + "]]>\n");
break;
case Node.COMMENT_NODE:
result.append(indent.toString() + "<!--"+((Comment)node).getData() + " -->\n");
break;
case Node.DOCUMENT_FRAGMENT_NODE:
result.append(indent.toString() + "<" + node.getNodeName());
if (node.hasAttributes()) {
NamedNodeMap nnm = node.getAttributes();
for (int i = 0;i < nnm.getLength();i++)
nodeToString(nnm.item(i),result,indent);
}
if (!node.hasChildNodes())
result.append("/>");
else {
result.append(">\n");
NodeList nl = node.getChildNodes();
for (int i = 0;i<nl.getLength();i++)
nodeToString(nl.item(i),result,indent.append(" "));
result.append("</" + node.getNodeName() + ">\n");
}
break;
case Node.DOCUMENT_TYPE_NODE:
DocumentType docType = (DocumentType)node;
result.append(indent.toString() + "<!DOCTYPE " + docType.getName() + " ");
result.append((docType.getPublicId() != "" ) ? "PUBLIC \"" + docType.getPublicId() +"\">" : "");
result.append((docType.getSystemId() != "" ) ? "SYSTEM \"" + docType.getSystemId() +"\">" : "");
break;
case Node.ELEMENT_NODE:
Element element = (Element)node;
//Node element = node;
result.append("<" + element.getNodeName() + " ");
if (element.hasAttributes()) { //
NamedNodeMap nnm = element.getAttributes();
for (int i = 0; i<nnm.getLength(); i++)
nodeToString(nnm.item(i),result,indent);
}
if (!element.hasChildNodes())
result.append("/>\n");
else {
result.append(">\n");
NodeList nl = element.getChildNodes();
for (int i = 0; i<nl.getLength(); i++)
nodeToString(nl.item(i),result,indent.append(" "));
result.append("</" + element.getNodeName() + ">\n");
}
break;
case Node.ENTITY_NODE:
break;
case Node.ENTITY_REFERENCE_NODE:
break;
case Node.NOTATION_NODE:
break;
case Node.PROCESSING_INSTRUCTION_NODE:
ProcessingInstruction pi = (ProcessingInstruction)node;
result.append(indent.toString() + "<?" + pi.getTarget() + " " + pi.getData() + " ?>\n");
break;
case Node.TEXT_NODE:
Text txt = (Text)node;
result.append(txt.getData() + "\n");
break;
default:
break;
}
}
public static final String nodeToString(Node node) {
if (node == null)
return "";
StringBuffer result = new StringBuffer(""),indent = new StringBuffer("");
nodeToString(node,result,indent);
return result.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -