閱讀我創建的以下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文件成功加載到對象加載數據。文件路徑工作正常,但我不知道我做錯了什麼。
非常感謝您的幫助。
我不小心複製了我的XML不正確,但它設置正確後,我檢查了鏈接的網站上。而我的代碼幾乎與你的一樣,除了你沒有在方法之上的PRSettings類中的@XmlElement,但由於某種原因,它仍然不起作用 –
我還在我的方法下添加了JAXBException,它似乎是如果我得到一個空指針例外,只要它開始解組 –
所以它完全讀取它是真棒,但由於某種原因寫入XML以下 challongeApiKey> ...... 3 startOfDecay>然後打印出完美的一切。它仍然讀得很好,所以它可以以這種方式工作,但是你有一個想法,爲什麼它可能會這樣做?無論如何,感謝您的幫助! –