Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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 使用DOM4J在XML中迭代具有名称空间前缀的项_Java_Xml_Parsing_Dom4j - Fatal编程技术网

Java 使用DOM4J在XML中迭代具有名称空间前缀的项

Java 使用DOM4J在XML中迭代具有名称空间前缀的项,java,xml,parsing,dom4j,Java,Xml,Parsing,Dom4j,问题是名称空间前缀。下面是一个示例xml代码 <rss version="2.0"> <channel> <item> <ed:filing xmlns:ed="http://www.ed.com"> <ed:name>ABC</ed:name> <ed:files>

问题是名称空间前缀。下面是一个示例xml代码

<rss version="2.0">
    <channel>        
        <item>
            <ed:filing xmlns:ed="http://www.ed.com">
                <ed:name>ABC</ed:name>
                <ed:files>
                    <ed:file ed:id="1" ed:file="abc.htm" />
                    <ed:file ed:id="2" ed:file="abc.zip" />
                </ed:files>
            </ed:filing>
        </item>
        <item>
            <ed:filing xmlns:ed="http://www.ed.com">
                <ed:name>CDF</ed:name>
                <ed:files>
                    <ed:file ed:id="1" ed:file="cdf.htm" />
                    <ed:file ed:id="2" ed:file="cdf.zip" />
                </ed:files>
            </ed:filing>
        </item>   
    </channel>
</rss>
这是我的Java代码

SAXReader reader = new SAXReader();
Document document = reader.read( inputFile );           
List<Node> nodes = document.selectNodes("//rss/channel/item");

for (Node node : nodes) {
    ??? How can I access "ed:name" and "ed:file" ???
}
SAXReader=newsaxreader();
文档=reader.read(inputFile);
List nodes=document.selectNodes(“//rss/channel/item”);
用于(节点:节点){
我如何访问“ed:name”和“ed:file”???
}

有几个选项,但这里有一个使用XPath和命名空间映射的选项:

    Map<String, String> nc = new HashMap<String, String>() {
        {
            put("ed", "http://www.ed.com");
        }
    };

    System.out.printf("Name\tFile 1\tFile 2\n");
    for (Node node : nodes) {
        XPath xp = document.createXPath(".//ed:name");
        xp.setNamespaceURIs(nc);
        String name = xp.selectSingleNode(node).getText();

        xp = document.createXPath(".//ed:file[@ed:id='1']/@ed:file");
        xp.setNamespaceURIs(nc);
        String f1 = xp.selectSingleNode(node).getText();

        xp = document.createXPath(".//ed:file[@ed:id='2']/@ed:file");
        xp.setNamespaceURIs(nc);
        String f2 = xp.selectSingleNode(node).getText();

        System.out.printf("%s\t%s\t%s\n", name, f1, f2);
    }
Map nc=new HashMap(){
{
付诸表决http://www.ed.com");
}
};
System.out.printf(“名称\t文件1\t文件2\n”);
用于(节点:节点){
XPathXP=document.createXPath(“../ed:name”);
xp.SetNamespaceURI(nc);
String name=xp.selectSingleNode(node.getText();
xp=document.createXPath(“../ed:file[@ed:id='1']/@ed:file”);
xp.SetNamespaceURI(nc);
字符串f1=xp.selectSingleNode(node.getText();
xp=document.createXPath(“../ed:file[@ed:id='2']/@ed:file”);
xp.SetNamespaceURI(nc);
字符串f2=xp.selectSingleNode(node.getText();
System.out.printf(“%s\t%s\t%s\n”,名称,f1,f2);
}
有点恼人的是,您不能像在
javax.xml.XPath
中那样重用XPath实例

    Map<String, String> nc = new HashMap<String, String>() {
        {
            put("ed", "http://www.ed.com");
        }
    };

    System.out.printf("Name\tFile 1\tFile 2\n");
    for (Node node : nodes) {
        XPath xp = document.createXPath(".//ed:name");
        xp.setNamespaceURIs(nc);
        String name = xp.selectSingleNode(node).getText();

        xp = document.createXPath(".//ed:file[@ed:id='1']/@ed:file");
        xp.setNamespaceURIs(nc);
        String f1 = xp.selectSingleNode(node).getText();

        xp = document.createXPath(".//ed:file[@ed:id='2']/@ed:file");
        xp.setNamespaceURIs(nc);
        String f2 = xp.selectSingleNode(node).getText();

        System.out.printf("%s\t%s\t%s\n", name, f1, f2);
    }