DOMが迂遠2

今までまじめにforループを回していたのだが、XMLの構造が確実に分かっている時には

node.getFirstChild().getChildNodes().item(2).getFirstChild();

みたいに書くという荒技もあるのだなぁと思った。


とはいえ、「何番目にあるかが分からない」事はよくあるのだが。

private static Node findChildElement(Node Parent, String ChildName){
	if(Parent.hasChildNodes() == false) return null;
	NodeList tmpList = Parent.getChildNodes();
	for(int loop=0;loop < tmpList.getLength();loop++){
		if(tmpList.item(loop).getNodeName() == ChildName){
			return tmpList.item(loop);
		}
	}
	return null;
}

みたいな関数を書いてしまうのがしっくりくるか。