2014-04-21 71 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