0

我尝试使用 dom4j 访问 xml 文件。我也尝试过 org.w3c 的 dom,但这以同样的方式失败了。我有一个 xml 文件的示例,我尝试在下面阅读。我尝试从元素 loc 读取属性 xlink:href 但由于某种原因这总是失败。当我在自己编写的简单 xml 文件上尝试相同的方法时,它确实有效。我已经为此工作了好几天了。这是我的方法:

 File file = new File("schemas/pfs-2013-04-01-presentation.xml");
            SAXReader reader = new SAXReader();
            Document document = reader.read(file);
            XPath xpath = document.createXPath("//loc");
            Map uris = new HashMap();
            uris.put("", "http://www.xbrl.org/2003/linkbase");
            uris.put("xbrli","http://www.xbrl.org/2003/instance");
            uris.put("xlink","http://www.w3.org/1999/xlink");
            uris.put("xsi","http://www.w3.org/2001/XMLSchema-instance");

            xpath.setNamespaceURIs(uris);

            List<Node> nodes =  xpath.selectNodes(xpath);

我想稍后从这些“节点”中读取属性。但是,当我执行此操作时,列表为空。这是不明白的事情。

任何人都可以帮我解决这个问题吗?谢谢

<?xml version="1.0" encoding="UTF-8"?>
<linkbase xmlns="http://www.xbrl.org/2003/linkbase" xmlns:xbrli="http://www.xbrl.org/2003/instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.xbrl.org/2003/linkbase http://www.xbrl.org/2003/xbrl-linkbase-2003-12-31.xsd" xmlns:presentationAttribute="http://www.nbb.be/be/fr/pfs/presentationAttribute" >
    <roleRef roleURI="http://www.nbb.be/be/fr/pfs/ci/role/FullIdentifyingData" xlink:type="simple" xlink:href="pfs-full-2013-04-01.xsd#FullIdentifyingData"/>
    <roleRef roleURI="http://www.nbb.be/be/fr/pfs/ci/role/FullBalanceSheet" xlink:type="simple" xlink:href="pfs-full-2013-04-01.xsd#FullBalanceSheet"/>
    <roleRef roleURI="http://www.nbb.be/be/fr/pfs/ci/role/FullIncomeStatement" xlink:type="simple" xlink:href="pfs-full-2013-04-01.xsd#FullIncomeStatement"/>
    <roleRef roleURI="http://www.nbb.be/be/fr/pfs/ci/role/FullAppropriationsWithdrawings" xlink:type="simple" xlink:href="pfs-full-2013-04-01.xsd#FullAppropriationsWithdrawings"/>
    <roleRef roleURI="http://www.nbb.be/be/fr/pfs/ci/role/FullDisclosures" xlink:type="simple" xlink:href="pfs-full-2013-04-01.xsd#FullDisclosures"/>
    <roleRef roleURI="http://www.nbb.be/be/fr/pfs/ci/role/FullSocialBalanceSheet" xlink:type="simple" xlink:href="pfs-full-2013-04-01.xsd#FullSocialBalanceSheet"/>
    <roleRef roleURI="http://www.nbb.be/be/fr/pfs/ci/role/FullValidationRules" xlink:type="simple" xlink:href="pfs-full-2013-04-01.xsd#FullValidationRules"/>
    <roleRef roleURI="http://www.nbb.be/be/fr/pfs/ci/role/FullManagementReport" xlink:type="simple" xlink:href="pfs-full-2013-04-01.xsd#FullManagementReport"/>
    <roleRef roleURI="http://www.nbb.be/be/fr/pfs/ci/role/FullAccountantsReport" xlink:type="simple" xlink:href="pfs-full-2013-04-01.xsd#FullAccountantsReport"/>
<presentationLink xlink:type="extended" xlink:role="http://www.nbb.be/be/fr/pfs/ci/role/FullIdentifyingData">
    <loc xlink:type="locator" xlink:href="pfs-2013-04-01.xsd#pfs_IdentifyingData" xlink:label="IdentifyingData_1" />
    <loc xlink:type="locator" xlink:href="pfs-gcd-2013-04-01.xsd#pfs-gcd_GlobalCommonDocument" xlink:label="GlobalCommonDocument_2" />
    <loc xlink:type="locator" xlink:href="pfs-gcd-2013-04-01.xsd#pfs-gcd_EntityInformation" xlink:label="EntityInformation_3" />
    <loc xlink:type="locator" xlink:href="pfs-gcd-2013-04-01.xsd#pfs-gcd_EntityName" xlink:label="EntityName_4" />
    <loc xlink:type="locator" xlink:href="pfs-gcd-2013-04-01.xsd#pfs-gcd_EntityCurrentLegalName" xlink:label="EntityCurrentLegalName_5" />
4

1 回答 1

0

由于您没有使用默认命名空间,xpath 不知道如何定位您的“loc”节点。您需要为“loc”节点添加前缀并将正确的命名空间更新为相同的前缀。有关更多信息,请参阅:XPath 和默认命名空间处理

于 2013-09-27T14:29:04.923 回答