我正在尝试通过遍历子节点列表来读取Collada XML文件,但每隔一个输出都会读取#text。这是怎么回事?我的代码:
public void runTest() {
File file = new File( "test.dae" );
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
}
catch( ParserConfigurationException error ) {
System.out.println( "--ParserConfigurationException: " + error.getMessage() ); return;
}
Document document = null;
try {
document = builder.parse( file );
}
catch( IOException error ) {
System.out.println( "--IOException: " + error.getMessage() ); return;
}
catch( SAXException error ) {
System.out.println( "--SAXException: " + error.getMessage() ); return;
}
Node node = document.getDocumentElement();
String node_name = node.getNodeName();
System.out.println( node_name );
NodeList node_list = node.getChildNodes();
for( int iterator = 0; iterator < node_list.getLength(); iterator++ ) {
Node child_node = node_list.item( iterator );
String child_node_name = child_node.getNodeName();
System.out.println( "-- " + child_node_name );
}
}
Element
接口的getElementsByTagName
方法。您可以使用getElementsByTagName("*")
来获取给定元素的元素子级。如果您正在浏览NodeList,则需要进行一些转换为Element
,但这是值得的。 - fakedad