Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > Java
You last visited: Today at 23:09

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[JAVA] DOM XML jedes folgende Attribut umbennen

Discussion on [JAVA] DOM XML jedes folgende Attribut umbennen within the Java forum part of the Coders Den category.

Reply
 
Old   #1
 
Zunft's Avatar
 
elite*gold: 0
Join Date: Mar 2013
Posts: 3,185
Received Thanks: 1,317
[JAVA] DOM XML jedes folgende Attribut umbennen

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
Zunft is offline  
Old 01/25/2014, 19:41   #2
 
XxharCs's Avatar
 
elite*gold: 34
Join Date: Apr 2011
Posts: 1,475
Received Thanks: 1,228
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);
    }
}
XxharCs is offline  
Thanks
1 User
Old 01/25/2014, 19:54   #3
 
Zunft's Avatar
 
elite*gold: 0
Join Date: Mar 2013
Posts: 3,185
Received Thanks: 1,317
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.
Zunft is offline  
Old 01/26/2014, 15:19   #4
 
elite*gold: 0
Join Date: Jan 2012
Posts: 759
Received Thanks: 416
Mit (oder vergleichbarem) ginge das viel einfacher - sofern du das noch nicht kennst.
dowhile is offline  
Reply


Similar Threads Similar Threads
[Java] Datei erstellt, wenn sie bereits existiert dann umbennen
01/16/2014 - General Coding - 2 Replies
Hey Leute, also ich habe folgenden Code int zahl = 1; File imageFile = new File("screenshot" + zahl + ".png"); ImageIO.write(bufferedImage, "png", imageFile);
Attribut - Meisterschaft
10/11/2012 - World of Warcraft - 2 Replies
Was bewirkt Meisterschaft genau? Ich hab das noch nicht ganz verstanden. (Lange WoW Pause) Bei Google habe ich nur das hier gefunden, aber so ganz beantwortet das nicht meine Frage... Meisterschaft – Hierbei handelt es sich um einen neuen Wert, der den Spielern erlauben wird sich in der Sache zu verbessern, die den gewählten Talentbaum cool oder einzigartig macht. Er ist direkt an die Talente gebunden. Was ihr also erhaltet, wenn ihr diesen Wert erhöht, hängt ganz von eurer Klasse und der...
[TuT] +3 > +10 Effekt + Attribut
07/17/2008 - Flyff - 40 Replies
So da ich ja schon in einem anderen Post geschrieben hatte, das ich mich an die Arbeit machen werden und herausfinden werde wie man sein Set leuchten (glow) von +3, +4 etc. auf +10 machen kann, habe dazu noch herausgefunden wie man Attribute die auf der waffe sind (muss mindestens +4 sein) wie man die auch ganz einfach auf +10 erweitern kann. Ihr braucht folgendes 1. FRM (Flyff Resource Manager) 2. Integrity Bypasser Und so geht es: Als erstes öffnet ihr mit dem FRM die data.res...
200 Attribut Punkte am Anfang
09/16/2006 - Guild Wars - 14 Replies
Als ich den char gemacht hab, hatte ich im tutorial schon 200 attributpunkte :) (factions) http://www.zerowaitingtime.com/4043-download-gw00 1.bmp
Attribut Hack
01/08/2006 - Guild Wars - 7 Replies
Gibt es einen Hack, der die Anzahl der Atrriputpunkte erhöht? Hab mal nen Screen gesehen, wo jemand bei jedem Atrribut über 200 Punkte hatte :eek: Bitte um Link oder sonstiges mfg



All times are GMT +1. The time now is 23:09.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.