How to update/change the value of elements in an xml file?
Hi Everyone,
Could any one of u tell me how to update the value of elements in an XML file, using java? The reason is i want to use an XML file as a data source (i.e. more or less like a database), without using any RDBMS, for simple applications such as to read a record and update the record. By the way, my XML file will have only one record, such as the current weather information, with fields such as temperature, humdity etc. for 1 city only.
Thanks in advance.
Here is a solution how to check a particular value or element name in an xml and update the changes e to an xml.
Sample.xml
<URLConstructor>
<application name="cp_outage">
<resource>hello</resource>
<value>val</value>
</application>
<application name="cp_outage">
<resource>hello</resource>
<value>val</value>
</application>
</URLConstructor>
XMLWriter.java
package com;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.DOMBuilder;
import org.jdom.output.XMLOutputter;
// used for printing
import org.apache.xml.serialize.XMLSerializer;
import org.jdom.output.XMLOutputter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
class XMLWriter{
public void update(File fileName)
{
try {
DOMBuilder domBuilder=new DOMBuilder();
Document doc=domBuilder.build(fileName);
Element element=doc.getRootElement();
getChildren(element);
writeToXML(doc,fileName);
getChildren(element);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param doc
*/
private void writeToXML(Document document,File filePath)
{
XMLOutputter xmloutputter = new XMLOutputter();
try
{
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
xmloutputter.output(document, fileOutputStream);
fileOutputStream.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void getChildren(Element element)
{
if(!element.hasChildren())
return;
List childrenList = element.getChildren();
Iterator itr=childrenList.iterator();
while(itr.hasNext())
{
Element childElement=(Element) itr.next();
if(childElement.hasChildren())
{
getChildren(childElement);
}
//System.out.println("Name"+childElement.getName());
//System.out.println("Value"+childElement.getText());
if(childElement.getText().equals("hello") || (childElement.getName().equals("resource")))
{
updateInfo(childElement,"New_Resource","AddedText");
}
}
}
/**
* @param childElement
* @param string
* @param string2
*/
private void updateInfo(Element element, String elementName, String value)
{
element.setName(elementName);
element.setText(value);
}
static public void main(String[] args)
{
XMLWriter xmlWriter=new XMLWriter();
xmlWriter.update(new File("c:/sample.xml"));
}
}
After execution the file will be changed to
<URLConstructor>
<application name="cp_outage">
<New_Resource>AddedText</New_Resource>
<value>val</value>
</application>
<application name="cp_outage">
<New_Resource>AddedText</New_Resource>
<value>val</value>
</application>
</URLConstructor>
Regards,
Maheswar