3
私は、子ノードのリストを反復してCollada XMLファイルを読み込もうとしており、他のすべての出力は#textを読み取ります。何のことですか?私のコード:XMLとDOMの#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);
}
}
Hmm。私は、テキストノードにテキストがあることを期待しています。私はちょうどgetNodeName()がノード名を取得する以外の何かをするとは思わなかった。ここでは、getNodeType()などの月明かりのように思えます。とにかく、私はなぜ疑問に思うことはない、おそらく良い理由があるだろう。現在のところ、if(node_name == "#text")のようなコードを追加するだけです。私が望むものを得るために。 – Espen
今後参考になるように、要素を取得するより良い方法があります。 'Element'インターフェースの' getElementsByTagName'メソッドをチェックしてください。 'getElementsByTagName(" * ")'を使って、指定された要素の子要素だけを取得することができます。あなたがNodeListを見ているなら、それは 'Element'へのキャストが必要ですが、それはそれだけの価値があります。 – fakedad