现在给出xml文件
<enterprise-bean>
<entity name="**********">
<local> text_value </local>
<remote> text_value </remote>
</entity>
</enterprise-bean>
现在我们来看上面xml文件的DOM树生成代码
/**
* 创建DOM树
*
* @return Document
*/
public static Document createDOM(){
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance() ;
DocumentBuilder builder = factory.newDocumentBuilder() ;
Document dom = builder.newDocument() ;
// 创建根节点
Element root = dom.createElement("enterprise-bean") ;
dom.appendChild(root) ; //生成节点enterprise-bean
Element subRoot = dom.createElement("entity") ;
root.appendChild(subRoot) ; //生成节点entity
Attr prop = dom.createAttribute("name") ;
prop.setNodeValue("com.vanceinfo.*") ;
subRoot.setAttributeNode(prop) ; //创建节点entity的属性节点
Text text1 = dom.createTextNode("text_value") ;
Text text2 = dom.createTextNode("text_value") ;
Element demo1 = dom.createElement("local") ;
Element demo2 = dom.createElement("remote") ;
subRoot.appendChild(demo1) ; //创建local节点
demo1.appendChild(text1) ; //*****在这追加了一个文本节点
subRoot.appendChild(demo2) ; //创建remote节点
demo2.appendChild(text2) ; //*****在这追加了一个文本节点
return dom ;
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return null ;
}
/**
* 负责将DOM树文件转换为xml文件
*/
public static void binding(){
TransformerFactory factory = TransformerFactory.newInstance() ;
try {
Transformer former = factory.newTransformer() ;
DOMSource source = new DOMSource(createDOM(dealing(HelloBean.class))) ;
StreamResult result = new StreamResult(new File("/text.xml"));
former.transform(source, result) ;
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
下面是解析代码
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(new File("xmlparse/NewFile.xml"));
// 去掉XML文档中作为格式化内容的空白而映射在DOM树中的不必要的Text Node对象
// dom.normalizeDocument();
Element root = dom.getDocumentElement();
System.out.print("root:" + root.getNodeName());
System.out.print(" root节点存在"
+ root.getAttributes().getLength() + "个子属性");
System.out.println(" root节点存在"
+ root.getChildNodes().getLength() + "个子节点");
Node circleNode = root;
if (circleNode.hasChildNodes()) {
NodeList list = root.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
outInfo(node) ;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void out(Node node) {
System.out.print(" NodeName:" + node.getNodeName());
System.out.print(" NodeValue:" + node.getNodeValue());
System.out.println(" TextContent:" + node.getTextContent());
}
private static void outInfo(Node node){
System.out.println("当前节点名:" + node.getNodeName());
if (node.hasAttributes()) {
NamedNodeMap map = node.getAttributes();
System.out.println(" 当前节点存在" + map.getLength()
+ "个子属性");
}
if (node.hasChildNodes()) {
System.out.println(" 当前节点存在"
+ node.getChildNodes().getLength() + "个子节点");
NodeList nodeList = node.getChildNodes();
for (int j = 0; j < nodeList.getLength(); j++) {
Node demo = nodeList.item(j);
out(demo) ;
}
}
}
现在再来看看对上面xml文件的解析结果
root:enterprise-bean root节点存在0个子属性 root节点存在3个子节点
当前节点名:#text
当前节点名:entity
当前节点存在1个子属性
当前节点存在5个子节点
NodeName:#text
NodeValue: TextContent:
NodeName:local
NodeValue:null TextContent:text_value
NodeName:#text
NodeValue: TextContent:
NodeName:remote
NodeValue:null TextContent:text_value
NodeName:#text
NodeValue: TextContent:
当前节点名:#text
对比上面代码与下面的解析结果,可以看出:
Document创建的对象 | 数量(个) | 包括 | Xml解析 |
Element | 4 | root:enterprise-bean + entity / local / remote | root:enterprise-bean + #text / entity /#text |
Attr | 1 | name="**********" |
|
Text | 2 | text_value text_value | #text local与text_value整合 #text remote与text_value整合 #text |
可以通过下面这条语句过滤掉#text节点
if(node.getNodeType() != Node.TEXT_NODE)
经人指点与测试确认,该#text节点所代表的其实只是一个换行符