• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How to update/insert/delete an element from an XML document

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,
I have a sample XML document,

How to delete <service id=1> from <package id=1> and insert a new service, say <service id=4> within <package id=1> using a java program. I know how to read a node and stuff but not delete and insert. Any sample code is greatly appreciated

Thanks
AJan
 
Ajan Balakrishnan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any luck for me guys
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the best to do when you have to manipulate a DOM in memory and querying in it, is to work with XPath. With XPath you can reach easily the node you are looking for and after you can manipulate it.
This is an example for removing a node:
****
Node aNode = ... result of an XPath query ...
Node parentNode = aNode.getParentNode();
parent.removeChild(aNode);
****
This is an example of a function for retrieving a list of nodes belonging to an XPath expression. This example works with Xalan.
public NodeList findNode(Document doc, String query) throws SAXException {
// query = xpath expression
// doc = the DOM object representing the XML document you want manipulate.
XPath xPath = new XPath();
XPathSupport xpathSupport = new XMLParserLiaisonDefault();
XPathProcessorImpl parser = new XPathProcessorImpl(xpathSupport);
PrefixResolverDefault prefixResolver = new PrefixResolverDefault(doc.getDocumentElement());
parser.initXPath(xPath, query, prefixResolver);
XObject xObject = xPath.execute(xpathSupport, doc.getDocumentElement(), prefixResolver);

return xObject.nodeset();
}
 
Ajan Balakrishnan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Laurent
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Laurent Kempeneers:
I think the best to do when you have to manipulate a DOM in memory and querying in it, is to work with XPath. With XPath you can reach easily the node you are looking for and after you can manipulate it.
This is an example for removing a node:
****
Node aNode = ... result of an XPath query ...
Node parentNode = aNode.getParentNode();
parent.removeChild(aNode);
****
This is an example of a function for retrieving a list of nodes belonging to an XPath expression. This example works with Xalan.
public NodeList findNode(Document doc, String query) throws SAXException {
// query = xpath expression
// doc = the DOM object representing the XML document you want manipulate.
XPath xPath = new XPath();
XPathSupport xpathSupport = new XMLParserLiaisonDefault();
XPathProcessorImpl parser = new XPathProcessorImpl(xpathSupport);
PrefixResolverDefault prefixResolver = new PrefixResolverDefault(doc.getDocumentElement());
parser.initXPath(xPath, query, prefixResolver);
XObject xObject = xPath.execute(xpathSupport, doc.getDocumentElement(), prefixResolver);

return xObject.nodeset();
}


How to import the XPath and other related classes?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!