使用DOM4j中的命名空间解析XML

时间:2019-07-30 09:52:37

标签: java xml dom4j

我的XML如下:

<?xml version="1.0" encoding="UTF-8"?>
<components version="1.0" xmlns="components.jaxb.config.a.b.c" xmlns:c="configuration.jaxb.config.a.b.c" xmlns:configuration="configuration.jaxb.config.a.b.c">
    <component name="Comp1">
        <description>Des1</description>
        <c:configuration />
        <calculationxml ref="somepath1" />
    </component>

    <component name="Comp2">
        <description>Des2</description>
        <c:configuration />
        <calculationxml ref="somepath2" />
    </component>
</components>

I'm following this tutorial

我面临的问题是我做document.selectNodes(“ / components / component”)时没有空节点

Java:

private List<?> parseComponentXml() throws Exception {

        File inputFile = new File(getComponentPath());
        SAXReader reader = new SAXReader();
        Document document = reader.read( inputFile );

        System.out.println("Root element :" + document.getRootElement().getName());

        Element classElement = document.getRootElement();

        List<Node> nodes = document.selectNodes("/components/component");

        return null;
    }

这里要注意的一件事是,如果我从根节点的 configuration 标记中删除了所有 xmlns和c:,它会起作用。

请建议如何解析名称空间或解决方法。

0 个答案:

没有答案