我写了下面的代码,但只能返回根下面的一层,如果指定的
Element在更深的层,就找不到了,怎么样才能返回深层的Element
谢谢
//elementName是要查找的element
public static Element getElement(Document doc,String elementName) throws
Exception {
Element childElement = null;
//判断传进来的参数不能为null
if(null == doc || null == elementName){
throw new Exception("doc or elmentName is unexpected null");
}
Element root = doc.getDocumentElement();
NodeList children = root.getChildNodes();
int length = children.getLength();
for(int i = 0; i < length; i++){
Node child = children.item(i);
if(child instanceof Element){
childElement = (Element) child;
//匹配得到相应TagName的元素
if (childElement.getTagName().equalsIgnoreCase(elementName)) {
return childElement;
}
}
}
return null; //找不到要查找的Element
}
Element在更深的层,就找不到了,怎么样才能返回深层的Element
谢谢
//elementName是要查找的element
public static Element getElement(Document doc,String elementName) throws
Exception {
Element childElement = null;
//判断传进来的参数不能为null
if(null == doc || null == elementName){
throw new Exception("doc or elmentName is unexpected null");
}
Element root = doc.getDocumentElement();
NodeList children = root.getChildNodes();
int length = children.getLength();
for(int i = 0; i < length; i++){
Node child = children.item(i);
if(child instanceof Element){
childElement = (Element) child;
//匹配得到相应TagName的元素
if (childElement.getTagName().equalsIgnoreCase(elementName)) {
return childElement;
}
}
}
return null; //找不到要查找的Element
}
Returns a NodeList of all descendant Elements with a given tag name, in document order.
我想返回指定name的Element,而不是他的后代
org-->w3c-->dom-->Document.getElementsByTagName(String name)
* Iterative tree-walker. When you have a Parent link, there's often no
* need to resort to recursion. NOTE THAT only Element nodes are matched
* since we're specifically supporting getElementsByTagName().
*/
protected Node nextMatchingElementAfter(Node current) { Node next;
while (current != null) {
// Look down to first child.
if (current.hasChildNodes()) {
current = (current.getFirstChild());
} // Look right to sibling (but not from root!)
else if (current != rootNode && null != (next = current.getNextSibling())) {
current = next;
} // Look up and right (but not past root!)
else {
next = null;
for (; current != rootNode; // Stop when we return to starting point
current = current.getParentNode()) { next = current.getNextSibling();
if (next != null)
break;
}
current = next;
} // Have we found an Element with the right tagName?
// ("*" matches anything.)
if (current != rootNode
&& current != null
&& current.getNodeType() == Node.ELEMENT_NODE) {
if (!enableNS) {
if (tagName.equals("*") ||
((ElementImpl) current).getTagName().equals(tagName))
{
return current;
}
} else {
// DOM2: Namespace logic.
if (tagName.equals("*")) {
if (nsName != null && nsName.equals("*")) {
return current;
} else {
ElementImpl el = (ElementImpl) current;
if ((nsName == null
&& el.getNamespaceURI() == null)
|| (nsName != null
&& nsName.equals(el.getNamespaceURI())))
{
return current;
}
}
} else {
ElementImpl el = (ElementImpl) current;
if (el.getLocalName() != null
&& el.getLocalName().equals(tagName)) {
if (nsName != null && nsName.equals("*")) {
return current;
} else {
if ((nsName == null
&& el.getNamespaceURI() == null)
|| (nsName != null &&
nsName.equals(el.getNamespaceURI())))
{
return current;
}
}
}
}
}
} // Otherwise continue walking the tree
} // Fell out of tree-walk; no more instances found
return null; } // nextMatchingEl