[JAVA] DOM XML jedes folgende Attribut umbennen

01/25/2014 19:16 Zunft#1
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));
                    
                }
jedoch sieht das Resultat dann aus irgendeinem Grund so aus:
9,2,4,5
01/25/2014 19:41 XxharCs#2
Welchen Wert hat deine Variable i in deinem Ansatz?
Stell sicher dass du von 0 Anfängst.

Code:
public void updateIdAttribute(Document doc) {
    
    NodeList movie = doc.getElementsByTagName("Movie");
    Element elm = null;
    for(int i = 0; i < movie.getLength(); i++) {
        
        elm = (Element) movie.item(i);
        elm.setAttribute("id", "" + i);
    }
}
01/25/2014 19:54 Zunft#3
Quote:
Originally Posted by XxharCs View Post
Welchen Wert hat deine Variable i in deinem Ansatz?
Stell sicher dass du von 0 Anfängst.

Der Ansatz ist eine Ergänzung zu dem obrigen Quelltext:


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));
                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));
                    
                }                
            }
        }
    }
Quote:
Originally Posted by XxharCs View Post
Code:
public void updateIdAttribute(Document doc) {
    
    NodeList movie = doc.getElementsByTagName("Movie");
    Element elm = null;
    for(int i = 0; i < movie.getLength(); i ++) {
        
        elm = (Element) movie.item(i);
        elm.setAttribute("id", "" + i);
    }
}
Danke hat soweit funktioniert lediglich
Code:
int cache = 0;
                for(int j = 0; j < movies.getLength(); j++)
                {
                    cache += 1;
                    elm = (Element) movies.item(j);
                    elm.setAttribute("id", "" +cache);
                    
                }
Musste ich abändern, damiit erstes Attribut 1 und nicht 0 ist.
01/26/2014 15:19 dowhile#4
Mit [Only registered and activated users can see links. Click Here To Register...] (oder vergleichbarem) ginge das viel einfacher - sofern du das noch nicht kennst.