benstpierre
benstpierre

Reputation: 33581

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. 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>

Upvotes: 28

Views: 58970

Answers (5)

Ulises Layera
Ulises Layera

Reputation: 884

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.

<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.

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.

Upvotes: 7

Eng.Fouad
Eng.Fouad

Reputation: 117587

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;
}

Upvotes: 31

Fazal
Fazal

Reputation: 3051

getElementsByTagName always 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 Document object (org.w3c.dom.Document) getElementsByTagName method, then all elements by the given tag name in the document will be returned.

Upvotes: 3

Jesper Andersen
Jesper Andersen

Reputation: 49

I had a similar problem. Try to look at the Node class instead:

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. You then need to filter that list to only get the element-nodes with the right tagname.

Upvotes: 4

Syntactic
Syntactic

Reputation: 10961

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. See here.

Upvotes: 0

Related Questions