简体   繁体   English

Java中的W3C DOM API,按名称获取子元素

[英]W3C DOM API in Java, get child elements by name

I just realized that the method Element.getElementsByTagName("someTagName") returns a nodelist of all elements in the document that have a given tagname. 我刚刚意识到方法Element.getElementsByTagName("someTagName")返回文档中具有给定标记名的所有元素的节点列表。 What if I just want to get all child elements by tag name? 如果我只想通过标签名称获取所有子元素怎么办?

For example... 例如...

<person>
  <name>Bob</name>
  <car>
    <name>Toyota Corolla</name>
  </car>
</person>
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;
}

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. 我试图查询操作节点INSIDE的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的操作元素。

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 all direct child nodes. 有一个名为“getChildNodes”的方法,它返回所有直接子节点的列表。 You then need to filter that list to only get the element-nodes with the right tagname. 然后,您需要过滤该列表以仅获取具有正确标记名的元素节点。

Not all elements in the document — all descendant elements of the element it's called on. 并非文档中的所有元素 - 它被调用的元素的所有后代元素。 It sounds like that's what you want. 听起来这就是你想要的。 You just need to be calling it on the right Element . 你只需要在正确的Element上调用它。 See here . 看到这里

getElementsByTagName always operates in the context of element it is called on. getElementsByTagName始终在调用它的元素的上下文中操作。 If called on Element , only child elements by the given tag name would be accessed. 如果在Element调用,则只能访问给定标记名称的子元素。 I think you are confusing this with Document object (org.w3c.dom.Document) getElementsByTagName method, then all elements by the given tag name in the document will be returned. 我认为你将它与Document对象(org.w3c.dom.Document) getElementsByTagName方法混淆,然后将返回文档中给定标记名称的所有元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM