Visualizzazione dei risultati da 1 a 4 su 4

Discussione: leggere file xml

  1. #1
    Utente di HTML.it
    Registrato dal
    Nov 2008
    Messaggi
    123

    leggere file xml

    Ho il seguente file Xml e lo devo leggere tramite Java
    codice:
    <documento>
    	<curriculum>
    		<nome> Mario </nome>
    		<esperienze>
    			<esperienze_formative>
    				<esperienza_f id = "1"> D </esperienza_f>
    				<esperienza_f id = "2"> C </esperienza_f>
    			<esperienze_professionali>
    				<esperienza_p id = "1"> S</esperienza_p>
    			</esperienze_professionali>
    		</esperienze>
    	</curriculum>
    </documento>
    E' tutto ok, tranne quando arrivo alle esperienze e tutto ciò che contengono, e non so più come rappresentarle...
    fino a quel momento ho fatto il seguente codice, poi sono bloccato..

    codice:
    public static void main(String[] args) {
            try
            {
                File fXmlFile = new File(args[0]);
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(fXmlFile);
                doc.getDocumentElement().normalize();
                System.out.println("Root element:+doc.getDocumentElement().getNodeName());
                NodeList nList = doc.getElementsByTagName("curriculum");
                for(int temp=0; temp < nList.getLength(); temp++ )
                {
                    Node nNode = nList.item(temp);
                    if(nNode.getNodeType() == Node.ELEMENT_NODE)
                    {
                        Element eElements = (Element) nNode;
                        System.out.println(" nome : " +getTagValue("nome", eElements));
                    }
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    
        private static String getTagValue(String sTag, Element eElement)
        {
            NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
            Node nValue = (Node) nlList.item(0);
            return nValue.getNodeValue();
        }
    }

  2. #2
    Utente di HTML.it L'avatar di bstefano79
    Registrato dal
    Feb 2004
    Messaggi
    2,520
    il file xml non è ben formattato, manca la chiusura di esperienze_formative

  3. #3
    Utente di HTML.it
    Registrato dal
    Nov 2008
    Messaggi
    123
    si si...ho riportato solo un piccolo frammento di tutto il codice xml...io ce lho fatto bene...
    solo che non so come rappresentare le varie esperienze...ciè fino a quando si tratta di fare nome, cognome e cosi via non ho problemi ma poi non so come fare per rappresentare le esperienze e tutte le sue indentature...

  4. #4
    Utente di HTML.it L'avatar di Pastore12
    Registrato dal
    Oct 2008
    Messaggi
    1,051
    esperienze potrebbe essere un oggetto con due array: esperienze professionali e esperienze formative.

    Questo lo potresti realizzare creando 3 oggetti:
    - Esperienza (che ha solo due array di oggetti EsperienzaProfessionale e EsperienzaFormativa)
    - EsperienzaProfessionale
    - EsperienzaFormativa

    Gli ultimi due dovrebbero avere un campo "id" e un campo "value", ad esempio, dove memorizzare id e valore.
    "Ethics are to me something private. Whenever you use it as an argument for why somebody_else should do something, you’re no longer being ethical, you’re just being a sanctimonious dick-head"
    Linus Torvalds

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.