我有以下 xml 文件。
<databaseChangeLog>
<changeSet author="debdipta.h (generated)" id="1442927616936-399">
<createTable tableName="SI_FORWARDING_EVENT_HISTORY_AU">
<column name="FORWARDING_ID_O" type="NUMBER"/>
<column name="FORWARDING_ID_N" type="NUMBER"/>
<column name="ASSIGNMENT_ID_O" type="NUMBER"/>
<column name="ASSIGNMENT_ID_N" type="NUMBER"/>
<column name="PROCESS_INSTANCE_ID_O" type="NUMBER"/>
<column name="PROCESS_INSTANCE_ID_N" type="NUMBER"/>
<column name="ORIGINAL_USER_ID_O" type="NUMBER"/>
<column name="ORIGINAL_USER_ID_N" type="NUMBER"/>
<column name="FORWARD_FROM_USER_O" type="NUMBER"/>
<column name="FORWARD_FROM_USER_N" type="NUMBER"/>
<column name="FORWARD_TO_USER_O" type="NUMBER"/>
<column name="FORWARD_TO_USER_N" type="NUMBER"/>
<column name="COMMENTS_O" type="VARCHAR2(4000 CHAR)"/>
<column name="COMMENTS_N" type="VARCHAR2(4000 CHAR)"/>
<column name="XML_O" type="VARCHAR2(4000 CHAR)"/>
<column name="XML_N" type="VARCHAR2(4000 CHAR)"/>
<column name="CREATED_BY_O" type="NUMBER"/>
<column name="CREATED_BY_N" type="NUMBER"/>
<column name="CREATION_DATE_O" type="date"/>
<column name="CREATION_DATE_N" type="date"/>
<column name="AUDIT_TRAIL_REASON" type="VARCHAR2(1000 CHAR)"/>
<column name="TRANSACTION_TYPE" type="VARCHAR2(100 CHAR)"/>
</createTable>
</changeSet>
<changeSet author="deb.h" id="4588999797">
</changeSet>
</databaseChangeLog>
我想显示元素changeSet的子节点,即
createTable
column
column
...
column
但我写的代码并没有向我展示任何内容。
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
任何人都可以告诉我到底我做错了什么。
答案 0 :(得分:0)
我会探索你的节点树,忽略空文本节点。
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));
}
}
}