aspose file tools
The moose likes XML and Related Technologies and the fly likes Exception while replacing an XML tag in an XML file Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Engineering » XML and Related Technologies
Reply Bookmark "Exception while replacing an XML tag in an XML file" Watch "Exception while replacing an XML tag in an XML file" New topic
Author

Exception while replacing an XML tag in an XML file

Devarakonda Srikanth
Greenhorn

Joined: Aug 27, 2007
Posts: 25
Hi,

When I am trying to replace the existing XML tag with other one I am getting the following exception:

org.w3c.dom.DOMException: NOT_FOUND_ERR: An attempt is made to reference a node in a context where it does not exist.
at org.apache.xerces.dom.ParentNode.internalInsertBefore(Unknown Source)
at org.apache.xerces.dom.ParentNode.replaceChild(Unknown Source)
at org.apache.xerces.dom.CoreDocumentImpl.replaceChild(Unknown Source)
at com.Replace_Tag.replaceNode(Replace_Tag.java:97)
at com.Replace_Tag.processRequest(Replace_Tag.java:39)
at com.Replace_Tag.doGet(Replace_Tag.java:56)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)

The following is the code I am using :


code:
--------------------------------------------------------------------------------

public void replaceNode() throws Exception{ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse("file:/C:/Documents and Settings/srikanth.d.KNOAHSOFT/Desktop/Linechart.jrxml"); Element root = doc.getDocumentElement(); Element newElement = doc.createElement("stackedBarchart"); NodeList oldList = root.getElementsByTagName("lineChart"); int length = oldList.getLength(); System.out.println("length::"+length); for(int i=0;i<length;i++) { Node oldNode = oldList.item(i); NodeList nl = doc.getChildNodes(); newElement.getTagName().toString()); (i).getNodeName().toString()); doc.replaceChild((Node)newElement, oldNode); } }

--------------------------------------------------------------------------------





Can anybody help me in this regard...

Thanks and Regards
DNV Srikanth
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

This seems to be more XML related than Servlet related.
Moving to our XML forum.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Devarakonda Srikanth
Greenhorn

Joined: Aug 27, 2007
Posts: 25
can anybody help in this regard...
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12325
    
    1
The next time you need help with code, kindly format it so it can be read.

I suspect your problem is due to the oldNode and newNode not being in the same Document context. At least thats what the Java 1.5 JavaDocs for org.w3c.dom.Node indicate.

Bill


Java Resources at www.wbrogden.com
Devarakonda Srikanth
Greenhorn

Joined: Aug 27, 2007
Posts: 25
I am trying to do the above said task in the same document context... i am giving the content xml file also:
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16487
    
    2

I had to read the API documentation, to see what exceptions that method could throw and what they meant. And here's what it says your exception means:
NOT_FOUND_ERR: Raised if oldChild is not a child of this node.
So, you should be calling that method on the parent of the oldChild node and not on the document element.
Devarakonda Srikanth
Greenhorn

Joined: Aug 27, 2007
Posts: 25
Hi
I have done the corresponding changes to replace the node. Here I am giving the source code I have changed. When I execute another exception is being encountered. Here is the code:




and the exception is:

org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted.
at org.apache.xerces.dom.NodeImpl.replaceChild(Unknown Source)
at com.Replace_Tag.replaceNode(Replace_Tag.java:92)
at com.Replace_Tag.processRequest(Replace_Tag.java:39)
at com.Replace_Tag.doGet(Replace_Tag.java:57)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)


Can you please provide the solution for this...If the code written is wrong, can anyone provide a code snippet to replace an existing tag with another one in an XML file...

Thanks and Regards
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12325
    
    1
Node node = oldList.item(i).getFirstChild();


Tch! I suspect that first child is a Text node instead of the Element you expect. Naturally a Text node is not legal for the replaceChild operation.

Bill
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16487
    
    2

Actually this codeisn't any better because "node" isn't the parent of "node" either. I don't think you meant to do that, but your choice of meaningless variable names led you into that error.

Doesn't the DOM have a getParent() method? If so, go back to your original code and apply that method.
Devarakonda Srikanth
Greenhorn

Joined: Aug 27, 2007
Posts: 25
Thanq Ranchers its working :thumb:
Devarakonda Srikanth
Greenhorn

Joined: Aug 27, 2007
Posts: 25

[ August 29, 2007: Message edited by: Paul Clapham ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16487
    
    2

Glad it's working.

(We have smilies disabled by default in this forum because XML with namespaces turns into a mess of smilies. But I edited your last post to enable smilies, to reflect what you really meant.)

 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Exception while replacing an XML tag in an XML file
 
Similar Threads
ADB webservice - attributes and elements content
Exception while replacing an XML tag in an XML file
Getting ClassCastException
Exception while replacing the child Node
Unsupported JNDI Object