Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it L'avatar di blueice
    Registrato dal
    Feb 2001
    Messaggi
    121

    Parser Xml, valore null :-(

    Il metodo java che non mi legge il valore di un nodo è questo :

    codice:
        public void readFile(String fileName){
            try{
                if(_configDoc == null) {
                    parse(fileName);
                }
                
            NodeList restaurants = (NodeList)XPathAPI.selectNodeList(_configDoc.getDocumentElement(), RESTAURANT);
     
    
            for(int i=0; i<restaurants.getLength(); i++) {
                    Node pub = restaurants.item(i);
    
                    if(pub.hasChildNodes()) {
                        NodeList child = pub.getChildNodes();
                        System.out.println("CHILD : " + child.getLength());
                        for(int g = 0; g < child.getLength(); g++ ){
                            Node aNode = child.item(g);
     
                            if(aNode.getNodeType() != Node.TEXT_NODE){
                                System.out.println("Name : " + aNode.getNodeName() + " Value :  " + aNode.getNodeValue());
                            }
                         }
                    }
            } 
            
            
            }catch(Exception e){
                System.out.println("E : " + e.getMessage());
            }
        }
    mentre il codice xml è il seguente :
    codice:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <restaurants>
    	<restaurant>
    		<name value="Blossom Street Restaurant">1 Blossom Street Restaurant &amp; Bar</name>
    		<rid>677</rid>
    		<postcode>E1 6BX</postcode>
    		<link>http://www.toptable.co.uk/whitesite/...partnerid=9563</link>
    	</restaurant>
    	<restaurant>
    		<name>Zuccato City</name>
    		<rid>1300</rid>
    		<postcode>EC4M 9EB</postcode>
    		<link>http://www.toptable.co.uk/whitesite/...partnerid=9563</link>
    	</restaurant>
    </restaurants>
    Il problema è che ricevo sempre valore null dal metodo .getNodeValue e non riesco a capire qual'è il problema.

    Dovè che ho sbagliato ?

  2. #2
    non saprei...
    ma posso dirti di provare ad utilizzare questo metodo (opportunatamente modificato ai dati del tuo xml) http://www.javaportal.it/bv/jip.dll/...Type=EDITORIAL per leggere i dati xml...

  3. #3
    Utente di HTML.it L'avatar di blueice
    Registrato dal
    Feb 2001
    Messaggi
    121
    Grazie lo stesso, ho risolto!

    Vi posto il codice cosi potrebbe essere utile a qualche altra persona.

    codice:
        public void readFile(String fileName){
            try{
                if(_configDoc == null) {
                    parse(fileName);
                }
                
            NodeList restaurants = (NodeList)XPathAPI.selectNodeList(_configDoc.getDocumentElement(), RESTAURANT);
            
            System.out.println("Length " + restaurants.getLength());
            for(int i=0;i<restaurants.getLength();i++)  {
                Node node = restaurants.item(i);
                if(node.hasChildNodes())    {
                    NodeList child = node.getChildNodes();
                    for(int k=0;k<child.getLength();k++)    {
                        Node aNode = child.item(k);
                        if(aNode.getNodeName().equals(NAME) 
                        || aNode.getNodeName().equals(RID) 
                        || aNode.getNodeName().equals(POSTCODE) 
                        || aNode.getNodeName().equals(LINK)){
                            System.out.println("Name : " + aNode.getNodeName()+ " Value : " + getNodeValue(aNode));
                        }
                    }
                }
            }
            
            }catch(Exception e){
                System.out.println("E : " + e.getMessage());
            }
        }
    
        public String getNodeValue(Node node) {
            StringBuffer buf = new StringBuffer();
            NodeList children = node.getChildNodes();
            for (int i = 0; i < children.getLength(); i++) {
                Node textChild = children.item(i);
                if (textChild.getNodeType() != Node.TEXT_NODE) {
                    System.err.println("Mixed content! Skipping child element " + textChild.getNodeName());
                    continue;
                }
                
                buf.append(textChild.getNodeValue());
            }
            return buf.toString();
        }
    }

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.