|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Aug 2001
Messaggi: 9538
|
[JAVA+XML] Lettura del valore dei nodi
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 Codice:
<?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>
Ho scritto in java il seguente programma che dovrebbe occuparsi del parsing del file XML Codice:
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) {
}
}
}
Codice:
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 |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Usa il metodo getTextContent al posto di getNodeValue.
Codice:
...
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());
}
}
...
Codice:
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
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Aug 2001
Messaggi: 9538
|
Grazie mille.
|
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Aug 2001
Messaggi: 9538
|
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ì Codice:
...
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);
}
}
...
|
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
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: Codice:
...
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());
}
}
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Aug 2001
Messaggi: 9538
|
Quindi avrei dovuto invocarlo nel ciclo principale e non nel ciclo innestato.
Grazie mille. |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 18:06.



















