salve a tutti. mi rivolto a tutti coloro i quali hanno usato SAX come parser XML.
devo leggere dei dati presenti all'interno di un file XML di configurazione di un database. Tralasciando l'istanziazione del parser e gli altri handler, ho queste 2 funzioni
codice:
public void startElement(String namespaceUri, String localName, String qualifiedName, Attributes attributes) throws SAXException {
if(qualifiedName.equals("database")){
this.database = true;
}else this.currentTag = qualifiedName;
//System.out.print("start");
}
public void characters(char[] chars, int startIndex, int length) throws SAXException {
if (this.currentTag != null) {
//System.out.print(this.currentTag);
if (this.database == true) {
if (this.currentTag.equals("server")){
this.server = new String(chars, startIndex, length); System.out.print(this.currentTag);System.out.println(this.server);
}
else if (this.currentTag.equals("port")){
this.port = new String(chars, startIndex, length);/*System.out.print(this.currentTag);*/ System.out.println(this.port+" è il porto");
}
else if (this.currentTag.equals("nomeDB")){
this.nomeDB = new String(chars, startIndex, length); System.out.print(this.currentTag);System.out.println(this.nomeDB);
}
else if (this.currentTag.equals("nomeutente")){
this.nomeUtente = new String(chars, startIndex, length);System.out.print(this.currentTag); System.out.println(this.nomeUtente);
}
else if (this.currentTag.equals("password")){
this.pwdUtente = new String(chars, startIndex, length);System.out.print(this.currentTag); System.out.println(this.pwdUtente);
}
}
}
}
(scusate il codice sporco, ma ho inserito i print per debugging)
quando vado ad effettuare il parsing, ad esempio del tag server, il parser mi realizza un comportamento anomalo, ovvero riconosce <server> e memorizza nella variabile server della classe il valore letto...poi legge </server>, lo riconosce come "server" e memorizza in this.server il valore successivo, ovvero /n...e questo mi accade per tutte le letture. se invece realizzo il file xml su un'unica riga, il problema non sussiste.
A I U T O