PDA

View Full Version : [JAVA+XML] Lettura del valore dei nodi


Xfree
21-07-2011, 12:16
Ciao a tutti.
Vorrei riuscire a stampare a video tutti i nodi, a partire da un determinato nodo, ed il relativo valore, da un file XML così formato


<?xml version="1.0" encoding="UTF-8"?>
<ResultSet version="1.0">
<Error>0</Error>
<ErrorMessage>Nessun errore</ErrorMessage>
<Locale>it_IT</Locale>
<Quality>87</Quality>
<Found>1</Found>
<Result>
<quality>72</quality>
<latitude>38.095540</latitude>
<longitude>13.463119</longitude>
<offsetlat>38.095540</offsetlat>
<offsetlon>13.463119</offsetlon>
<radius>500</radius>
<name></name>
<line1>contrada Badia</line1>
<line2>90010 Ficarazzi PA</line2>
<line3></line3>
<line4>Italia</line4>
<house></house>
<street>contrada Badia</street>
<xstreet></xstreet>
<unittype></unittype>
<unit></unit>
<postal>90010</postal>
<neighborhood></neighborhood>
<city>Ficarazzi</city>
<county>Palermo</county>
<state>Sicilia</state>
<country>Italia</country>
<countrycode>IT</countrycode>
<statecode></statecode>
<countycode>PA</countycode>
<uzip>90010</uzip>
<hash></hash>
<woeid>12847749</woeid>
<woetype>11</woetype>
</Result>
</ResultSet>

In particolare mi interessano i tutti i nodi ed i relativi valori compresi tra <Result> e </Result>
Ho scritto in java il seguente programma che dovrebbe occuparsi del parsing del file XML

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

public class xml_2 {
public static void main (String [] args) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
Document doc = factory.newDocumentBuilder().parse(new File("file.xml"));

NodeList list = doc.getElementsByTagName("Result");
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
NodeList children = node.getChildNodes();
for (int j = 0; j < children.getLength(); j++) {
Element el = (Element)children.item(j);
System.out.println(el.getNodeName()+" : "+children.item(j).getNodeValue());
}
}
} catch (Exception e) {
}
}
}


Il problema è che ottengo tutti i nodi figli di Result, però con valore null, come se non riuscissi a prenderne il valore.

quality : null
latitude : null
longitude : null
offsetlat : null
offsetlon : null
radius : null
name : null
line1 : null
line2 : null
line3 : null
line4 : null
house : null
street : null
xstreet : null
unittype : null
unit : null
postal : null
neighborhood : null
city : null
county : null
state : null
country : null
countrycode : null
statecode : null
countycode : null
uzip : null
hash : null
woeid : null
woetype : null

Dove sto sbagliando?

banryu79
21-07-2011, 15:15
Usa il metodo getTextContent al posto di getNodeValue.

...
for (int j = 0; j < children.getLength(); j++) {
Node n = children.item(j);
if (n.getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element)n;
System.out.println(e.getNodeName()+" : "+e.getTextContent());
}
}
...


quality : 72
latitude : 38.095540
longitude : 13.463119
offsetlat : 38.095540
offsetlon : 13.463119
radius : 500
name :
line1 : contrada Badia
line2 : 90010 Ficarazzi PA
line3 :
line4 : Italia
house :
street : contrada Badia
xstreet :
unittype :
unit :
postal : 90010
neighborhood :
city : Ficarazzi
county : Palermo
state : Sicilia
country : Italia
countrycode : IT
statecode :
countycode : PA
uzip : 90010
hash :
woeid : 12847749
woetype : 11

Xfree
21-07-2011, 16:16
Grazie mille. :)

Xfree
21-07-2011, 17:56
Posso chiederti un'altra cosa?
Per visualizzare il valore di un determinato tag, come si fa?
Ho cercato e mi è sembrato si potesse fare così


...
for (int j = 0; j < children.getLength(); j++) {
Node n = children.item(j);
if (n.getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element) children.item(j);
String street = e.getElementsByTagName("street").item(0).getTextContent();
System.out.println(street);
}
}
...


Però non ottengo nulla.

banryu79
22-07-2011, 08:40
Non ottieni nulla perchè parti dal nodo sbagliato, nel tuo codice invochi getElementsByTagName direttamente sul nodo "street", cercando un figlio di nome "street", ecco perchè.
Ecco un esempio:

...
public static void main (String [] args) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
Document doc = factory.newDocumentBuilder().parse(new File("fileXML.xml"));

NodeList resultList = doc.getElementsByTagName("Result");
for (int i = 0; i < resultList.getLength(); i++) {
Node result = resultList.item(i);
printNodesByType(result, Node.ELEMENT_NODE);
System.out.println("---------------");
printNodeByName(result, "street");
printNodeByName(result, "city");
printNodeByName(result, "county");
}
} catch (Exception e) {
System.out.println("Something wrong...");
e.printStackTrace();
}
}

private static void printNodesByType(Node parent, short nodeType) {
NodeList children = parent.getChildNodes();
for (int j = 0; j < children.getLength(); j++) {
Node n = children.item(j);
if (n.getNodeType() == nodeType) {
Element e = (Element)n;
System.out.println(e.getNodeName()+" : "+e.getTextContent());
}
}
}

private static void printNodeByName(Node parent, String childName) {
Element e = (Element)parent;
NodeList list = e.getElementsByTagName(childName);
if (list.getLength() > 0) {
System.out.println(childName+" : "+list.item(0).getTextContent());
}
}

Xfree
22-07-2011, 11:51
Quindi avrei dovuto invocarlo nel ciclo principale e non nel ciclo innestato.
Grazie mille. :)