首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用xPathAPI解析xml时出现的问题

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

Stack Overflow用户
提问于 2014-01-20 11:51:06
回答 1查看 226关注 0票数 0

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

代码语言:javascript
运行
复制
<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文档的代码如下所示

代码语言:javascript
运行
复制
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);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-21 04:07:11

试试这个。

代码语言:javascript
运行
复制
        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());
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21233308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档