Search code examples
javaxmldom4j

dom4j - select node by 2 conditions


I want to select node element by 2 conditions. For example:

<bbb>
    <aaa name="param1">val1</aaa>
    <aaa name="param2">val2</aaa>
    <aaa name="param3">val3</aaa>
</bbb>

I want to get element aaa, with attribute name = "param1". What's the best way to do that?


Solution

  • Assuming you've already parsed the document with Dom4j, then...

    Document doc = ...
    Node node = doc.selectSingleNode("//aaa[@name='param1']");
    

    ... should work. The node variable should contain your desired element.