Visualizzazione dei risultati da 1 a 10 su 10

Discussione: estrazione da file xml

  1. #1
    Utente di HTML.it
    Registrato dal
    Nov 2009
    Messaggi
    142

    estrazione da file xml

    ciao!!!
    ho un file xml che ha la seguente struttura

    <virtual-sensor name="MultiFormatTemperatureHandler" priority="10">
    <processing-class>
    <class-name>gsn.vsensor.BridgeVirtualSensor</class-name>
    <init-params />
    <output-structure>
    <field name="light" type="double" />
    <field name="temperature" type="double" />
    <field name="packet_type" type="double" />
    </output-structure>
    </processing-class>
    <description>This sensor simulates light and temperature readings
    every one second.
    </description>
    <life-cycle pool-size="10" />
    <addressing>
    <predicate key="geographical">Sensor 114 @ EPFL</predicate>
    <predicate key="LATITUDE">46.520000</predicate>
    <predicate key="LONGITUDE">6.565000</predicate>
    </addressing>
    <storage history-size="5m" />
    <streams>
    <stream name="input1">
    <source alias="source1" sampling-rate="1" storage-size="1">
    <address wrapper="multiformat">

    </address>
    <query>SELECT light, temperature, packet_type, timed FROM wrapper</query>
    </source>
    <query>SELECT light, temperature, packet_type, timed FROM source1</query>
    </stream>
    </streams>
    </virtual-sensor>

    sto sviluppando un programma java che estragga i campi <description>,<addressing> e fin qui tutto bene.. il problema è che nn riesco a prendere i campi del tag <field name="light" type="double" />...dovrei prendere l atributo light,temperature e packet_type...qualcuno sa come si fa?sto usando le librerie dom

  2. #2
    Utente di HTML.it
    Registrato dal
    Nov 2009
    Messaggi
    142
    questo è il codice java che sto provando a sviluppare

    import java.io.File;
    import java.io.IOException;
    import java.util.Date;

    import org.w3c.dom.*;
    import javax.xml.parsers.*;

    public class file_output {

    public static void main( String [] args) throws IOException{
    //create an object that is a directory
    File mydir= new File("C:\\Users\\andrea\\workspace\\gsn\\virtual-sensors");
    DocumentBuilderFactory factory;
    DocumentBuilder parser;
    Document document;
    //get the content of the directory
    File [] contents = mydir.listFiles();
    //list the contents of the directory
    if (contents != null){

    for (File file :contents){
    if (file.isDirectory()==false){

    System.out.println (" "+file.getName());
    try {
    factory = DocumentBuilderFactory.newInstance();
    parser = factory.newDocumentBuilder();
    document = parser.parse(new File(file.getAbsolutePath()));
    handleDocument(document);
    } catch(Exception ex) {
    System.out.println("Errore.");
    ex.printStackTrace();
    }

    }//end for
    }

    }else{
    System.out.println(mydir.getName()+ " is not a directory");
    }

    System.exit(0);

    } //end of main

    private static void handleDocument(Document document) {
    /* Tutti i nodi contenuti in document che si chiamano "libro" */
    String risultato = null;
    NodeList doc_xml = document.getElementsByTagName("virtual-sensor");
    for(int i = 0; i < doc_xml.getLength(); i++) {
    Element data = (Element)doc_xml.item(i);
    String Descriptor = data.getElementsByTagName("description").item(0).g etTextContent();
    String table_name = data.getAttribute("name");

    NamedNodeMap output= data.getElementsByTagName("field").item(0).getAttr ibutes();
    Node res =output.getNamedItem("name");
    risultato=res.getNodeValue();
    String Cord = data.getElementsByTagName("addressing").item(0).ge tTextContent();
    System.out.println("Table: "+table_name);
    System.out.println("Descriptor: " + Descriptor);
    System.out.println("OutputStructure: " + risultato);
    System.out.println("Coordinate: " + Cord);
    }
    }
    }

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

    Re: estrazione da file xml

    Originariamente inviato da knuckles
    il problema è che nn riesco a prendere i campi del tag <field name="light" type="double" />...dovrei prendere l atributo light,temperature e packet_type...qualcuno sa come si fa?sto usando le librerie dom
    Sì, stai usando il DOM secondo le specifiche W3C.
    Dato un Element e sapendo il nome di un attributo, ne prendi il valore con element.getAttribute(nomeAttributo). Tutto qui.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    Java Versions Cheat Sheet

  4. #4
    devi prendere prima "output-structure" e poi cicli i figli e ne prendi gli attributi

    codice:
    NodeList internalList = data.getElementsByTagName("output-structure").item(0).getChildNodes();
    for(int i =0 ;i<=internalList.getLength()-1;i++){
        Node field= ( Node )internalList.item(i);
        ecc ecc
    }

  5. #5
    Utente di HTML.it
    Registrato dal
    Nov 2009
    Messaggi
    142
    OOOKKKKKK GRAZIE MILLE!!!!!!MI SIETE SEMPRE DI GRANDE AIUTO!!!!!

  6. #6
    Utente di HTML.it
    Registrato dal
    Nov 2009
    Messaggi
    142
    ciao!scusa se chiedo ancora aiuto ma ho cambiato il codice ma mi da errore dopo un po....i campi li prende e li visualizza ma poi mi da errore....posto il codice della funzione e l errore...

    private static void handleDocument(Document document) {
    /* Tutti i nodi contenuti in document che si chiamano "libro" */
    String risultato = null;
    NodeList doc_xml = document.getElementsByTagName("virtual-sensor");
    for(int i = 0; i < doc_xml.getLength(); i++) {
    Element data = (Element)doc_xml.item(i);
    String Descriptor = data.getElementsByTagName("description").item(0).g etTextContent();
    String table_name = data.getAttribute("name");
    NodeList internalList = data.getElementsByTagName("output-structure").item(0).getChildNodes();
    for(int j =0 ;j<=internalList.getLength()-1;j++){
    Node field= ( Node )internalList.item(i);

    NamedNodeMap output= data.getElementsByTagName("field").item(j).getAttr ibutes();
    Node res =output.getNamedItem("name");
    risultato=res.getNodeValue();
    System.out.println("out: "+risultato);
    System.out.println("j: "+j);
    }

    //NamedNodeMap output= data.getElementsByTagName("field").item(0).getAttr ibutes();
    //Node res =output.getNamedItem("name");
    //risultato=res.getNodeValue();
    String Cord = data.getElementsByTagName("addressing").item(0).ge tTextContent();
    System.out.println("Table: "+table_name);
    System.out.println("Descriptor: " + Descriptor);
    System.out.println("OutputStructure: " + risultato);
    System.out.println("Coordinate: " + Cord);
    }
    }


    l errore che mi da è questo:

    Errore.
    java.lang.NullPointerException
    at file_output.handleDocument(file_output.java:59)
    at file_output.main(file_output.java:30)

    bha nn capisco proprio:-)

  7. #7
    internalList.item(i); ????

    Stai ciclando con l'indice sbagliato?

  8. #8
    un altra cosa
    il codice va messo dentro i tag code
    cosi:
    codice:
    private static void handleDocument(Document document) {
    /* Tutti i nodi contenuti in document che si chiamano "libro" */
    String risultato = null;
    NodeList doc_xml = document.getElementsByTagName("virtual-sensor");
    for(int i = 0; i < doc_xml.getLength(); i++) {
    Element data = (Element)doc_xml.item(i);
    String Descriptor = data.getElementsByTagName("description").item(0).getTextContent();
    String table_name = data.getAttribute("name");
    NodeList internalList = data.getElementsByTagName("output-structure").item(0).getChildNodes();
    for(int j =0 ;j<=internalList.getLength()-1;j++){
    Node field= ( Node )internalList.item(i);
    
    NamedNodeMap output= data.getElementsByTagName("field").item(j).getAttributes();
    Node res =output.getNamedItem("name");
    risultato=res.getNodeValue();
    System.out.println("out: "+risultato);
    System.out.println("j: "+j);
    }
    
    //NamedNodeMap output= data.getElementsByTagName("field").item(0).getAttributes();
    //Node res =output.getNamedItem("name");
    //risultato=res.getNodeValue();
    String Cord = data.getElementsByTagName("addressing").item(0).getTextContent();
    System.out.println("Table: "+table_name);
    System.out.println("Descriptor: " + Descriptor);
    System.out.println("OutputStructure: " + risultato);
    System.out.println("Coordinate: " + Cord);
    }
    }
    prima di tutto è più chiaro, poi gli amministratori non ti chiudono il thread

  9. #9
    questo è il codice corretto

    codice:
    private static void handleDocument(Document document) {
    		/* Tutti i nodi contenuti in document che si chiamano "libro" */
    		String risultato = null;
    		NodeList doc_xml = document.getElementsByTagName("virtual-sensor");
    		for(int i = 0; i < doc_xml.getLength(); i++) {
    			Element data = (Element)doc_xml.item(i);
    			String Descriptor = data.getElementsByTagName("description").item(0).getTextContent();
    			String table_name = data.getAttribute("name");
    			NodeList internalList = data.getElementsByTagName("output-structure").item(0).getChildNodes();
    			for(int j =0 ;j<internalList.getLength();j++){
    				Node field= ( Node )internalList.item(j);
    				if(field.hasAttributes()){
    					NamedNodeMap output= field.getAttributes();
    					Node res =output.getNamedItem("name");
    					risultato=res.getNodeValue();
    					System.out.println("out: "+risultato);
    					System.out.println("j: "+j);
    				}
    			}
    
    //			NamedNodeMap output= data.getElementsByTagName("field").item(0).getAttributes();
    //			Node res =output.getNamedItem("name");
    //			risultato=res.getNodeValue();
    			String Cord = data.getElementsByTagName("addressing").item(0).getTextContent();
    			System.out.println("Table: "+table_name);
    			System.out.println("Descriptor: " + Descriptor);
    			System.out.println("OutputStructure: " + risultato);
    			System.out.println("Coordinate: " + Cord);
    		}
    	}

  10. #10
    Utente di HTML.it
    Registrato dal
    Nov 2009
    Messaggi
    142
    grazie mille!!!!!nn sapevo la storia del tag per il codice :-D....cmq ti ringrazio davvero tanto!!!!mi hai salvato :-)

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.