[jdom-interest] Modify an xml thru JDOM

Edelson, Justin Justin.Edelson at mtvn.com
Wed Feb 15 08:03:16 PST 2006


In short...
 
// call the build method on a reader to produce a Document object. If you have a File, and URL, a String (via a StringReader), etc. use SAXBuilder.
// If you have a DOM object already parsed, use DOMBuilder.
Document doc = new SAXBuilder().build(new File("/path/to/your/file.xml"));
 
// find the specific Element you want to modify. this step could also be done by XPath
for (Iterator it = doc.getRootElement().getChildren("id").iterator; it.hasNext();) {
  Element el = (Element) it.next();
  if (element.getAttributeValue("art").equals("293")) {
 
     // do some modification, like element.setAttribute("done", "true");
     break;
  }
}
 
// serialize the Document object to a String. You can output to a File, an OutputStream, a String etc. through XMLOutputter.
//To output to DOM, use DOMOutputter and to output to a SAX ContentHandler, use SAXOutputter.
 
String des = new XMLOutputter().outputString(doc);
 

________________________________

From: jdom-interest-bounces at jdom.org on behalf of Roberto Marra
Sent: Wed 2/15/2006 10:31 AM
To: jdom-interest at jdom.org
Subject: [jdom-interest] Modify an xml thru JDOM


Hi guys Im new in this list & new with JDOM, so "hi" to everybody & thanx for your precious comments & helps.
 
My question is:
 
Let say that I got an XML file like this one:
 
<?xml version="1.0" encoding="ISO-8859-1"?>
<righeOrdine nrOrd="1">
 <id art="292">
  <sequenza>1.1</sequenza>
  <codice>110061</codice>
  <descrizione>PET FILM CORONA OUTSIDE</descrizione>
  <tipologia>PTR</tipologia>
  <trattamento>CR2</trattamento>
 </id>
 <id art="293">
  <sequenza>2.1</sequenza>
  <codice>110071</codice>
  <descrizione>PET PVDC COATED</descrizione>
  <tipologia>PTR</tipologia>
  <trattamento>PC1</trattamento>
 </id>
</righeOrdine>
 
 
what I would like to do is thru JDOM update just some value of the <id art="293">, let say I would update the "codice" from 110071 to 112071. Im sure is possible to do that, can you just suggest me some tips&tricks to do it? (of course using java)
 
Any help is appriciated
 
Cheers
Roberto
 


 
 
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.jdom.org/pipermail/jdom-interest/attachments/20060215/228a1a4d/attachment.htm


More information about the jdom-interest mailing list