2017-01-12 62 views
0

我是新來的xml解析不能決定如何解析java中的這個複雜的xml文件。 我能解析簡單的xml文件,但是當涉及到複雜的xml文件時,我感到困惑。無法使用java讀取xml的元素。 這是我的示例xml文件。如何在JAVA中解析複雜的嵌套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/" 
    xmlns:ful="http://fundamental.schemas.financial.jso.com/FundamentalLineItem/2011-07-07/" 
    xmlns:fun="http://fundamental.schemas.financial.jso.com/Fundamental/2011-07-07/" 
    xmlns:ir="http://fundamental.schemas.financial.jso.com/FinancialInstrumentRelationship/2011-07-07/" 
    xmlns:fl="http://fundamental.schemas.financial.jso.com/FinancialLineItem/2011-07-07/" 
    xmlns:pe="http://fundamental.schemas.financial.jso.com/FinancialPeriod/2011-07-07/" 
    xmlns:seg="http://fundamental.schemas.financial.jso.com/FinancialSegment/2011-07-07/" 
    xmlns:sr="http://fundamental.schemas.financial.jso.com/FinancialSource/2011-07-07/" 
    xmlns:sli="http://fundamental.schemas.financial.jso.com/StandardizedLineItem/2011-07-07/" 
    xmlns:ss="http://fundamental.schemas.financial.jso.com/StandardizedStatement/2011-07-07/" 
    xmlns:fs="http://fundamental.schemas.financial.jso.com/FinancialStatement/2011-07-07/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:env="http://data.schemas.tfn.jso.com/Envelope/2008-05-01/" minVers="1.0" majVers="3" pubStyle="Message"> 
    <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> 
+1

你說你想解析數據,但你不知道爲什麼。你實際上想要對數據做什麼?解析XML(在術語的技術意義上)是微不足道的,解析後的數據處理會產生所有的複雜性。 –

回答

3

肯定JAXB會幫助你。 由於你正在處理複雜的XML文件,我會建議下面的方法(我同意這是冗長和手動,但應該很好)。

1)生成XSD架構出給定的XML內容的

2)在Eclipse中創建一個JAXB項目以及創建和空XSD文件,並將其含)以上

3生成的XSD架構轉換的.xsd寫文件到pojo右鍵單擊.xsd文件並生成JAXB類

4)現在編寫一個代碼來解構數據並運行它,這應該會給你一個相應的java類。

+0

簡單明瞭。 – Mordechai

+0

我打算根據你的方法嘗試一下。但是在xml文件中提到的xsd會不起作用? – SUDARSHAN

+0

如果你有.xsd文件,你可以從它開始 –