Java 中的 W3C DOM API,按名称获取子元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2640231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
W3C DOM API in Java, get child elements by name
提问by benstpierre
I just realized that the method Element.getElementsByTagName("someTagName")returns a nodelist of all elements in the document that have a given tagname. What if I just want to get all child elements by tag name?
我刚刚意识到该方法Element.getElementsByTagName("someTagName")返回文档中具有给定标记名的所有元素的节点列表。如果我只想通过标签名称获取所有子元素怎么办?
For example...
例如...
<person>
<name>Bob</name>
<car>
<name>Toyota Corolla</name>
</car>
</person>
采纳答案by Fazal
getElementsByTagNamealways operates in the context of element it is called on. If called on Element, only child elements by the given tag name would be accessed.
I think you are confusing this with Documentobject (org.w3c.dom.Document) getElementsByTagNamemethod, then all elements by the given tag name in the document will be returned.
getElementsByTagName总是在它被调用的元素的上下文中操作。如果调用 on Element,则只会访问给定标签名称的子元素。我认为您将此与Documentobject (org.w3c.dom.Document)getElementsByTagName方法混淆,然后将返回文档中给定标签名称的所有元素。
回答by Syntactic
回答by Jesper Andersen
I had a similar problem. Try to look at the Node class instead:
我有一个类似的问题。尝试查看 Node 类:
http://java.sun.com/javase/6/docs/api/org/w3c/dom/Node.html#getChildNodes()
http://java.sun.com/javase/6/docs/api/org/w3c/dom/Node.html#getChildNodes()
There is a method called "getChildNodes" which returns the list of alldirect child nodes. You then need to filter that list to only get the element-nodes with the right tagname.
有一个名为“getChildNodes”的方法,它返回所有直接子节点的列表。然后,您需要过滤该列表以仅获取具有正确标记名的元素节点。
回答by Eng.Fouad
public static Element getDirectChild(Element parent, String name)
{
for(Node child = parent.getFirstChild(); child != null; child = child.getNextSibling())
{
if(child instanceof Element && name.equals(child.getNodeName())) return (Element) child;
}
return null;
}
回答by Ulises Layera
Had the same problem but none of the answers actually solved the question.
有同样的问题,但没有一个答案真正解决了这个问题。
I was trying to query the operation Nodes INSIDE the portType Node of a WSDL, given that the binding node also have operations.
我试图查询 WSDL 的 portType 节点内的操作节点,因为绑定节点也有操作。
<portType name="MyService">
<operation name="op1">
<input wsam:Action="http://somedomain.org/MyService/MyServiceRequest" message="tns:MyServiceRequest"/>
<output wsam:Action="http://somedomain.org/MyService/MyServiceResponse" message="tns:MyServiceResponse"/>
</operation>
...
</portType>
<binding name="MyServicePortBinding" type="tns:MyService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="op1">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
Solved it by finding the parent (portTypes) and just casting it from Node to Element and using the method named above.
通过找到父级 (portTypes) 并将其从 Node 强制转换为 Element 并使用上面命名的方法来解决它。
Node portType = document.getElementsByTagName("portType").item(0);
NodeList operations = ((Element)portType).getElementsByTagName("operation");
Which gave me as a result the operation elements INSIDE portType Node only.
结果只给了我 INSIDE portType Node 的操作元素。

