• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Modifying XML content using JDOM

 
Nirban Dutta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using JDOM for manipulating my external XML file from within JSP. For retrieving information, it is working fine, but while modifying the Text of an element, it is not chaning the text , also it is not showing any error.
I have used both SAXBuilder and DOMBuilder classes and tried to change the text of particular element like:
Element nm = pt.getChild("name");
//where pt is the parent element of element "name"
nm = nm.setText("New Name");
But its not changing the text of the element to "New Name".
Can anyone help me?
Thanx in advance
 
Nirban Dutta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hurrey , I have done it.
What I did, I modified the Document(ie JDOM Tree) and then transfer the changes to actual xml document.
The code is:
try
{
SAXBuilder sb = new SAXBuilder();
Document db = sb.build(new File("D:/weblogic/wlserver6.0/config/mydomain/applications/DefaultWebApp_myserver/jsp/hotel/myHotel.xml"));
Element rtel = db.getRootElement();
Element hotel = rtel.getChild("hotel");
Element general = hotel.getChild("general");
Element name = general.getChild("name");
name.setText(hotelname);
XMLOutputter xout = new XMLOutputter();
xout.output(db,(Writer)(new FileWriter(new File("D:/weblogic/wlserver6.0/config/mydomain/applications/DefaultWebApp_myserver/jsp/hotel/myHotel.xml"))));
}
Anybody, who is interested in JDOM, can use JDOM very easily, it is very easy as compared to XML4J.
Cheers
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic