2016-03-07 152 views
0

閱讀我創建的以下XML文件時出現問題。無法使用JAXB讀取XML文件?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<prsettings> 
<prsetting> 
    <players> 
     <player> 
      <lastDateEntered>0</lastDateEntered> 
      <lastTournament>1</lastTournament> 
      <playerId>0</playerId> 
      <playersStatus>unrated</playersStatus> 
      <playersTag>asfd</playersTag> 
      <score>5.0</score> 
      <setsPlayed>0</setsPlayed> 
      <tourneysWhileInactive>0</tourneysWhileInactive> 
     </player> 
     <player> 
      <lastDateEntered>0</lastDateEntered> 
      <lastTournament>1</lastTournament> 
      <playerId>1</playerId> 
      <playersStatus>unrated</playersStatus> 
      <playersTag>ba</playersTag> 
      <score>5.0</score> 
      <setsPlayed>0</setsPlayed> 
      <tourneysWhileInactive>0</tourneysWhileInactive> 
     </player> 
     <player> 
      <lastDateEntered>0</lastDateEntered> 
      <lastTournament>1</lastTournament> 
      <playerId>2</playerId> 
      <playersStatus>unrated</playersStatus> 
      <playersTag>asdgf</playersTag> 
      <score>5.0</score> 
      <setsPlayed>0</setsPlayed> 
      <tourneysWhileInactive>0</tourneysWhileInactive> 
     </player> 
    </players> 
    <challongeApiKey>asbg</challongeApiKey> 
    <challongeUsername>asf</challongeUsername> 
    <implementPointDecay>false</implementPointDecay> 
    <numSetsNeeded>5</numSetsNeeded> 
    <numTourneysForActive>2</numTourneysForActive> 
    <numTourneysForInnactive>5</numTourneysForInnactive> 
    <numTourneysNeeded>5</numTourneysNeeded> 
    <pointsRemoved>5</pointsRemoved> 
    <prName>asf</prName> 
    <removeInnactive>false</removeInnactive> 
    <showPlacingDiff>false</showPlacingDiff> 
    <showPointDiff>false</showPointDiff> 
    <startOfDecay>3</startOfDecay> 
</prsetting> 

我PRSetting對象的observableList和PRSetting對象中我有運動員的ArrayList。這就是爲什麼我創建了一個POJO文件,並且在PRSetting對象中,我設置的唯一對象是以下內容。

@XmlElementWrapper(name="players") 
@XmlElement(name ="player") 
private ArrayList<PlayerProfile> playersList = new ArrayList<PlayerProfile>(); 

這也是我的POJO文件,應該是用來寫和讀的XML文件。

@XmlRootElement (name = "prsettings") 
public class PRSettingsWrapper { 
private ObservableList<PRSettings> prList; 

@XmlElement(name = "prsetting") 
public ObservableList<PRSettings> getPrList(){ 
    return prList; 
} 

public void setPrList(ObservableList<PRSettings> prList){ 
    this.prList = prList; 
} 
} 

出於某種原因,每當我試圖用下面的代碼

 JAXBContext context = JAXBContext 
       .newInstance(PRSettingsWrapper.class); 
     Unmarshaller um = context.createUnmarshaller(); 

     // Reading XML from the file and unmarshalling. 
     PRSettingsData wrapper = (PRSettingsData) um.unmarshal(file); 

     prList.clear(); 
     prList.addAll(wrapper.getPrList()); 

     // Save the file path to the registry. 
     setPrSettingsFilePath(file); 

我似乎無法到XML文件成功加載到對象加載數據。文件路徑工作正常,但我不知道我做錯了什麼。

非常感謝您的幫助。

回答

0

抱歉誤導你的原因。

該問題是由ObservableList引起的;

你可以參考這篇文章: Marshalling ObservableList with JAXB

我修改後的代碼從上面的鏈接,以及以下應該是可行的。

anotherTest.MyContainer

package anotherTest; 

import java.util.List; 

    import javafx.collections.FXCollections; 
    import javafx.collections.ObservableList; 

    import javax.xml.bind.annotation.XmlElement; 
    import javax.xml.bind.annotation.XmlElementWrapper; 
    import javax.xml.bind.annotation.XmlElements; 
    import javax.xml.bind.annotation.XmlRootElement; 

    @XmlRootElement(name = "prsettings") 
    public class MyContainer extends MyObject { 

     private ObservableList<MyObject> children = FXCollections.observableArrayList(); 

     @XmlElements({ @XmlElement(name = "prsetting", type = MyObject.class) }) 
     public List<MyObject> getChildren() { 
      return children; 
     } 

     public String toString() { 

      StringBuilder sb = new StringBuilder(); 
      sb.append("children:"); 

      for (MyObject node : children) { 
       sb.append("\n"); 
       sb.append(" " + node.toString()); 
      } 
      return sb.toString(); 

     } 
    } 

