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.

[501 byte] By [java_player] at [2007-9-19]
# 1
You could parse the xml file into a DOM or JDOM object, These are objects that represent your XML file in memory. You can read / change the node values and write it back to a file again. http://www.jdom.org http://xml.apache.org/xerces-j/
samlewis23 at 2007-7-5 > top of java,Enterprise & Remote Computing,Enterprise Technologies...
# 2
Hi ,Thnx a lot for ur help. now i'm able to update my xml docs.rgds,java_player
java_player at 2007-7-5 > top of java,Enterprise & Remote Computing,Enterprise Technologies...
# 3
can u give me some examples?about update/delete/insert examplesthanx firstly
fltom at 2007-7-5 > top of java,Enterprise & Remote Computing,Enterprise Technologies...
# 4
I'd like some examples, too. Reading the Xml into a Document is easy, but how to write back into the file...?
grbrg at 2007-7-5 > top of java,Enterprise & Remote Computing,Enterprise Technologies...
# 5

Check out the samples.

If u are looking at JDOM check out samples\TestXMLOutputter.java

Or if u are looking at xalan/xerces DOM, take a look at the examples apps.

I would recomend JDOM, it is easier to use and it has approx 1/2 the mem footprint of the equivilent DOM Obj.

samlewis23 at 2007-7-5 > top of java,Enterprise & Remote Computing,Enterprise Technologies...
# 6

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

maheswarlenka at 2007-7-5 > top of java,Enterprise & Remote Computing,Enterprise Technologies...
# 7
above code is incorrect. DOMBuilder doesnt have a build() method which takes File object.Message was edited by: RBJ
RBJ at 2007-7-5 > top of java,Enterprise & Remote Computing,Enterprise Technologies...