Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    Stampa del nome e del valore dei nodi figli in DOM

    Ciao a tutti...

    Voglio parsare un documento XML con DOM...

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new File(args[0]));

    //Elemento radice:

    Element root= doc.getDocumentElement();
    System.out.println("Root node: "+root.getNodeName());
    NodeList children= root.getChildNodes();
    for (int i=0; i<mapRoot.getLength();i++)
    System.out.println(children.item(i).getNodeName()) ;

    Mettiamo che l'xml sia così:

    <squadra>
    <portiere>
    </portiere>
    <difensore>
    </difensore>
    <centrocampista>
    </centrocampista>
    <attaccante>
    </attaccante>
    </squadra>

    Ecco l'output:

    Root node: squadra
    #text
    portiere
    #text
    difensore
    #text
    centocampista
    #text
    attaccante

    Come faccio a togliere di mezzo qui "#text"?
    Se ho degli attributi che sono associati a un elemento, come faccio ad estrarre il nome di quegli attributi? E il loro valore?

    Grazie mille per le risposte.
    Aldo.

  2. #2
    Ho trovato la soluzione... grazie lo stesso...
    Ciao ciao.

  3. #3
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    postala, così qualcuno col tuo stesso problema si trova una traccia per la soluzione.
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  4. #4
    Parsing di un documento con SAX
    import java.io.File;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;

    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.NamedNodeMap;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.w3c.dom.Text;

    public class firstDOM {
    //ricorsiva, stampa di tutta l'alberatura dell'xml...
    public static void print(Node n, String spaces) {
    //base della ricorsione
    if (n == null) return;
    //non foglia
    if (n instanceof Element) {
    String s = spaces + n.getNodeName() + " ( ";
    NamedNodeMap map = n.getAttributes();
    if (map != null)
    for (int i = 0; i < map.getLength(); i++)
    s += map.item(i).getNodeName() + "=" + map.item(i).getNodeValue() +" ";
    s += ")";
    System.out.println(s);
    } else
    //foglia
    if (n instanceof Text)
    System.out.println(spaces + n.getNodeValue());
    NodeList children = n.getChildNodes();
    for (int i = 0; i < children.getLength(); i++)
    print(children.item(i), spaces );
    }

    public static void main(String[] args) {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
    builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new File(args[0]));
    Element root= doc.getDocumentElement();
    print(root,"");

    } catch (ParserConfigurationException e) {
    System.out.println("Error: "+e.fillInStackTrace());
    }catch (Exception e) {
    System.out.println("Error: "+e.fillInStackTrace());
    }
    }
    }



    Vi posto anche un semplice esempio di parsing di con SAX:

    import java.io.File;

    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;

    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.XMLReader;
    import org.xml.sax.helpers.DefaultHandler;

    public class firstSax extends DefaultHandler{
    public void startDocument()throws SAXException{
    System.out.println("Start Document...");
    }
    public void endDocument()throws SAXException {
    System.out.println("...End Document");
    }
    public void startElement(String namespaceURI, String lName, String qName,Attributes attrs)throws SAXException{
    System.out.println("Start Element: " + lName);
    System.out.println("Attributes" + lName);
    //Stampo gli attributi dell'elemento con il rispettivo valore, separati da un ":"
    for (int i=0; i<attrs.getLength(); i++)
    System.out.println(" " + attrs.getQName(i)+" : "+attrs.getValue(i));
    }
    public void endElement(String namespaceURI, String lName, String qName,Attributes attrs)throws SAXException{
    System.out.println("End Element: /" + lName);
    }

    public void characters(char ch[], int start, int length) throws SAXException {
    System.out.println("characters " + start + " to " +
    (start + length - 1) + ": " + new String(ch, start, length));
    }

    public static void main(String args[]) throws Exception {

    if(args.length!=1){
    System.out.println("Uso: java firstSax NomeFile.xml");
    System.exit(1);
    }
    try{
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser saxParser = spf.newSAXParser();
    XMLReader parser = saxParser.getXMLReader();
    //Creo l'handeler di default
    DefaultHandler handler = new firstSax();
    //Parso il documento... il nome del documento deve essere specificato nel prompt dei comandi...
    saxParser.parse(new File(args[0]),handler);
    }catch(Throwable t){
    t.fillInStackTrace();
    }

    }
    }


    Ciao a tutti, Aldo.

  5. #5
    Ovviamente il primo esempio usa DOM e il secondo SAX...
    Ciao ciao.

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.