I'm trying to edit an XML file on Android and when I do the update, if the String that I'm adding to a node is greater than the one by default, my XML becomes corrupted.
If I have:
<Configuracion>
<CamposEditables>Empresa</CamposEditables>
</Configuracion>
and I add ", Web" to the node I get:
<Configuracion>
<CamposEditables>Empresa, Web</CamposEditables>
</Configura
I've added 5 chars to a node but my file keeps having the same name of chars so it takes 5 chars from the end of the file.
Here is my code:
File filexml = new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString() + "/configuracion.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(filexml);
doc.getDocumentElement().normalize();
updateElementValue(doc, camposeditables);
doc.getDocumentElement().normalize();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(filexml);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
and:
private static void updateElementValue(Document doc, String txt)
{
NodeList config = doc.getElementsByTagName("Configuracion");
Element emp = null;
for(int i=0; i<config.getLength();i++){
emp = (Element) config.item(i);
Node name = emp.getElementsByTagName("CamposEditables").item(0).getFirstChild();
name.setNodeValue(txt);
}
}
Thanks in advance for the help you can give me.
No comments:
Post a Comment