anotherTest.MyObject

package anotherTest; 

    import java.util.List; 

    import javax.xml.bind.annotation.XmlElement; 
    import javax.xml.bind.annotation.XmlElementWrapper; 
    import javax.xml.bind.annotation.XmlType; 

    @XmlType(name = "Object") 
    public class MyObject { 

     private String challongeApiKey; 

     @XmlElementWrapper(name = "players") 
     private List<PlayerProfile> player; 

     public String getChallongeApiKey() { 
      return challongeApiKey; 
     } 

     public void setChallongeApiKey(String challongeApiKey) { 
      this.challongeApiKey = challongeApiKey; 
     } 

     public List<PlayerProfile> getPlayer() { 
      return player; 
     } 

     public void setPlayers(List<PlayerProfile> player) { 
      this.player = player; 
     } 

    } 

anotherTest.PlayerProfile

package anotherTest; 

public class PlayerProfile { 

    private int playerId; 
    private String playersStatus; 

    public int getPlayerId() { 
     return playerId; 
    } 

    public void setPlayerId(int playerId) { 
     this.playerId = playerId; 
    } 

    public String getPlayersStatus() { 
     return playersStatus; 
    } 

    public void setPlayersStatus(String playersStatus) { 
     this.playersStatus = playersStatus; 
    } 

} 

anotherTest.Example

package anotherTest; 

import java.io.StringReader; 
import java.io.StringWriter; 
import java.util.ArrayList; 
import java.util.List; 

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

public class Example { 
    public static void main(String[] args) { 

     // create container with list 
     MyContainer container = new MyContainer(); 

     // add objects 
     MyObject object; 

     object = new MyObject(); 
     object.setChallongeApiKey("ABCABC"); 
     container.getChildren().add(object); 

     PlayerProfile p = new PlayerProfile(); 
     p.setPlayerId(1); 
     p.setPlayersStatus("unrated"); 

     List<PlayerProfile> l = new ArrayList<>(); 
     l.add(0, p); 

     object.setPlayers(l); 

     // marshal 
     String baseXml = marshal(container); 

     // unmarshal 
     container = unmarshal(baseXml); 

     System.out.println("unmarshal test: " + container.getChildren().get(0).getChallongeApiKey()); 
     System.out.println("unmarshal test: " + container.getChildren().get(0).getPlayer().get(0).getPlayerId()); 
     System.out.println("unmarshal test: " + container.getChildren().get(0).getPlayer().get(0).getPlayersStatus()); 

     System.exit(0); 
    } 

    public static String marshal(MyContainer base) { 

     try { 

      JAXBContext jaxbContext = JAXBContext.newInstance(MyContainer.class); 

      Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); 
      jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 

      StringWriter stringWriter = new StringWriter(); 
      jaxbMarshaller.marshal(base, stringWriter); 
      String xml = stringWriter.toString(); 

      System.out.println("XML:\n" + xml); 

      return xml; 

     } catch (Exception e) { 
      throw new RuntimeException(e); 
     } 

    } 

    public static MyContainer unmarshal(String xml) { 

     try { 

      JAXBContext jaxbContext = JAXBContext.newInstance(MyContainer.class); 

      Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
      StringReader stringReader = new StringReader(xml); 

      MyContainer container = (MyContainer) jaxbUnmarshaller.unmarshal(stringReader); 

      return container; 

     } catch (Exception e) { 
      throw new RuntimeException(e); 
     } 

    } 
} 

在控制檯輸出

XML: 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<prsettings> 
    <prsetting> 
     <players> 
      <player> 
       <playerId>1</playerId> 
       <playersStatus>unrated</playersStatus> 
      </player> 
     </players> 
     <challongeApiKey>ABCABC</challongeApiKey> 
    </prsetting> 
</prsettings> 

unmarshal test: ABCABC 
unmarshal test: 1 
unmarshal test: unrated 

對不起,我不知道爲什麼無論是。我測試瞭如果聲明孩子爲List<MyObject>,它可以正常工作。但是,當涉及到ObservableList時,您必須聲明它是這樣的,否則將在解組時發生NullPointerException(但相同的代碼在編組時很好)。

private ObservableList<MyObject> children = FXCollections.observableArrayList(); 
+0

我不小心複製了我的XML不正確,但它設置正確後,我檢查了鏈接的網站上。而我的代碼幾乎與你的一樣,除了你沒有在方法之上的PRSettings類中的@XmlElement,但由於某種原因,它仍然不起作用 –

+0

我還在我的方法下添加了JAXBException,它似乎是如果我得到一個空指針例外,只要它開始解組 –

+0

所以它完全讀取它是真棒,但由於某種原因寫入XML以下 ...... 3然後打印出完美的一切。它仍然讀得很好,所以它可以以這種方式工作,但是你有一個想法,爲什麼它可能會這樣做?無論如何,感謝您的幫助! –