Ciao a tutti!!
Ho il seguente file .xml
codice:
<?xml version="1.0" encoding="UTF-8"?>
<TODO_LIST>
<ITEM importanza="3" perc_completamento="75" completata="no">
<DESCR>Fare la spesa</DESCR>
</ITEM>
<ITEM importanza="7" perc_completamento="100" completata="si">
<DESCR>Stendere i panni</DESCR>
</ITEM>
<ITEM importanza="4" perc_completamento="0" completata="no">
<DESCR>Cucinare</DESCR>
</ITEM>
</TODO_LIST>
estrapolo correttamente i dati in questo modo:
codice:
public static void main(String[] args) throws Exception {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build("file.xml");
Iterator itr = doc.getRootElement().getChildren().iterator();
while (itr.hasNext()) {
Element elem = (Element) itr.next(); //ITEM
Element description = elem.getChild("DESCR"); // DESCR SOTTO ITEM
System.out.println("*" + description.getText());
System.out.println("\tImportanza: " + elem.getAttributeValue("importanza"));
System.out.println("\tCompletamento: " + elem.getAttributeValue("perc_completamento") + "%");
System.out.println("\tItem copmletata: " + elem.getAttributeValue("completata")+"\n");
}
}
Se modifico il file .xml in questo modo:
codice:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="0.92">
<TODO_LIST>
<ITEM importanza="3" perc_completamento="75" completata="no">
<DESCR>Fare la spesa</DESCR>
</ITEM>
<ITEM importanza="7" perc_completamento="100" completata="si">
<DESCR>Stendere i panni</DESCR>
</ITEM>
<ITEM importanza="4" perc_completamento="0" completata="no">
<DESCR>Cucinare</DESCR>
</ITEM>
</TODO_LIST>
</rss>
purtroppo la classe postata sopra genera un'eccezione e non funziona piu':
Exception in thread "main" java.lang.NullPointerException
at jdom.main(jdom.java:23)
sapete aiutarmi! grazie!