2011-11-30 84 views
1

我有xml,看起來有點類似於以下內容。我需要從屬性和標籤獲取值並存儲它們。但我不明白如何到達2級(3,4等)嵌套元素。我使用以下代碼,我發現在互聯網上,它使用DOM但我不能得到正確的RegNumber及其屬性。提前致謝。Java:閱讀許多嵌套元素的XML

nodeList = xmldocument.getElementsByTagName("Header");  
     if (nodeList != null && nodeList.getLength() > 0) { 
      for (int i = 0; i < nodeList.getLength(); i++) { 
       //get the header element 
       Element header = (Element) nodeList.item(i); 
       System.out.println("Element: " + ((Element)nodeList.item(i)).getNodeName()); 
       System.out.println(header.getAttribute("time")); 
      } 
     } 
     nodeList = xmldocument.getElementsByTagName("Document");  
     if (nodeList != null && nodeList.getLength() > 0) { 
      for (int i = 0; i < nodeList.getLength(); i++) { 

       //get the document element 
       Element document = (Element) nodeList.item(i); 
       System.out.println("Element: " + ((Element)nodeList.item(i)).getNodeName()); 
       System.out.println(document.getAttribute("Id")); 

       nodeList = document.getElementsByTagName("RegNumber"); 
       for (int j = 0; j < nodeList.getLength(); j++) { 

        //get the RegNumber element 
        Element regNumber = (Element) nodeList.item(j); 
        System.out.println("Element: " + ((Element)nodeList.item(j)).getNodeName()); 
        System.out.println(regNumber.getAttribute("regpoint")); 

       } 
      } 

     } 

XML:

<XML xsi:schemaLocation="http://www.codetools.it XSD2.xsd" xmlns="http://www.codetools.it" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
      <Header time="2001-12-17T09:30:47Z" /> 
      <Document Id="456"> 
       <RegNumber regpoint="" regdate="2001-12-17T09:30:47Z">123/456</RegNumber> 
       <Confident flag="0"/> 
       <DocNumber kind=""> 
        <RegNumber RegPoint="" regdate="2001-12-17T09:30:47Z">456/789</RegNumber> 
        <Organization fullname="lol" shortname="" ownership="lol" ogrn="78945612" inn=""> 
         <OfficialPerson> 
          <Name Firstname="John"/> 
          <Official Department="" post=""/> 
          <SignDate>2001-12-17T09:30:47Z</SignDate> 
         </OfficialPerson> 
         <Econtact type="1">[email protected]</Econtact> 
         <Address street="" settlement="" postcode="" postbox="" flat="" district="" region="" country="" nontypical="" house=""/> 
        </Organization> 
       </DocNumber> 
       <DocTransfer os="Windows" type=".docx" type_ver="" char_set="" description="kkkk"> 
    </DocTransfer> 
      <Reghistory idnumber=""> 
       <RegNumber RegPoint="" regdate=""/> 
       <Organization fullname="" shortname="" ownership="" ogrn="" inn=""> 
        <Econtact type=""/> 
        <Address street="" settlement="" postcode="" postbox="" flat="" district="" region="" country="" nontypical="" house=""/> 
       </Organization> 
      </Reghistory> 
      <Author> 
       <Organization fullname="" shortname="" ownership="" ogrn="12345678" inn=""> 
        <OfficialPerson> 
         <Name Firstname="" /> 
         <Official Department="" post=""/> 
         <SignDate>2011-12-17T09:30:47Z</SignDate> 
        </OfficialPerson> 
        <Econtact type="Рї">[email protected]</Econtact> 
        <Address street="" postcode="1234" postbox="" flat="" district="" region="" country="" nontypical="" house="12"/> 
       </Organization> 
       <PrivatePerson> 
        <Name Surname="" Firstname="" Fathername=""/> 
        <Econtact type=""/> 
        <Address street="" settlement="" postcode="" postbox="" flat="" district="" region="" country="" nontypical="" house=""/> 
        <SignDate/> 
        <Rank privilege="" socialposition="" sex=""/> 
       </PrivatePerson> 
      </Author> 
      <Validator attestation="0"> 
       <DocNumber kind="наказ"> 
        <RegNumber RegPoint="" regdate="2001-12-17T09:30:47Z"/> 
        <Organization fullname="" shortname="" ownership="" ogrn="14725836" inn=""> 
         <Econtact type="Рї">[email protected]</Econtact> 
         <Address street="" settlement="" postcode="" postbox="" flat="" district="" region="" country="" nontypical="" house=""/> 
        </Organization> 
       </DocNumber> 
       <Organization fullname="" shortname="РљРњРЈ" ownership="" ogrn="96325878" inn=""> 
        <Econtact type="String">[email protected]</Econtact> 
        <Address street="Пушкіна" settlement="" postcode="4563" postbox="" flat="" district="" region="" country="" nontypical="" house="23"/> 
        <OfficialPerson> 
         <Name Surname="" Firstname="" Fathername=""/> 
         <Official Department="" post=""/> 
         <SignDate>2011-12-17T09:30:47Z</SignDate> 
        </OfficialPerson> 
       </Organization> 
       <PrivatePerson> 
        <Name Surname="" Firstname="" Fathername=""/> 
        <Econtact type=""></Econtact> 
        <Address street="" settlement="" postcode="" postbox="" flat="" district="" region="" country="" nontypical="" house=""/> 
        <SignDate></SignDate> 
        <Rank privilege="" socialposition="" sex=""/> 
       </PrivatePerson> 
      </Validator> 
      <Addressee> 
       <Referred id="" retype=""> 
        <RegNumber regdate="" regpoint=""/> 
        <TaskNumber taskDate=""/> 
       </Referred> 
       <Organization fullname="String" shortname="" ownership="" ogrn="85236974" inn=""> 
        <OfficialPerson> 
         <Name Surname="" Firstname="" Fathername=""/> 
         <Official Department="" post=""/> 
        </OfficialPerson> 
        <Econtact type="Рї">[email protected]</Econtact> 
        <Address street="" settlement="" postcode="" postbox="" flat="" district="" region="" country="" nontypical="" type="" house=""/> 
       </Organization> 
       <PrivatePerson> 
        <Name Surname="" Firstname="" Fathername=""/> 
        <Econtact type=""></Econtact> 
        <Address street="" settlement="" postcode="" postbox="" flat="" district="" region="" country="" nontypical="" house=""/> 
        <Rank privilege="" socialposition="" sex=""/> 
       </PrivatePerson> 
      </Addressee> 
      <Writer> 
       <Organization fullname="" shortname="" ownership="" ogrn="" inn=""> 
        <OfficialPerson> 
         <Name Surname="" Firstname="" Fathername=""/> 
         <Econtact type=""></Econtact> 
        </OfficialPerson> 
       </Organization> 
       <PrivatePerson> 
        <Name Surname="" Firstname="" Fathername=""/> 
        <Econtact type=""></Econtact> 
       </PrivatePerson> 
      </Writer> 
     </Document> 

