Ciao a tutti, ho un problema: sto creando un software per modificare un documento XML.
Il codice che dovrebbe aggiungere al documento questi tag:
<entry>
<data></data>
<head></head>
<body></body>
</entry>
in questa posizione:
<og> (questo è il root)
<news>
----QUI LA NUOVA ENTRY-----
----altre entry----
</news>
---altri tag----
</og>
è il seguente:
String xmlFile = "db.xml";
DOMParser parser = new DOMParser();
try {
parser.parse(xmlFile);
} catch (SAXException e) { e.printStackTrace(); }
catch (IOException ee) { ee.printStackTrace(); }
Document doc = parser.getDocument();
Node news=doc.getDocumentElement().getFirstChild().getN extSibling();
Element entry=doc.createElement("entry");
news.appendChild(entry);
news.insertBefore(entry, news.getFirstChild());
Date oggi=new Date();
DateFormat dateGenerator=DateFormat.getDateInstance();
String dataDiOggi=dateGenerator.format(oggi);
Element data=doc.createElement("data");
data.appendChild(doc.createTextNode(dataDiOggi));
entry.appendChild(data);
Element head=doc.createElement("head");
head.appendChild(doc.createTextNode(testa.getText( )));
entry.appendChild(head);
Element body=doc.createElement("body");
body.appendChild(doc.createTextNode(corpo.getText( )));
entry.appendChild(body);
System.out.print(doc.getElementsByTagName("data"). item(0).getFirstChild().getNodeValue());
A questo punto viene stampato un valore corretto, ma se ripeto il parsing e ci riprovo non accade...
Sapete dirmi come posso fare per favore?
Edit: intendevo dire che se riprovo a stampare i valori senza prima impostarli questi risultano errati.
Controllando il documento XML lo trovo invariato rispetto a prima dell'aggiunta dei nuovi elementi

Rispondi quotando

