PDA

View Full Version : [Java] Leggere file Xml


GiulioCesare
15-03-2006, 09:33
Salve ragazzi, ho un problema nel leggere un file xml con java, purtroppo il mio file xml, ha la caratteristica che ha dei tag che hanno lo stesso nome, quindi quando passo al metodo il nome del tag, lui mi legge il primo che incontra. Per farmi capire meglio, il file xml è così strutturato:

<variabili>
<Titolo>
<p>Prova</p>
</Titolo>
<testo>
<p>Testo</p>
</testo>
</variabili>

Questo invece è il metodo che legge il file xml

public String getElement(String paramname)
{
String retparam = null;
try
{

Element docEle = dom.getDocumentElement();
NodeList nl = docEle.getElementsByTagName(paramname);
Element el = (Element)nl.item(0);
retparam = el.getFirstChild().getNodeValue();
}
catch(Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
return retparam;
}


Grazie a chi vorrà aiutarmi

sottovento
15-03-2006, 10:54
Stai usando un parser DOM, giusto?

Potresti fare un esempio di file xml un pochino piu' esteso? Nel file xml che hai messo, non ci sono tag con lo stesso nome...

Cmq nella tua procedura, invece della getElementsByTagName(paramname); potresti usare la getChildNodes ()

High Flying
Sottovento