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>johnwatson@sh.com</email>
        <Employee emplid="2222" type="Manager">
            <firstname>Sherlock</firstname>
            <lastname>Homes</lastname>
            <age>32</age>
            <email>sherlock@sh.com</email>
            <city>
                <name>ger</name>
            </city>
            <Employee emplid="3333" type="Clerk">
                <firstname>Tom</firstname>
                <lastname>Irish</lastname>
                <age>32</age>
                <email>tom@sh.com</email>
                <city>
                    <name>tok</name>
                </city>
                <Employee emplid="3333" type="Staff">
                    <firstname>Jerking</firstname>
                    <lastname>rash</lastname>
                    <age>32</age>
                    <email>jer@sh.com</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());;
            }
        }
    }
}
Sundar G
  • 1,069
  • 1
  • 11
  • 29

2 Answers2

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

Elfentech
  • 747
  • 5
  • 10
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();
            }
        }
    }
jpmc26
  • 28,463
  • 14
  • 94
  • 146
adamretter
  • 3,885
  • 2
  • 23
  • 43