0

in the xml file , there are nodes with same name in different levels. How can i distinguish based on the leve or parent

<account email="[email protected]" >

<contacts>

    <contact>
        <id>
        0
        </id>
    </contact>
    <contact>
        <id>
         1
        </id>
    </contact>
    <contact>
        <id>
         2
        </id>
    </contact>
</contacts>

<groups>

    <group>

        <groupname>
         xyz

        </groupname>

        <members>

            <contact>

                <id>
                 7

                </id>
            </contact>

            <contact>

                <id>
                 8
                </id>
            </contact>
        </members>
    </group>
</groups>

here i have contact under contacts as well as under groups ->group->memebers I need to distinguish between them if i try

doc.getElementsByTagName("contact");

it will return all the contact nodes in all levels. This is not what is required. I need to contact unders contacts separate and under memebers separate.

Please help.

1 Answer 1

1

using DOM XML technology you can extract NodeSet by mean of XPATHExpression:

   String path = "//account/groups/group/members/contact";
   XPath xpath = XPathFactory.newInstance().newXPath();
    XPathExpression expr1;
    try {
        expr1 = xpath.compile(path);
        return (NodeList)expr1.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }

this will return only contact inside groups

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.