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?
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?