Java 如何提取create表的子节点
我有以下xml文件 但是我写的代码没有向我展示任何东西Java 如何提取create表的子节点,java,xml,dom,Java,Xml,Dom,我有以下xml文件 但是我写的代码没有向我展示任何东西 p ackage com.xml.diff; import java.io.FileInputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Ele
p
ackage com.xml.diff;
import java.io.FileInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class isolateXML {
public static void main(String[] args)throws Exception{
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document dom=db.parse(new FileInputStream("E:/1.xml"));
Element ele=dom.getDocumentElement();
ele.normalize();
NodeList list=ele.getElementsByTagName("changeSet");
for(int i=0;i<list.getLength();i++){
Node r=list.item(i);
NodeList rs=r.getChildNodes();
for(int j=0;j<rs.getLength();j++){
Node k=rs.item(j);
System.out.println(k.getNodeName());
}
}
}
}
谁能告诉我我到底做错了什么。我会浏览你的节点树,忽略空文本节点
public static void exploreNode(Node node) {
//Ignore empty text node
if (node instanceof Text) {
String value = node.getNodeValue().trim();
if (value.equals("")) {
return;
}
}
System.out.println(node.getNodeName());
if (node.hasChildNodes()) {
NodeList childrens = node.getChildNodes();
for (int i = 0; i < childrens.getLength(); i++) {
exploreNode(childrens.item(i));
}
}
}
公共静态void exploreNode(节点){
//忽略空文本节点
if(文本的节点实例){
字符串值=node.getNodeValue().trim();
if(值等于(“”){
回来
}
}
System.out.println(node.getNodeName());
if(node.hasChildNodes()){
NodeList childrens=node.getChildNodes();
for(int i=0;i
请使用调试器查看ele
、list
和r
的内容。请尝试。但元素变更集的子节点数仅显示为2。我不明白它为什么显示这个。有例外。它无法继续循环。在某个地方它得到的值为空。我无法确切地理解在哪里。以前的代码的确切问题是什么?它在哪里得到空文本节点?这是因为node.getNodeValue()
在列节点上返回空值。
p
ackage com.xml.diff;
import java.io.FileInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class isolateXML {
public static void main(String[] args)throws Exception{
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document dom=db.parse(new FileInputStream("E:/1.xml"));
Element ele=dom.getDocumentElement();
ele.normalize();
NodeList list=ele.getElementsByTagName("changeSet");
for(int i=0;i<list.getLength();i++){
Node r=list.item(i);
NodeList rs=r.getChildNodes();
for(int j=0;j<rs.getLength();j++){
Node k=rs.item(j);
System.out.println(k.getNodeName());
}
}
}
}
#text
createTable
#text
#text
public static void exploreNode(Node node) {
//Ignore empty text node
if (node instanceof Text) {
String value = node.getNodeValue().trim();
if (value.equals("")) {
return;
}
}
System.out.println(node.getNodeName());
if (node.hasChildNodes()) {
NodeList childrens = node.getChildNodes();
for (int i = 0; i < childrens.getLength(); i++) {
exploreNode(childrens.item(i));
}
}
}