2014-04-21 118 views
1

我目前可以讀取XML文件:,讀取XML文件,JAXB

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<customer id="100" r="q"> 
<datas> 
    <data> 
     <age>29</age> 
     <name>mky</name> 
    </data> 
</datas> 
</customer> 

使用Customer類:

@XmlRootElement 
public class Customer { 

String name; 
String age; 
String id; 
String r; 

@XmlAttribute 
public void setR(String R) { 
    this.r = R; 
} 

    /etc 
} 

我決定延長XML文件,以支持多個客戶:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<customers> 
<customer id="100" r="q"> 
     <age>29</age> 
     <name>mky</name> 
</customer> 
<customer id="101" r="q"> 
     <age>29</age> 
     <name>mky</name> 
</customer> 
</customers> 

然後我遇到了一些麻煩,試圖閱讀此。

我嘗試添加一個客戶等級:

@XmlRootElement 
public class Customers{ 
private ArrayList<Customer> customers; 

public List<Customer> getCustomers() { 
    return customers; 
} 

@XmlElement 
public void setCustomers(ArrayList<Customer> customers) { 
    this.customers = customers; 
} 

} 

然後嘗試與打印:

 try { 

      File file = new File("/Users/s.xml"); 
      JAXBContext jaxbContext = JAXBContext.newInstance(Customers.class); 

      Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
      Customers c = (Customers) jaxbUnmarshaller.unmarshal(file); 

      System.out.println(c.getCustomers()); 

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

     }} 

但我發現了一個空值,試圖打印此。有人能告訴我如何閱讀第二個XML文件嗎?

+0

嘗試將'customers'屬性的類型從'ArrayList '更改爲'List '。 (還沒有嘗試過,但至少我讀過的每個例子都使用'List',如果我從xsd文件生成代碼,它也使用'List') – fabian

+0

[如何使用JAXB讀取XML文件?] http://stackoverflow.com/questions/12053379/how-to-read-an-xml-file-with-jaxb) – malat

回答

2

更改您的Customers

@XmlRootElement(name = "customers") 
class Customers { 
    private List<Customer> customers; 

    public List<Customer> getCustomers() { 
     return customers; 
    } 

    @XmlElement(name = "customer") 
    public void setCustomers(List<Customer> customers) { 
     this.customers = customers; 
    } 
} 

什麼你不想要的是XML元素的get/set方法之間的不匹配。如果其中一方返回ArrayList,另一方應接受ArrayList的說法。同樣的List(這只是一個好的做法)。

+0

謝謝,這工作。 – user1413969

0

如果您在使用註釋時遇到問題,可以將其刪除並使用JAXBElement的實例代替。 要做到這一點:

  1. 首先刪除任何註釋在Customers

    public class Customers{ 
        private ArrayList<Customer> customers; 
    
        public List<Customer> getCustomers() { 
        return customers; 
        } 
    
        public void setCustomers(ArrayList<Customer> customers) { 
        this.customers = customers; 
        } 
    
    } 
    
  2. 使用JAXBElement一個實例在解析方法

    try { 
    
        File file = new File("/Users/s.xml"); 
        JAXBContext jaxbContext = JAXBContext.newInstance(Customers.class); 
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
        JAXBElement<Customers> je1 = unmarshaller.unmarshal(file, Customers.class); 
        Customers c = je1.getValue(); 
        System.out.println(c.getCustomers()); 
    
        } catch (JAXBException e) { 
        e.printStackTrace(); 
        } 
    } 
    

但是請注意,當您要覆蓋默認行爲時,註釋是必需的。 您會找到一個完整的示例here