|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Apr 2004
Messaggi: 364
|
[Java] Leggere file Xml
Salve ragazzi, sto cercando di leggere un file xml, tramite java, ma ho dei problemi, ho fatto due metodi che leggono dalla radice del file, ed a secondo dell'element che viene passato leggono il relativo valore, però invece di ritornare il valore giusto, ritorna un valore nullo. Posto di seguito i due metodi che ho fatto, e poi il mio file xml da leggere, con la speranza che qualcuno mi aiuti
Codice:
public String getParam(String paramname)
{
//get the root elememt
Element docEle = dom.getDocumentElement();
String retparam = null;
NodeList nl = docEle.getElementsByTagName("radice");
if(nl != null && nl.getLength() > 0)
{
for(int i = 0 ; i < nl.getLength();i++)
{
Element el = (Element)nl.item(i);
retparam = getTextValue(el,paramname);
}
}
return retparam;
}
public String getTextValue(Element ele, String tagName)
{
String textVal = null;
NodeList nl = ele.getElementsByTagName(tagName);
if(nl != null && nl.getLength() > 0)
{
Element el = (Element)nl.item(0);
textVal = el.getFirstChild().getNodeValue();
}
if(textVal.equals(null)) textVal = "";
return textVal;
}
File XML
<?xml version="1.0" encoding="UTF-8"?>
<radice>
<driver_db>Valore</driver_db>
<url_db>Valore</url_db>
<user_db>Valore</user_db>
<path_dest_xml>Valore</path_dest_xml>
<tipo_db>Valore</tipo_db>
</radice>
|
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Nov 2005
Città: Texas
Messaggi: 1722
|
Ciao,
da quello che ho capito, il problema e' che dai per scontato che il primo nodo, sotto a quello che passi per parametro, sia il #text. Prova a controllarlo esplicitamente. Magari per prova, nella getTextValue(), stampali tutti. Immagino lo troverai facilmente High Flying Sottovento |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Feb 2001
Città: Kasteddu
Messaggi: 1890
|
Codice:
public String getTextValue(Element ele, String tagName)
{
String textVal = null;
NodeList nl = ele.getElementsByTagName(tagName);
if(nl != null && nl.getLength() > 0)
{
Element el = (Element)nl.item(0);
textVal = ((Text)el.getFirstChild()).getData();
}
if(textVal.equals(null)) textVal = "";
return textVal;
}
__________________
Asus P5Q-E - Intel Q6600 - Corsair 4GB DDR2 - Radeon HD5770 - Seagate 320GB sata2 16M - Audigy2 ZS Acer Aspire 3023 WLMI |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 08:13.



















