Xpath以递归方式获取值

时间:2014-03-07 07:04:22

标签: java xml xpath

我有一个像这样的xml文件

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

我想获得如下输出

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

我已尝试但未按正确顺序获取值。

到目前为止,我试过

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

2 个答案:

答案 0 :(得分:1)

对于子元素的典型递归,您的XPath将是:

  • 选择孩子的孩子等......

    后代或自身:: *

  • 选择特定节点名称

    后代或自身::节点名称

e.g。 descendant-or-self::foo将选择当前的所有后代 名为foo的上下文。

将它放入递归循环中,您将使用以下内容:

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

了解更多信息:http://www.stylusstudio.com/xsllist/200310/post30960.html

答案 1 :(得分:1)

您正在迭代所有员工的子女,而不是员工本身。此外,你有一个奇怪的混合XPath和DOM之类的操作。为了简化代码,我建议坚持使用XPath。我相信这就是你要找的东西:

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