wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes How to update/insert/delete an element from  an XML document Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to update/insert/delete an element from  an XML document" Watch "How to update/insert/delete an element from  an XML document" New topic
Author

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

Ajan Balakrishnan
Ranch Hand

Joined: Jan 04, 2001
Posts: 71
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

Joined: Jan 04, 2001
Posts: 71
Any luck for me guys
Laurent Kempeneers
Greenhorn

Joined: May 21, 2001
Posts: 3
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

Joined: Jan 04, 2001
Posts: 71
Thanks a lot Laurent
ravi_r
Greenhorn

Joined: Jul 25, 2001
Posts: 1
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?
 
GeeCON Prague 2014
 
subject: How to update/insert/delete an element from an XML document