Visualizzazione dei risultati da 1 a 6 su 6

Discussione: [SAX] help parsing XML

  1. #1
    Utente di HTML.it
    Registrato dal
    Dec 2001
    Messaggi
    390

    [SAX] help parsing XML

    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
    Date a Cesare quel che è di Cesare,
    tutto il resto a me!!

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: [SAX] help parsing XML

    Originariamente inviato da dmitraco
    quando vado ad effettuare il parsing, ad esempio del tag server, il parser mi realizza un comportamento anomalo
    Per cortesia, leggi questa discussione dove è spiegata la questione (e c'è un mio esempio di codice che chiarisce).
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    Utente di HTML.it
    Registrato dal
    Dec 2001
    Messaggi
    390
    vabbè...grazie per il link...ma alla fine ho risolto: avevo sbagliato la signature del metodo endElement
    Date a Cesare quel che è di Cesare,
    tutto il resto a me!!

  4. #4
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da dmitraco
    avevo sbagliato la signature del metodo endElement
    Ma almeno hai capito ora come funziona characters() ??
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  5. #5
    Utente di HTML.it
    Registrato dal
    Dec 2001
    Messaggi
    390
    si...e dato che mi viene invocato più volte all'interno della stringa racchiusa dai tag, gestisco l'unicità tramite un controllo su currentTag fatto su endElement
    Date a Cesare quel che è di Cesare,
    tutto il resto a me!!

  6. #6
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da dmitraco
    si...e dato che mi viene invocato più volte all'interno della stringa racchiusa dai tag, gestisco l'unicità tramite un controllo su currentTag fatto su endElement
    Che vuol dire "gestisco l'unicità tramite un controllo"???

    Se (e ripeto, se) hai guardato quel mio esempio, la cosa tipica che si può fare è "buttare" tutto in un buffer e poi solo nel endElement() si ha finalmente il testo completo. Se hai fatto così, ok è appropriato. Se non hai fatto così .... potrebbe non esserlo ... ma per me ok, fai come preferisci ovviamente.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.