Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ajan Balakrishnan
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
 
Laurent Kempeneers
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
 
ravi_r
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic