如何在java x路径中获取带有命名空间的嵌套xml的属性

时间:2017-01-13 06:44:33

标签: java xml xpath

我有一个带有嵌套结构和命名空间的复杂xml。 我能够读取xml元素但无法读取属性。

属性就像我必须从我的xml中读取contentSet或action。

这是我的XML结构

<?xml version="1.0"?>
<env:ContentEnvelope xsi:schemaLocation="http://fundamental.schemas.financial.jso.com/Fundamental/2011-07-07/ 
https://theshare.jso.com/sites/TRM-IA/Content%20Marketplace/Strategic%20Data%20Interfaces/SDI%20Schemas/Schemas/Fundamentals/2015-09-25/FundamentalMaster.xsd"
    xmlns:esg="http://fundamental.schemas.financial.jso.com/ESGSupportingInfo/2011-07-07/"
    xmlns:md="http://data.schemas.financial.jso.com/metadata/2010-10-10/"
    xmlns:cr="http://fundamental.schemas.financial.jso.com/CoraxData/2012-10-25/"

    <env:Header>
        <env:Info>
            <env:Id>urn:uuid:069527ab-2c10-48bb-b3d2-206f4e66e5d2</env:Id>
            <env:TimeStamp>2016-12-23T10:09:09+00:00</env:TimeStamp>
        </env:Info>
        <fun:OrgId>20240</fun:OrgId>
        <fun:PartitionId>1</fun:PartitionId>
    </env:Header>

    <env:Body minVers="0.0" majVers="1" contentSet="Fundamental">
        <env:ContentItem action="Insert">

            <env:Data xsi:type="fun:FundamentalDataItem">
                <fun:Fundamental effectiveTo="9999-12-31T00:00:00+00:00" effectiveFrom="2013-06-29T00:55:15.313+00:00" uniqueFuamentalSet="0054341342">
                    <fun:OrganizationId objectType="Organization" objectTypeId="404510">42565596</fun:OrganizationId>
                    <fun:PrimaryReportingEntityCode>A4C67</fun:PrimaryReportingEntityCode>
                    <fun:TotalPrimaryReportingShares>567923000.00000</fun:TotalPrimaryReportingShares>
                    <fun:LocalLanguageId>505074</fun:LocalLanguageId>
                    <fun:IndustryGroups>
                        <fun:IndustryGroup validTo="9999-12-31T00:00:00+00:00" validFrom="1900-01-01T00:00:00+00:00">
                            <fun:GroupCode>BNK</fun:GroupCode>
                            <fun:GroupName languageId="505074">Bank</fun:GroupName>
                            <fun:TaxonomyId>1</fun:TaxonomyId>
                            <fun:IndustryGroupCodeId>3011649</fun:IndustryGroupCodeId>
                        </fun:IndustryGroup>
                    </fun:IndustryGroups>
                    <fun:GaapCode>CAG</fun:GaapCode>
                    <fun:ConsolidationBasis>Consolidated</fun:ConsolidationBasis>
                    <fun:IsFiling>true</fun:IsFiling>
                    <fun:ConsolidationBasisId>3013598</fun:ConsolidationBasisId>
                    <fun:GaapCodeId>3011536</fun:GaapCodeId>
                    <fun:Taxonomies>
                        <fun:Taxonomy>1</fun:Taxonomy>
                    </fun:Taxonomies>
                    <fun:WorldScopeIds>
                        <fun:WorldScopeId validTo="9999-12-31T00:00:00+00:00" validFrom="2012-03-31T00:00:00+00:00">C12436390</fun:WorldScopeId>
                    </fun:WorldScopeIds>
                </fun:Fundamental>
            </env:Data>
        </env:ContentItem>

这是我读取的java sxpression。

    FileInputStream file = new FileInputStream(new File("c://temp/Fun.xml"));
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder =  builderFactory.newDocumentBuilder();
    Document xmlDocument = builder.parse(file);

    XPath xPath =  XPathFactory.newInstance().newXPath();

    System.out.println("*************************");
    String expression = "/ContentEnvelope/Body[@minVers='0.0']/contentSet";
    System.out.println(expression);

1 个答案:

答案 0 :(得分:1)

使用@attribute_name语法引用XPath中的属性,就像使用@minVers一样:

/ContentEnvelope/Body[@minVers='0.0']/@contentSet