Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it L'avatar di Angelo1974
    Registrato dal
    Feb 2003
    Messaggi
    1,107

    [ JAVA ] Lettura file XML.............

    Salve a tutti..
    Allora, ho un file .xml in cui ci sono diversi campi tra i quali ci sono
    due stringhe che per me rappresentano della date; in particolare ho:

    .
    .
    .
    .
    <DATA_PRESCRIZIONE>
    05052001
    </DATA_PRESCRIZIONE>
    .
    .
    .
    .
    .
    <DATA_SPEDIZIONE>
    05052001
    </DATA_SPEDIZIONE>
    .
    .
    .
    .


    Ora, lasciamo perdere che i 2 valori sono identici; poichè il file è molto grosso
    e non ho bisogno di operazioni particolari altre alla sua lettura, mi sono orientato
    verso la tecnologia SAX per leggere il file; dunque avrò, nell'handler del file .xml
    una cosa del tipo:
    ( sono in public void characters(char[] ch, int start, int length) throws SAXException )

    .
    .
    if( readingDataSpedizione ){
    dati.setDataSpedizione( chars );
    }else if( readingDataPrescrizione ){
    dati.setDataPrescrizione( chars );
    }
    .
    .


    Dopo di che vado a leggere col gestore ma mi capita che per la maggior
    parte dei dati le date ( o meglio le stringhe che rappresntano delle date )
    vengono lette correttamente, per altre, però, succede che è
    come se venissero spezzettate e, quindi, non sono lette correttamente; se,
    però, apro il file .xml e vado a controllare questi campi, sono scritti in maniera
    corretta... qualcuno sa dirmi come mai?
    Ho provato di tutto, ho aperto il file .xml con tutti gli editor che avevo per vedere
    se c'era qualche invio che mi era scappato ( perchè è come se ci fosse un invio che mi
    "sconquassa" tutto ). Spero di essere stato sufficientemente chiaro. Grazie e ciao a tutti

    Se vuoi trovare l'arcobaleno, devi sopportare la pioggia

  2. #2
    Utente di HTML.it L'avatar di Angelo1974
    Registrato dal
    Feb 2003
    Messaggi
    1,107
    Ciao di nuovo... ho risolto il problema usando JDom per la scrittura del file xml ( prima scrivevo il file in maniera molto agreste.... lasciamo perdere... ); ora utilizzando il JDom ho scritto una classe di questo tipo:


    import org.jdom.*;
    import org.jdom.output.XMLOutputter;

    public class ConvertimiBene{
    private String nomeFile;
    private File myFile;
    public ConvertimiBene() {
    }
    public void creaXml( String path, String nomeDirCodice, String nomeDirDataCC ) {
    }

    public void scriviXml( String path){
    Document documento = null;
    String stringaLetta = "";
    int dimArray = 0;
    int j = 0;
    //C:\WINDOWS\Desktop\parser
    try{
    BufferedReader buff = new BufferedReader(new FileReader(new File(
    "C:\\WINDOWS\\Desktop\\parser\\02102_082003.txt")) );
    File fileTmp = new File( path );

    while( (stringaLetta = buff.readLine())!= null ){
    dimArray++;
    }
    System.out.println("Dimensione array: "+dimArray);
    buff.close();
    buff = new BufferedReader(new FileReader(new File(
    "C:\\WINDOWS\\Desktop\\parser\\02102_082003.txt")) );
    /*
    ISTANZIAMO UN OGGETTO Element
    PASSANDOGLI LA STRINGA
    CHE SARA' IL NOME DEL NOSTRO SUPERTAG
    */
    Element root = new Element("FARMACIA");

    /*
    ISTANZIAMO UN OGGETTO Document
    PASSANDOGLI COME PARAMETRO L'OGGETTO
    ELEMENT PRECEDENTEMENTE ISTANZIATO
    */

    documento = new Document(root);
    stringaLetta = "";

    Element[] sottoNodo = new Element[ dimArray ];
    Element[] tipologia = new Element[ dimArray ];
    Element[] regione = new Element[ dimArray ];
    Element[] asl = new Element[ dimArray ];
    Element[] distretto = new Element[ dimArray ];
    Element[] riferimento = new Element[ dimArray ];
    Element[] identificativoRicetta = new Element[ dimArray ];
    Element[] codFisc = new Element[ dimArray ];
    Element[] nomeAssistito = new Element[ dimArray ];
    Element[] cognomeAssistito = new Element[ dimArray ];
    Element[] indirizzoAssistito = new Element[ dimArray ];
    Element[] etaAssistito = new Element[ dimArray ];
    Element[] sessoAssistito = new Element[ dimArray ];
    Element[] nonSignificativo1 = new Element[ dimArray ];
    Element[] esenzione = new Element[ dimArray ];
    Element[] numeroTotPezzi = new Element[ dimArray ];
    Element[] dataPresc = new Element[ dimArray ];
    Element[] prescrittore = new Element[ dimArray ];
    Element[] nonSignificativo2 = new Element[ dimArray ];
    Element[] codiceFustella1 = new Element[ dimArray ];
    Element[] codiceFustella2 = new Element[ dimArray ];
    Element[] codiceFustella3 = new Element[ dimArray ];
    Element[] codiceFustella4 = new Element[ dimArray ];
    Element[] codiceFustella5 = new Element[ dimArray ];
    Element[] codiceFustella6 = new Element[ dimArray ];
    Element[] dataSped = new Element[ dimArray ];
    Element[] codFarmacia = new Element[ dimArray ];
    Element[] categoria = new Element[ dimArray ];
    Element[] mazzettaNumero = new Element[ dimArray ];
    Element[] mazzettaProgressivo = new Element[ dimArray ];
    Element[] impoLordo = new Element[ dimArray ];
    Element[] impoTicket = new Element[ dimArray ];
    Element[] impoNetto = new Element[ dimArray ];
    Element[] impoGalenici = new Element[ dimArray ];

    while( (stringaLetta = buff.readLine())!= null ){
    sottoNodo[j] = new Element("DATI");
    tipologia[j] = new Element("TIPOLOGIA");
    regione[j] = new Element("REGIONE");
    asl[j] = new Element("ASL");
    distretto[j] = new Element("DISTRETTO");
    riferimento[j] = new Element("RIFERIMENTO");
    identificativoRicetta[j] = new Element("IDENTIFICATIVO_RICETTA");
    codFisc[j] = new Element("CODICE_FISCALE_ASSISTITO");
    nomeAssistito[j] = new Element("NOME_ASSISTITO");
    cognomeAssistito[j] = new Element("COGNOME_ASSISTITO");
    indirizzoAssistito[j] = new Element("INDIRIZZO_ASSISTITO");
    etaAssistito[j] = new Element("ETA_ASSISTITO");
    sessoAssistito[j] = new Element("SESSO_ASSISTITO");
    nonSignificativo1[j] = new Element("NON_SIGNIFICATIVO1");
    esenzione[j] = new Element("ESENZIONE");
    numeroTotPezzi[j] = new Element("NUMERO_TOT_PEZZI");
    dataPresc[j] = new Element("DATA_PRESCRIZIONE");
    prescrittore[j] = new Element("PRESCRITTORE");
    nonSignificativo2[j] = new Element("NON_SIGNIFICATIVO2");
    codiceFustella1[j] = new Element("CODICE_FUSTELLA1");
    codiceFustella2[j] = new Element("CODICE_FUSTELLA2");
    codiceFustella3[j] = new Element("CODICE_FUSTELLA3");
    codiceFustella4[j] = new Element("CODICE_FUSTELLA4");
    codiceFustella5[j] = new Element("CODICE_FUSTELLA5");
    codiceFustella6[j] = new Element("CODICE_FUSTELLA6");
    dataSped[j] = new Element("DATA_SPEDIZIONE");
    codFarmacia[j] = new Element("CODICE_FARMACIA");
    categoria[j] = new Element("CATEGORIA");
    mazzettaNumero[j] = new Element("MAZZETTA_NUMERO");
    mazzettaProgressivo[j] = new Element("MAZZETTA_PROGRESSIVO");
    impoLordo[j] = new Element("IMPORTO_LORDO_RIC");
    impoTicket[j] = new Element("IMPORTO_TICKET");
    impoNetto[j] = new Element("IMPORTO_NETTO_RIC");
    impoGalenici[j] = new Element("IMPORTO_GALENICI");

    tipologia[j].addContent( stringaLetta.substring(0,3) );
    regione[j].addContent( stringaLetta.substring(3,6) );
    asl[j].addContent( stringaLetta.substring(6,9) );
    distretto[j].addContent( stringaLetta.substring(9,12) );
    riferimento[j].addContent( stringaLetta.substring(12,18) );
    identificativoRicetta[j].addContent( stringaLetta.substring(18,34) );
    codFisc[j].addContent( stringaLetta.substring(34,50) );
    nomeAssistito[j].addContent( stringaLetta.substring(80,100) );
    cognomeAssistito[j].addContent( stringaLetta.substring(50,80) );
    indirizzoAssistito[j].addContent( stringaLetta.substring(103,133) );
    etaAssistito[j].addContent( stringaLetta.substring(100,103) );
    sessoAssistito[j].addContent( stringaLetta.substring(133,134) );
    nonSignificativo1[j].addContent( stringaLetta.substring(134,144) );
    esenzione[j].addContent( stringaLetta.substring(144,145) );
    numeroTotPezzi[j].addContent( stringaLetta.substring(145,148) );
    dataPresc[j].addContent( stringaLetta.substring(148,156) );
    prescrittore[j].addContent( stringaLetta.substring(156,162) );
    nonSignificativo2[j].addContent( stringaLetta.substring(162,163) );
    codiceFustella1[j].addContent( stringaLetta.substring( 163,172 ) );
    codiceFustella2[j].addContent( stringaLetta.substring(172,181) );
    codiceFustella3[j].addContent( stringaLetta.substring(181,190) );
    codiceFustella4[j].addContent( stringaLetta.substring(190,199) );
    codiceFustella5[j].addContent( stringaLetta.substring(199,208) );
    codiceFustella6[j].addContent( stringaLetta.substring(208,217) );
    dataSped[j].addContent( stringaLetta.substring(217,225) );
    codFarmacia[j].addContent( stringaLetta.substring(225,231) );
    categoria[j].addContent( stringaLetta.substring(231,232) );
    mazzettaNumero[j].addContent( stringaLetta.substring(232,235) );
    mazzettaProgressivo[j].addContent( stringaLetta.substring(235,240) );
    impoLordo[j].addContent( stringaLetta.substring(240,249) );
    impoTicket[j].addContent( stringaLetta.substring(249,258) );
    impoNetto[j].addContent( stringaLetta.substring(258,267) );
    impoGalenici[j].addContent( stringaLetta.substring(267,276) );

    sottoNodo[j].addContent(tipologia[j]);
    sottoNodo[j].addContent(regione[j]);
    sottoNodo[j].addContent(asl[j]);
    sottoNodo[j].addContent(distretto[j]);
    sottoNodo[j].addContent(riferimento[j]);
    sottoNodo[j].addContent(identificativoRicetta[j]);
    sottoNodo[j].addContent(codFisc[j]);
    sottoNodo[j].addContent(nomeAssistito[j]);
    sottoNodo[j].addContent(cognomeAssistito[j]);
    sottoNodo[j].addContent(indirizzoAssistito[j]);
    sottoNodo[j].addContent(etaAssistito[j]);
    sottoNodo[j].addContent(sessoAssistito[j]);
    sottoNodo[j].addContent(nonSignificativo1[j]);
    sottoNodo[j].addContent(esenzione[j]);
    sottoNodo[j].addContent(numeroTotPezzi[j]);
    sottoNodo[j].addContent(dataPresc[j]);
    sottoNodo[j].addContent(prescrittore[j]);
    sottoNodo[j].addContent(nonSignificativo2[j]);
    sottoNodo[j].addContent(codiceFustella1[j]);
    sottoNodo[j].addContent(codiceFustella2[j]);
    sottoNodo[j].addContent(codiceFustella3[j]);
    sottoNodo[j].addContent(codiceFustella4[j]);
    sottoNodo[j].addContent(codiceFustella5[j]);
    sottoNodo[j].addContent(codiceFustella6[j]);
    sottoNodo[j].addContent(dataSped[j]);
    sottoNodo[j].addContent(codFarmacia[j]);
    sottoNodo[j].addContent(categoria[j]);
    sottoNodo[j].addContent(mazzettaNumero[j]);
    sottoNodo[j].addContent(mazzettaProgressivo[j]);
    sottoNodo[j].addContent(impoLordo[j]);
    sottoNodo[j].addContent(impoTicket[j]);
    sottoNodo[j].addContent(impoNetto[j]);
    sottoNodo[j].addContent(impoGalenici[j]);
    root.addContent(sottoNodo[j]);
    j++;
    }
    }catch( IOException io ){
    io.printStackTrace();
    }
    BufferedWriter out = null;
    try{
    out = new BufferedWriter(new FileWriter(path));

    /*ISTANZIAMO LA CLASSE XMLOutputter
    CHE CI PERMETTE DI FORMATTARE IL
    FILE XML.
    */

    XMLOutputter xout = new XMLOutputter();
    xout.setIndent(true);
    xout.setNewlines(true);

    /*CON QUESTA ISTRUZIONE SCRIVIAMO
    IL DOCUMENTO documento IN UN
    BufferedWriter CHE CREA IL FILE
    TRAMITE UN FileWriter*/

    xout.output(documento,out);
    }catch (IOException e){
    System.err.println ("errore: "+e);
    }finally{
    try{
    out.close();
    }catch(IOException e){
    System.err.println ("errore: "+e);
    }
    }
    }
    public static void main(String[] args){
    ConvertimiBene c = new ConvertimiBene();
    c.scriviXml("C:\\WINDOWS\\Desktop\\parser\\02102_0 82003.xml");
    }
    }


    Come si nota dal codice, però, questa tecnica da luogo ad un grosso dispendio di memoria... qualcuno sa se esiste un altro parser xml in grado di poter dare una cosa simile a questa e, quindi, un file .xml ben formattato.
    Grazie a tutti e ciao
    Se vuoi trovare l'arcobaleno, devi sopportare la pioggia

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 © 2024 vBulletin Solutions, Inc. All rights reserved.