Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it
    Registrato dal
    Nov 2009
    Messaggi
    16

    Sax Parser e limitazione

    Caio a tutti,

    avrei bisogno del vostro aiuto. Devo parsificare un file XML usando Sax ma ho un problema di character limitation (2048) che random mi taglia delle stringhe.
    Immagino che dovrei scansionare tutto in un buffer ma non sono molto pratico,mi potete aiutare?
    Di seguito la class Handler che uso:
    class HowToHandler extends DefaultHandler {
    boolean nome = false;
    boolean valore = false;
    int x,y=0;
    int i;
    private Vector<String> fileItems = new Vector<String>();
    StringBuffer buf = new StringBuffer();






    public void startElement(String nsURI, String strippedName, String tagName,
    Attributes attributes) throws SAXException {


    if ( tagName.equalsIgnoreCase("Name") )

    nome = true;


    if ( tagName.equalsIgnoreCase("Val") )
    valore = true;
    }



    public void characters(char [] ch, int start, int length) {
    //buf.append(new String(ch, start, length));

    if ( nome ) {



    fileItems.addElement((new String(ch, start, length)));

    System.out.println("Prova:" + fileItems.get(x));
    x++;
    nome = false;


    }


    else
    if ( valore )
    {


    buf.append(new String(ch, start, length));




    System.out.println("PLuto:" + fileItems.get(x));
    x++;
    valore = false;




    }



    public Vector getFileItems ()
    {
    return fileItems;
    }

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

    Re: Sax Parser e limitazione

    Originariamente inviato da sbondi
    Immagino che dovrei scansionare tutto in un buffer ma non sono molto pratico
    Sì, credo di aver capito cosa vuoi fare. Ma vediamo la cosa in generale: hai un tag che contiene del testo. E una volta che, tramite gli eventi, sei arrivato a quel tag vuoi ottenere una stringa con il testo completo contenuto nel tag.
    La questione è che characters() ti viene invocato potenzialmente più volte e con una quantità arbitraria di testo volta per volta, che tu non sai a priori.

    Quello che si fa generalmente è questo: quando trovi il tag di apertura, istanzi (o svuoti, se vuoi riusarne uno già istanziato) un StringBuffer o StringBuilder. Ad ogni invocazione di characters() semplicemente "appendi" nel buffer i caratteri (e nota bene, in StringBuffer/StringBuilder c'è già un metodo append() apposito che riceve char[]+offset+length, non c'è bisogno di creare un String!!).
    Quando trovi il tag di chiusura, hai nel StringBuffer/StringBuilder il tuo bel contenuto del tag.

    Se vuoi posso anche abbozzare del codice, ma credo/spero che la descrizione sopra sia chiara.
    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
    Nov 2009
    Messaggi
    16

    Help

    Ciao,
    innanzitutto grazie del suggerimento.
    Ho provato come di seguito
    import org.xml.sax.*;
    import org.xml.sax.helpers.*;
    import java.util.*;


    class HowToHandler extends DefaultHandler {
    boolean nome = false;
    boolean valore = false;
    int x,y=0;
    int i;
    private Vector<String> fileItems = new Vector<String>();
    StringBuffer buf = new StringBuffer();


    /* public void characters(char[] ch, int start, int length)
    {
    buf.append(ch, start, length);
    }*/



    public void startElement(String nsURI, String strippedName, String tagName,
    Attributes attributes) throws SAXException {


    if ( tagName.equalsIgnoreCase("Name") )

    nome = true;



    if ( tagName.equalsIgnoreCase("Val") )
    valore = true;

    }



    public void characters(char [] ch, int start, int length) {


    if ( nome ) {

    buf.append(ch, start, length);

    //addElement((new String(ch, start, length)));

    fileItems.addElement(buf.toString());
    System.out.println("Prova:" + fileItems.get(x));
    x++;
    nome = false;


    }


    else
    if ( valore )
    {

    //fileItems.addElement((new String(ch, start, length)));
    buf.append(new String(ch, start, length));

    fileItems.addElement(buf.toString());


    System.out.println("PLuto:" + fileItems.get(x));
    x++;
    valore = false;

    // x++;

    }

    }



    public Vector getFileItems ()
    {
    return fileItems;
    }



    }
    ma ho un Exception:java.lang.ArrayIndexOutOfBoundsException :
    apena finisco il parse.

    Sai mica cosa possa essere?

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

    Re: Help

    Originariamente inviato da sbondi
    ma ho un Exception:java.lang.ArrayIndexOutOfBoundsException :
    apena finisco il parse.

    Sai mica cosa possa essere?
    No, non ho idea perché dovrei analizzare bene il sorgente.

    Comunque tu fai:
    fileItems.addElement(buf.toString());

    E lo fai in characters(). NO. Il buffer completo di tutto il testo contenuto nel tag lo puoi avere solo quando trovi il tag di chiusura, ovvero quando ti viene invocato endElement() per quello stesso elemento.


    Quindi un esempio (molto abbozzato) che (ri)usa in modo intelligente lo StringBuffer è:

    codice:
    private StringBuffer buf = new StringBuffer();
    private boolean interessaContenuto;
    
    
    public void startElement(String uri, String localName, String qName, Attributes attributes) {
        interessaContenuto = false;
    
        if (qName.equalsIgnoreCase("Name")) {
            interessaContenuto = true;
        }
    
        // ... altri eventuali test ...
    
        if (interessaContenuto) {
            buf.setLength(0);
        }
    }
    
    
    public void characters(char[] ch, int start, int length) {
        if (interessaContenuto) {
            buf.append(ch, start, length);
        }
    }
    
    
    public void endElement(String uri, String localName, String qName) {
        if (qName.equalsIgnoreCase("Name")) {
            String contenutoDiName = buf.toString();     // <---- Qui ho il contenuto!!!
    
            .....
        }
    }
    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
    Nov 2009
    Messaggi
    16

    Grazie!

    Grazie, mi è stato utie per capire dove sbagliavo!!!!

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.