編輯。據建議,我試圖弄清楚JAXB thingy。 xjc工具生成了3個類:ObjectFactory,package-info,XML。我發現這個代碼從XML創建對象:

public static void main(String[] args) { 
     try { 

      File file = new File("F:\\Untitled3.xml"); 
      JAXBContext jaxbContext = JAXBContext.newInstance(XML.class); 

      Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
      XML xml = (XML) jaxbUnmarshaller.unmarshal(file); 
      System.out.println(xml); 

     } catch (JAXBException e) { 
      e.printStackTrace(); 
     } 
    } 

這是正確的方法,或者我需要使用的ObjectFactory?:

ObjectFactory f =new ObjectFactory(); 
      XML xml1 = f.createXML(); 

如何創建對象所有的標籤和屬性,然後從它創建xml文件?我新來java和jaxb的東西,所以請忍受我。

+2

你真的想手動解析XML嗎?有更好的方法,看看[JAXB](http://en.wikipedia.org/wiki/Java_Architecture_for_XML_Binding) –

+0

謝謝你的回答。我真的不知道我應該用什麼。我剛剛通過Google搜索「Java xml read」發現了Dom的東西,它對我來說很簡單。您能否給我一些關於JAXB初學者的鏈接? – bunnyjesse112

回答

3

它通常是沒有意義或方法,讓你在一定的深度訪問節點中很常用。這不僅是關於Java,而是關於DOM規範,我相信這是正確的決定。

人們通常是通過許多有意義的方式之一訪問節點:

  1. 通過標籤名稱和標記名稱通常表示該節點的意義。
  2. XPath - 表示文檔的完整結構。
  3. 綁定,例如JAXB(基本上你是不是在處理XML的任何以上,但Java類)
  4. 有舊的SAX(未DOM)和StAX的(DOM和SAX的混合物)

查找xpath,這將是非常有用的。

+0

感謝您的回答。您能否將我的一些教程鏈接給JAXB的初學者? – bunnyjesse112

+0

再次感謝。如果有辦法將我的xml文件(我有它的xsd文件)轉換爲準備用於jaxb的類? Hibernate有類似的東西。我懷疑我可以手動編寫並正確註釋它。再次感謝。 – bunnyjesse112

+1

+1 - @ bunnyjesse112如果您已經有了XML模式,您可以使用XJC工具來生成對象模型。從我的博客下面的帖子演示如何做到這一點:http://blog.bdoughan.com/2010/09/processing-atom-feeds-with-jaxb.html –

1

在每個節點上再次使用「getElementsByTagName」函數以通過標籤名稱獲取元素。很顯然......

下面是去幾級深我的示例代碼:

DocumentBuilder db = dbf.newDocumentBuilder(); 
     Document doc = db.parse(acpFile); 
     Element docEle = doc.getDocumentElement(); 
     NodeList series = docEle.getElementsByTagName("dod:recordSeries"); 

     if(series != null && series.getLength()>0) 
     { 
      for(int i=0; i<series.getLength(); i++) 
      { 
       Element serie = (Element)series.item(i); 
       System.out.println("S -- "+serie.getAttribute("view:childName")); 
       NodeList categories = serie.getElementsByTagName("dod:recordCategory"); 

       for(int j=0; j<categories.getLength(); j++) 
       { 
        Element category = (Element)categories.item(j); 
        System.out.println("C ---- "+category.getAttribute("view:childName")); 
        NodeList recordFolders = category.getElementsByTagName("rma:recordFolder"); 

        for(int k=0; k<recordFolders.getLength(); k++) 
        { 
         Element folder = (Element)recordFolders.item(k); 
         System.out.println("F ------ "+folder.getAttribute("view:childName")); 
        } 
       } 
      } 
     } 
+0

感謝您的回答。但是,如果不僅意味着類別? – bunnyjesse112