Hi!
My problem is that when i try to add an element on the root element, after the
fifth one it always overwrites instead the fifth element.
Please consider that I first
parse the following xml document that has 5 elements and then I try to add one
more. Also the code is available below:
<?xml version="1.0" encoding="UTF-8" ?>
- <Matchups>
- <Matchup>
<Home_team>PAO</Home_team>
<Away_team>PAOK</Away_team>
<Home_win_return>1.3</Home_win_return>
<Draw_return>1.2</Draw_return>
<Away_win_return>2.9</Away_win_return>
</Matchup>
- <Matchup>
<Home_team>Apollonas</Home_team>
<Away_team>Anorthosis</Away_team>
<Home_win_return>2.0</Home_win_return>
<Draw_return>2.3</Draw_return>
<Away_win_return>2.0</Away_win_return>
</Matchup>
- <Matchup>
<Home_team>OSFP</Home_team>
<Away_team>AEK</Away_team>
<Home_win_return>1.7</Home_win_return>
<Draw_return>3.2</Draw_return>
<Away_win_return>2.5</Away_win_return>
</Matchup>
- <Matchup>
<Home_team>Apollonas</Home_team>
<Away_team>Anorthosis</Away_team>
<Home_win_return>2.0</Home_win_return>
<Draw_return>2.0</Draw_return>
<Away_win_return>2.0</Away_win_return>
</Matchup>
- <Matchup>
<Home_team>Omonoia</Home_team>
<Away_team>Omonoia</Away_team>
<Home_win_return>3.0</Home_win_return>
<Draw_return>3.0</Draw_return>
<Away_win_return>3.0</Away_win_return>
</Matchup>
</Matchups>
and here's the method which parses this xml and tries to add the 6th element:
public void add_matchup(Matchup matchup)
{
boolean exists = (new File("C:\\Eclipse Projects\\AIBet\\Matchups.xml")).exists();
if (exists) {
try {
Document matchups_doc = this.parse("C:\\Eclipse Projects\\AIBet\\Matchups.xml");
Element root_mod = matchups_doc.getRootElement();
Element matchups = root_mod.addElement(CONSTANTS.MATCHUP);/*Here!*/
matchups.addElement(CONSTANTS.HOME_TEAM).addText(matchup.getHome_team());
matchups.addElement(CONSTANTS.AWAY_TEAM).addText(matchup.getAway_team());
matchups.addElement(CONSTANTS.HOME_WIN_RETURN)
.addText(Double.toString(matchup.getHome_win_return()));
matchups.addElement(CONSTANTS.DRAW_RETURN)
.addText(Double.toString(matchup.getDraw_return()));
matchups.addElement(CONSTANTS.AWAY_WIN_RETURN)
.addText(Double.toString(matchup.getAway_win_return()));
XMLWriter writer;
try {
writer = new XMLWriter(new FileWriter( "matchups.xml" ) );
writer.write(matchups_doc );
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (DocumentException e) {
e.printStackTrace();
}
}else {
// File or directory does not exist
Document matchups_doc = DocumentHelper.createDocument();
Element root = matchups_doc.addElement("Matchups");
Element matchups = root.addElement(CONSTANTS.MATCHUP);
matchups.addElement(CONSTANTS.HOME_TEAM).addText(matchup.getHome_team());
matchups.addElement(CONSTANTS.AWAY_TEAM).addText(matchup.getAway_team());
matchups.addElement(CONSTANTS.HOME_WIN_RETURN)
.addText(Double.toString(matchup.getHome_win_return()));
matchups.addElement(CONSTANTS.DRAW_RETURN)
.addText(Double.toString(matchup.getDraw_return()));
matchups.addElement(CONSTANTS.AWAY_WIN_RETURN)
.addText(Double.toString(matchup.getAway_win_return()));
XMLWriter writer;
try {
writer = new XMLWriter(new FileWriter( "matchups.xml" ) );
writer.write( matchups_doc );
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Thanks for any help in advance!!
Br
Oxinoschar