|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Apr 2004
Messaggi: 364
|
[Java] Leggere file Xml
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 Codice:
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; } |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Nov 2005
Città: Texas
Messaggi: 1722
|
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 |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 16:22.