2015-11-02 102 views
0

如何使用JAXB解組以下xml並填充java對象。如何使用JAXB解析此xml

我是這個JAXB的新手。我需要爲多個客戶填充Java對象。客戶列表中有兩個客戶需要轉換爲java對象。

同樣在XML太提到的服務...

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <customerList><customer><name>ATNT</name><category>Network</category>        
     <country>USA</country><device>7600</device></customer>  
     <customer> 
     <name>cisco</name> 
     <category>Network</category> 
     <country>USA</country> 
     <device>ubr10k</device>  
     </customer> 
    </customerList> 
    <services> 
     <softwareServices> 
     <company>TCS</company> 
     <country>India</country> 
     <clients> 
      <bank>SBI</bank> 
      <insurance>LIC</insurance> 
      <telecom>Ericsson</telecom> 
     </clients>  
     </softwareServices> 
     <softwareServices> 
     <company>Infosys</company> 
     <country>India</country> 
     <clients> 
      <bank>IDBI</bank> 
      <insurance>Lombard</insurance> 
      <telecom>Airtel</telecom> 
     </clients>  
     </softwareServices> 
    </services> 
    </root> 
+5

你到目前爲止嘗試過什麼?你卡在哪裏?有很多教程可以幫助你開始 – reto

+0

我試圖讓這個XML解組並且填充customerList中的所有客戶對象,但是我得到了空值。假設XML是簡單的格式一樣包含單個客戶節點能夠得到客戶對象 思科 網絡 美國 ubr10k martin

回答

0

使用.xsd文件(見代碼片段下面SO_customer.xsd

2。執行xjc:

C:\dev\jdk1.6.0_41\bin\xjc -d src SO_customer.xsd 

這將基因率的java類,你需要

和解組(請參見下面的代碼片段MainClass.java)。 這將輸出的第一個客戶的名稱: 「ATNT」

SO_customer.xsd(從您提供的XML ...):

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="root"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="customerList"> 
      <xs:complexType> 
      <xs:sequence> 
       <xs:element name="customer" maxOccurs="unbounded" minOccurs="0"> 
       <xs:complexType> 
        <xs:sequence> 
        <xs:element type="xs:string" name="name"/> 
        <xs:element type="xs:string" name="category"/> 
        <xs:element type="xs:string" name="country"/> 
        <xs:element type="xs:string" name="device"/> 
        </xs:sequence> 
       </xs:complexType> 
       </xs:element> 
      </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
     <xs:element name="services"> 
      <xs:complexType> 
      <xs:sequence> 
       <xs:element name="softwareServices" maxOccurs="unbounded" minOccurs="0"> 
       <xs:complexType> 
        <xs:sequence> 
        <xs:element type="xs:string" name="company"/> 
        <xs:element type="xs:string" name="country"/> 
        <xs:element name="clients"> 
         <xs:complexType> 
         <xs:sequence> 
          <xs:element type="xs:string" name="bank"/> 
          <xs:element type="xs:string" name="insurance"/> 
          <xs:element type="xs:string" name="telecom"/> 
         </xs:sequence> 
         </xs:complexType> 
        </xs:element> 
        </xs:sequence> 
       </xs:complexType> 
       </xs:element> 
      </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 

MainClass.java

package call; 

import generated.Root; 

import java.io.File; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.Unmarshaller; 

    public class MainClass { 

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

       File file = new File("C:\\SO\\src\\input.xml"); 
       JAXBContext jaxbContext = JAXBContext.newInstance(Root.class); 

       Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
       Root root = (Root) jaxbUnmarshaller.unmarshal(file); 
       System.out.println(root.getCustomerList().getCustomer().get(0).getName()); 

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

感謝你們的及時幫助。其工作正常 – martin

+0

歡迎您。你能否確認答案?或者通過提高我的回答,給我一個我喜歡的「假互聯網點」? ;-) –