Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用xPathAPI解析xml时出现的问题_Java_Xml_Parsing - Fatal编程技术网

Java 使用xPathAPI解析xml时出现的问题

Java 使用xPathAPI解析xml时出现的问题,java,xml,parsing,Java,Xml,Parsing,我试图解析以下xml,但每次迭代都会重复第一个子标记[],而不是获取下一个子标记的值[e.e]。请提供您的帮助 <error_code_rules enabled="true"> <errors> <error> <error_source>ldap</error_source> <error_code>ALL</error_code>

我试图解析以下xml,但每次迭代都会重复第一个子标记[],而不是获取下一个子标记的值[e.e]。请提供您的帮助

<error_code_rules enabled="true">
    <errors>
        <error>
            <error_source>ldap</error_source>
            <error_code>ALL</error_code>
            <oms_error_code>OMS-1</oms_error_code>
            <priority>3</priority>
        </error>
        <error>
            <error_source>nagravision</error_source>
            <error_code>ALL</error_code>
            <oms_error_code>OMS-2</oms_error_code>
            <priority>1</priority>
        </error>
        <error>
            <error_source>hitexpress</error_source>
            <error_code>ALL</error_code>
            <oms_error_code>OMS-3</oms_error_code>
            <priority>2</priority>
        </error>
        <error>
            <error_source>netinventory</error_source>
            <error_code>ALL</error_code>
            <oms_error_code>OMS-4</oms_error_code>
            <priority>2</priority>
        </error>
        <error>
            <error_source>seachangeeventis</error_source>
            <error_code>ALL</error_code>
            <oms_error_code>OMS-5</oms_error_code>
            <priority>2</priority>
        </error>
        <error>
            <error_source>embratel</error_source>
            <error_code>ALL</error_code>
            <oms_error_code>OMS-6</oms_error_code>
            <priority>2</priority>
        </error>
        <error>
            <error_source>siemens</error_source>
            <error_code>ALL</error_code>
            <oms_error_code>OMS-7</oms_error_code>
            <priority>2</priority>
        </error>
        <error>
            <error_source>netsiemens</error_source>
            <error_code>ALL</error_code>
            <oms_error_code>OMS-8</oms_error_code>
            <priority>2</priority>
        </error>
        <error>
            <error_source>nokiaonends</error_source>
            <error_code>ALL</error_code>
            <oms_error_code>OMS-9</oms_error_code>
            <priority>2</priority>
        </error>
    </errors>
</error_code_rules>
解析xml文档的代码如下

private void populateKsuRulesList() {
    logger.trace(3, "Populating ksuRulesList");
    logger.log("populter ksu rule list");
    CachedXPathAPI xPathAPI = new CachedXPathAPI();
    try {
        NodeList components = xPathAPI.eval(doc, KSU_RULES_COMPONENT_PATH)
                .nodelist();
        logger.log("components.getLength()" + components.getLength());
        for (int i = 0; i < components.getLength(); i++) {
            Node component = components.item(i);
            // NodeIterator nodeItr = xPathAPI.selectNodeIterator(component,
            // KSU_RULES_COMPONENT_CATEGORY_PATH);
            String componentCategory = xPathAPI.eval(components.item(i),
                    KSU_RULES_COMPONENT_CATEGORY_PATH).toString();
            String componentType = xPathAPI.eval(component,
                    KSU_RULES_COMPONENT_TYPE_PATH).toString();
            String mandatory = xPathAPI.eval(component,
                    KSU_RULES_MANDATORY_PATH).toString();
            logger.log("componentCategory" + componentCategory);
            logger.log("componentType" + componentType);
            logger.log("mandatory" + mandatory);
            String ruleParms[] = { componentCategory, componentType,
                    mandatory };
            ksuRulesList.add(ruleParms);
        }
    } catch (TransformerException te) {
        logger.log("Exception: ", te);
    }
}
试试这个

        NodeList components = xPathAPI.eval(doc, "//errors/error").nodelist();
        System.out.println("components.getLength()" + components.getLength());
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        for (int i = 0; i < components.getLength(); i++) {
            Node component = components.item(i);
            Element product = (Element) component;
            NodeList nodes = (NodeList) xpath.compile("error_source").evaluate(product, XPathConstants.NODESET);
            System.out.println(nodes.item(0).getTextContent());
        }

您忘了向我们显示XPath字符串。