Salve a tutti.Ho fatto una funzione per la visita di un albero.In poche parole dopo aver costruito in memoria una DOM da un albero XML ho applicato la mia funzione all'albero.
Vi posto il codice e l'albero XML:
Adesso vi mostro il risultato della stampa :codice:Documento books.xml: <?xml version="1.0"?> <catalog> <book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> </book> <book id="bk102"> <author >Ralls, Kim</author> <title>Midnight Rain</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2000-12-16</publish_date> <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description> </book> </catalog> Funzione di parserizzazione : private void parse(String stFileName) throws ParseException { try { DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance(); DocumentBuilder constructeur = fabrique.newDocumentBuilder(); this.document = constructeur.parse(new File(stFileName)); Element root = this.document.getDocumentElement(); printNode(root); } catch (ParserConfigurationException e) { System.out.println(e.getMessage()); } catch (SAXException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } } Stampa dei nodi : public void printNode(Node nodep){ int ivar = 0; if(nodep.getNodeType() == Node.ELEMENT_NODE){ NodeList children = nodep.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { if(!((Node)children.item(i)).getTextContent().equals(" ")){ Node node = children.item(i); if(node.getNodeType() == Node.ELEMENT_NODE ){ System.out.println(" Il figlio di "+nodep.getNodeName()+" è "+((Node)children.item(i)).getNodeName()); printNode(node); } else if(node.getNodeType() == Node.TEXT_NODE & ivar == 0){ ivar++; System.out.println(" il suo valore è :"+node.getNodeValue()); } } } } }
Il problema è che come si vede dalla stampa dei nodi compare la scritta "Il mio valore è " senza il valore,questo è dovuto a quanto pare al fatto che ci sono dei TEXT_NODE(#text) che precedono i tag book che ovviamente non hanno valore..e per come è costruita la funzione il valore è vuoto.Come posso fare per ovviare a questo????codice:Il figlio di catalog è book il suo valore è : Il figlio di book è author il suo valore è :Gambardella, Matthew Il figlio di book è title il suo valore è :XML Developer's Guide Il figlio di book è genre il suo valore è :Computer Il figlio di book è price il suo valore è :44.95 Il figlio di book è publish_date il suo valore è :2000-10-01 Il figlio di book è description il suo valore è :An in-depth look at creating applications with XML. Il figlio di catalog è book il suo valore è : Il figlio di book è author il suo valore è :Ralls, Kim Il figlio di book è title il suo valore è :Midnight Rain Il figlio di book è genre il suo valore è :Fantasy Il figlio di book è price il suo valore è :5.95 Il figlio di book è publish_date il suo valore è :2000-12-16 Il figlio di book è description il suo valore è :A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.

Rispondi quotando