This week's book giveaway is in the
Agile and other Processes
forum.
We're giving away four copies of
The Mikado Method
and have Ola Ellnestam and Daniel Brolund on-line!
See
this thread
for details.
A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
The Mikado Method
this week in the
Agile and other Processes
forum!
JavaRanch
»
Java Forums
»
Engineering
»
XML and Related Technologies
Author
Unable to create child elements.... XMl Parsing...
Jigar Naik
Ranch Hand
Joined: Dec 12, 2006
Posts: 744
posted
Mar 14, 2007 23:34:00
0
hi folloing is the structure of my XML file
XML file : bofore adding child node to root node.
<?xml version="1.0" encoding="UTF-8"?><blog create-date="2007-03-14 17:52:57" created-by="Amish Desai" title="unble to parse xml"> </blog>
Method to add child node to a parent node
public void create_xml(){ try{ DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); String c_date = dateFormat.format(date).toString(); parse_conn_xml(); String filename1=""; prop.load(connProp); Class.forName(connectionClass); Connection conn = DriverManager.getConnection(connectionName); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("Select max(id) from blog"); rs.next(); filename1 = rs.getString(1); String filename = "D:/usr/jboss/server/default/deploy/blog/" + filename1 + ".xml"; File file = new File(filename); file.createNewFile(); BufferedWriter out = new BufferedWriter(new FileWriter(filename)); out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); out.write("\r\n"); out.write("<blog>"); out.write("\r\n"); out.write("</blog>"); out.write("\r\n"); out.close(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new InputSource(filename)); doc.getDocumentElement().setAttribute("created-by",BlogBean.getCreated_by()); doc.getDocumentElement().setAttribute("title",BlogBean.getTitle()); doc.getDocumentElement().setAttribute("create-date",c_date); Node post = doc.createElement("post"); post.setTextContent(BlogBean.getPost()); [I]Element element = (Element)doc.getElementsByTagName("blog");[/I] [I][B]line no : 130[/B][/I] //Element element = (Element)doc.getElementsByTagName("blog"); //System.out.println("Node Name : " + element.getNodeName()); //element.appendChild(post); //doc.getParentNode().appendChild(post)); //Transftering s DOMSource ds = new DOMSource(doc); StreamResult sr = new StreamResult(new File(filename)); TransformerFactory tf = TransformerFactory.newInstance(); Transformer trans = tf.newTransformer(); trans.transform(ds,sr); }catch(Exception e){ System.out.println("*** EXCEPTION IN CREATE_XML METHOD ***"); e.printStackTrace(); } }
ERROR I'm getting :
10:56:53,375 INFO [STDOUT] *** EXCEPTION IN CREATE_XML METHOD *** 10:56:53,375 ERROR [STDERR] java.lang.ClassCastException: org.apache.xerces.dom.DeepNodeListImpl 10:56:53,375 ERROR [STDERR] at com.tg.utils.Blog.create_xml(Blog.java:130) 10:56:53,375 ERROR [STDERR] at com.tg.servlets.Controller.doPost(Controller.java:39) 10:56:53,375 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 10:56:53,375 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:810) 10:56:53,375 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252) 10:56:53,375 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) 10:56:53,375 ERROR [STDERR] at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96) 10:56:53,375 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202) 10:56:53,375 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) 10:56:53,375 ERROR [STDERR] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) 10:56:53,375 ERROR [STDERR] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178) 10:56:53,375 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175) 10:56:53,375 ERROR [STDERR] at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74) 10:56:53,375 ERROR [STDERR] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126) 10:56:53,375 ERROR [STDERR] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105) 10:56:53,375 ERROR [STDERR] at org.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156) 10:56:53,375 ERROR [STDERR] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107) 10:56:53,375 ERROR [STDERR] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) 10:56:53,375 ERROR [STDERR] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869) 10:56:53,375 ERROR [STDERR] at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664) 10:56:53,375 ERROR [STDERR] at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527) 10:56:53,375 ERROR [STDERR] at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112) 10:56:53,375 ERROR [STDERR] at java.lang.Thread.run(Thread.java:595)
Jigar Naik
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35252
7
posted
Mar 15, 2007 00:42:00
0
getElementsByTagName returns a
NodeList
, not an Element. I'm curious how that code can even compile.
If you're certain that the list only contains a single element, then something like
Element element = (Element) doc.getElementsByTagName("blog").item(0);
Android apps
–
ImageJ plugins
–
Java web charts
Jigar Naik
Ranch Hand
Joined: Dec 12, 2006
Posts: 744
posted
Mar 15, 2007 00:44:00
0
ya i did that and its working fine...
//Creating a DOM DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new InputSource(filename)); doc.getDocumentElement().setAttribute("created-by",BlogBean.getCreated_by());//Setting attribute to root node Blog doc.getDocumentElement().setAttribute("title",BlogBean.getTitle());//Setting attribute to root node Blog doc.getDocumentElement().setAttribute("create-date",c_date);//Setting attribute to root node Blog Element post = doc.createElement("post");//Created a new independent Node/Element post.setTextContent(BlogBean.getPost());//Adding a value to independent Node/Element post.setAttribute("posted-by",BlogBean.getCreated_by());//Adding attribute to independent element post.setAttribute("posted_on",c_date);//Adding attribues to independent element Element element = (Element)doc.getElementsByTagName("blog").item(0);//Getting to root node/element element.appendChild(post);//Adding the independent element/node to root node/element //Transformation DOMSource ds = new DOMSource(doc); StreamResult sr = new StreamResult(new File(filename)); TransformerFactory tf = TransformerFactory.newInstance(); Transformer trans = tf.newTransformer(); trans.transform(ds,sr);
Thanks....
I agree. Here's the link:
http://zeroturnaround.com/jrebel
- it saves me about five hours per week
subject: Unable to create child elements.... XMl Parsing...
Similar Threads
Unable to get the correct data from the XML file
Unable to create child elements.... XMl Parsing...
Java beginner- trying a DOM parser
dom problem
JRE 1.4.2 to 1.5 Migration issue in XML
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter