Code:
public static void deleteMovie(Document doc, String personName)
{
NodeList nodes = doc.getFirstChild().getChildNodes();
for (int i = 0; i < nodes.getLength(); i++)
{
if(nodes.item(i).getNodeName().equals(STR_RETURN_TEXT))
{
continue;
}
if(nodes.item(i).getAttributes().item(0).getTextContent().equals(personName))
{
doc.getFirstChild().removeChild(nodes.item(i));
}
}
}
Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <MovieDB> <Movie id="1"> <titel>Titanic1</titel> <genre>Drama, Romance</genre> <jahr>1997</jahr> <schauspieler>Leonardo DiCaprio, Kate Winslet, Billy Zane, Kathy Bates</schauspieler> <handlung>A seventeen-year-old aristocrat, expecting to be married to a rich claimant by her mother, falls in love with a kind but poor artist aboard the luxurious, ill-fated R.M.S. Titanic.</handlung> <dauer>194 min</dauer> </Movie> <Movie id="2"> <titel>Titanic2</titel> <genre>Drama, Romance</genre> <jahr>1997</jahr> <schauspieler>Leonardo DiCaprio, Kate Winslet, Billy Zane, Kathy Bates</schauspieler> <handlung>A seventeen-year-old aristocrat, expecting to be married to a rich claimant by her mother, falls in love with a kind but poor artist aboard the luxurious, ill-fated R.M.S. Titanic.</handlung> <dauer>194 min</dauer> </Movie> <Movie id="3"> <titel>Titanic3</titel> <genre>Drama, Romance</genre> <jahr>1997</jahr> <schauspieler>Leonardo DiCaprio, Kate Winslet, Billy Zane, Kathy Bates</schauspieler> <handlung>A seventeen-year-old aristocrat, expecting to be married to a rich claimant by her mother, falls in love with a kind but poor artist aboard the luxurious, ill-fated R.M.S. Titanic.</handlung> <dauer>194 min</dauer> </Movie> <Movie id="4"> <titel>Titanic4</titel> <genre>Drama, Romance</genre> <jahr>1997</jahr> <schauspieler>Leonardo DiCaprio, Kate Winslet, Billy Zane, Kathy Bates</schauspieler> <handlung>A seventeen-year-old aristocrat, expecting to be married to a rich claimant by her mother, falls in love with a kind but poor artist aboard the luxurious, ill-fated R.M.S. Titanic.</handlung> <dauer>194 min</dauer> </Movie> <Movie id="5"> <titel>Titanic5</titel> <genre>Drama, Romance</genre> <jahr>1997</jahr> <schauspieler>Leonardo DiCaprio, Kate Winslet, Billy Zane, Kathy Bates</schauspieler> <handlung>A seventeen-year-old aristocrat, expecting to be married to a rich claimant by her mother, falls in love with a kind but poor artist aboard the luxurious, ill-fated R.M.S. Titanic.</handlung> <dauer>194 min</dauer> </Movie> </MovieDB>
Der Java-Algorithmus oben löscht aus der XML Datei einen Film komplett heraus jedoch liegt das Problem dabei, dass die Attribute von "Movie" nicht mitgeändert werden heißt:
vor dem löschen:
1,2,3,4,5
nach dem löschen von 3:
1,2,4,5
Soll aber so aussehen:
1,2,3,4
Das möchte ich ändern -- mein Ansatz:
Code:
NodeList movies = doc.getElementsByTagName("Movie");
Element emp = null;
for(int j = i; j < movies.getLength(); j++)
{
emp = (Element) movies.item(j);
emp.setAttribute("id", String.valueOf(j));
}
9,2,4,5