2013-09-27 197 views
0

我嘗試使用dom4j訪問xml文件。我也嘗試過org.w3c的dom,但是這種方式失敗了。 我有一個xml文件的示例,我嘗試在下面閱讀。我嘗試從元素loc讀取屬性xlink:href,但由於某些原因,這總是失敗。當我在一個簡單的xml文件上嘗試相同的方法時,我自己寫了它,它確實有效。我已經爲此工作了好幾天了。這裏是我的方法:XPath搜索不起作用

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" /> 
+0

您確定可以設置空白前綴嗎?在XPath中,它表示「無URI」名稱空間,任何其他名稱空間都必須有一個前綴。 – choroba

回答

0

既然你不使用默認的命名空間,XPath不知道如何定位你的「祿」的節點。您需要爲「loc」節點加前綴,並更新適當的名稱空間以使用相同的前綴。有關更多信息,請參閱此處:XPath and Default Namespace handling