我有这样的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());;
            }
        }
    }
}

最佳答案

典型的子元素递归的XPath为:


选择一个孩子中的一个孩子等等...

后代或自己:: *
选择特定的节点名称

后代或自身:: nodeName


例如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

10-07 13:12
查看更多