0

I have a xml document like this

<?xml version="1.0"?>
<Employees>
    <Employee emplid="1111" type="admin">
        <firstname>John</firstname>
        <lastname>Watson</lastname>
        <age>30</age>
        <email>[email protected]</email>
        <Employee emplid="2222" type="Manager">
            <firstname>Sherlock</firstname>
            <lastname>Homes</lastname>
            <age>32</age>
            <email>[email protected]</email>
            <city>
                <name>ger</name>
            </city>
            <Employee emplid="3333" type="Clerk">
                <firstname>Tom</firstname>
                <lastname>Irish</lastname>
                <age>32</age>
                <email>[email protected]</email>
                <city>
                    <name>tok</name>
                </city>
                <Employee emplid="3333" type="Staff">
                    <firstname>Jerking</firstname>
                    <lastname>rash</lastname>
                    <age>32</age>
                    <email>[email protected]</email>
                    <city>
                        <name>rus</name>
                    </city>
                </Employee>
            </Employee>
        </Employee>
        <city>
            <name>cal</name>
        </city>
    </Employee>
</Employees>

I would like to get the output like below

empid = admin
firstname = john
last name = watson
age = 30
city name = cal

empid = manager
firstname = sherlock
last name = homes
age = 32
city name = ger

empid = clerk
firstname = tom
last name = irish
age = 32
city name = tok

empid = staff
firstname = jerking
last name = rash
age = 30
city name = rus

I have tried but not getting the values in correct order..

So far i tried

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = Factory.newDocumentBuilder();
    FileInputStream file = new FileInputStream(new File("D:/new11.xml"));
    Document doc = builder.parse(file);

    //creating an XPathFactory:
    XPathFactory factory = XPathFactory.newInstance();
    //using this factory to create an XPath object: 
    XPath xpath = factory.newXPath();

    // XPath Query for showing all nodes valuetext()
    XPathExpression expr = xpath.compile("//" + "Employee" + "/*");
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    System.out.println(nodes.getLength());
    for (int i = 0; i < nodes.getLength(); i++) {

        Element el = (Element) nodes.item(i);

        System.out.println("tag: " + el.getNodeName());
        // seach for the Text children
        if (el.getFirstChild().getNodeType() == Node.TEXT_NODE){

             System.out.println("inner value:" + el.getFirstChild().getNodeValue());
            //System.out.println("inner value:" + el.getNodeName());
        }


        NodeList children = el.getChildNodes();
        for (int k = 0; k < children.getLength(); k++) {
            Node child = children.item(k);
            if (child.getNodeType() != Node.TEXT_NODE) {
                System.out.println("child tag: " + child.getNodeName());
                if (child.getFirstChild().getNodeType() == Node.TEXT_NODE)
                    System.out.println("inner child value:" + child.getFirstChild().getNodeValue());;
            }
        }
    }
}
4
  • Are you familiar with how to use XPath from within Java? I'm not sure whether you're asking what the appropriate XPath expression is, or asking how to use XPath expressions in a Java program. Commented Mar 7, 2014 at 7:20
  • i need to use xpath expression in java.. Commented Mar 7, 2014 at 7:32
  • Did you look at stackoverflow.com/questions/340787/… ? Commented Mar 7, 2014 at 7:43
  • Can you show the code you've tried so far? Commented Mar 7, 2014 at 8:01

2 Answers 2

1

Your XPath for a typical recursion of child elements would be:

  • to choose any child of a child of a child etc....

    descendant-or-self::*

  • to choose a specific node name

    descendant-or-self::nodeName

e.g. descendant-or-self::foo will select all descendants of the current context with the name foo.

to put this into a recursive loop you would use the following:

<xsl:for-each select="document/node">
    <xsl:for-each select="descendant-or-self::*>
        ...
    </xsl:for-each>
</xsl:for-each>

For more info: http://www.stylusstudio.com/xsllist/200310/post30960.html

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

Comments

1

You were iterating over the children of all employees, rather than the employees themselves. In addition you had a strange mix of XPath and DOM like operations. For the purpose of code simplicity I would recommend sticking to XPath. I believe this is what you were looking for:

public static void main(String[] args) throws Exception {
        DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = Factory.newDocumentBuilder();

        InputStream is = null;
        try {
            is = new FileInputStream(new File("D:/new11.xml"));

            Document doc = builder.parse(is);

            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();

            XPathExpression xpEmployee = xpath.compile("//Employee");
            XPathExpression xpEmpType = xpath.compile("@type");
            XPathExpression xpFirstName = xpath.compile("firstname");
            XPathExpression xpLastName = xpath.compile("lastname");
            XPathExpression xpAge = xpath.compile("age");
            XPathExpression xpCityName = xpath.compile("city/name");

            NodeList employees = (NodeList)xpEmployee.evaluate(doc, XPathConstants.NODESET);
            for(int i = 0; i < employees.getLength(); i++) {
                Element employee = (Element)employees.item(i);

                System.out.println("emptype = " + xpEmpType.evaluate(employee, XPathConstants.STRING));
                System.out.println("firstname = " + xpFirstName.evaluate(employee, XPathConstants.STRING));
                System.out.println("last name = " + xpLastName.evaluate(employee, XPathConstants.STRING));
                System.out.println("age = " + xpAge.evaluate(employee, XPathConstants.STRING));
                System.out.println("city name = " + xpCityName.evaluate(employee, XPathConstants.STRING));
                System.out.println("");
            }
        } finally {
            if(is != null) {
                is.close();
            }
        }
    }

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.