2014-03-06 79 views
1

我正試圖解組一個XML文件。不過,我結束了:JAXB解組問題

unexpected element (uri:"", local:"show-list"). Expected elements are <{}showList> 

我的代碼:

顯示:

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(propOrder = { "title", "description", "host", "logo", "feed" }) 
public class Show { 
    private String title; 
    private String description; 
    private String host; 
    private String logo; 
    private String feed; 
     // getter, setter 

} 

ShowList:

@XmlRootElement 
public class ShowList { 

    @XmlElementWrapper(name = "shows") 
    @XmlElement(name = "show") 
    private ArrayList<Show> shows; 

    public ArrayList<Show> getList() { 
     return shows; 
    } 

    public void setList(ArrayList<Show> shows) { 
     this.shows = shows; 
    } 

} 

XML:

<?xml version="1.0" encoding="utf-8"?> 
<show-list count="23"> 
    <show> 
     <title>TQA Weekly</title> 
     <description> 
      <![CDATA[ 
      Technology Show, dedicated to those who wish to learn about new electronics that they have bought, or will buy soon. 
      We will explaining in each episode new ways of doing things like protecting your identity online, file backup and storage, encryption, using email wisely, and each show we will be giving you new tools to do so. 
      You may visit our web-site for show notes, lists of software, links to sites, other suggested web-sites, or to send e-mails to Steve Smith with questions, comments or concerns. 
      ]]> 
     </description> 
     <host>Steve Smith</host> 
     <logo>http://images.tqaweekly.com/tqa-weekly-logo.png</logo> 
     <feed>http://feeds.podtrac.com/tTKj5t05olM$</feed> 
    </show> 
... 
</show-list>  

的main()

public static void main(String[] args) { 
     JAXBContext context; 
     try { 
      context = JAXBContext.newInstance(ShowList.class); 
      Unmarshaller um = context.createUnmarshaller(); 
      ShowList list = (ShowList) um.unmarshal(new File("D:/Program Files/apache-tomcat-7.0.35-windows-x86/apache-tomcat-7.0.35/webapps/xml/show.xml")); 
      System.out.println(list.getList().size()); 
     } catch (JAXBException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

我在哪裏犯了一個錯誤?

回答

3

設置你的@XmlRootElementname屬性的值

@XmlRootElement(name = "show-list") 

否則JAXB使用類的名稱。

而且,擺脫

@XmlElementWrapper(name = "shows") 

那會,如果你的XML就像

<show-list count="23"> 
    <shows> 
     <show> 
      <title>TQA Weekly</title> 
      <description> 
      <![CDATA[ 
      Technology Show, dedicated to those who wish to learn about new electronics that they have bought, or will buy soon. 
      We will explaining in each episode new ways of doing things like protecting your identity online, file backup and storage, encryption, using email wisely, and each show we will be giving you new tools to do so. 
      You may visit our web-site for show notes, lists of software, links to sites, other suggested web-sites, or to send e-mails to Steve Smith with questions, comments or concerns. 
      ]]> 
      </description> 
      <host>Steve Smith</host> 
      <logo>http://images.tqaweekly.com/tqa-weekly-logo.png</logo> 
      <feed>http://feeds.podtrac.com/tTKj5t05olM$</feed> 
     </show> 
    </shows> 
</show-list> 
+0

我現在結束了一個空指針工作。 :/發佈我的代碼。 –

+1

@LittleChild可能有其他映射丟失或做錯了。 –

+0

所有我已經發布在這裏:)請看看? :) –