Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within XML
Search Coderanch
Advance search
Google search
Register / Login
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
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
XML and Related Technologies
error while inserting a node into a dom using a java
s ithu
Greenhorn
Posts: 11
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hi guys,
i am trying to insert a node into a dom using a
java
method.
public static void insertXml(String dest,String s) throws ParserConfigurationException, IOException, SAXException, TransformerException { File file = new File(dest); Element root; if (file.exists()) { DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = fact.newDocumentBuilder(); doc = docBuilder.parse(file); root = doc.getDocumentElement(); String sr = root.getNodeName(); Element child1 = doc.createElement("fname"); child1.appendChild(child1); Text text1 = (Text) doc.createTextNode(s); child1.appendChild(text1); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); transformer.transform(source, result); String xmlString = sw.toString(); FileWriter fw=new FileWriter(file,true); BufferedWriter bw = new BufferedWriter(fw); bw.write(xmlString); bw.flush(); bw.close(); } else{ System.out.println("File not exists"); } }
But i get this error
hierarchy request_err : An attempt was made to insert a node where it is not permitted
so i changed permission using chmod command but still it is not working ......
Paul Clapham
Marshal
Posts: 28193
95
I like...
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
child1.appendChild(child1);
This was the line which threw the exception, right? Well, you can't make a node be its own child. Probably you meant for the node to be a child of something else.
s ithu
Greenhorn
Posts: 11
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thank you for your help
I changed code like this so it is working now.
root = doc.getDocumentElement(); Element child = doc.createElement("Row"); root.appendChild(child); Element child1 = doc.createElement(childNode); child.appendChild(child1); Text text1 = (Text) doc.createTextNode(s); child1.appendChild(text1);
Ruth Stout was famous for gardening naked. Just like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Problem in creating XML using DOM
Help regarding how to check the element entered is equal or not in xml file
problem in modification of the value in xml file
Xml result is not coming.
Java code to append data into a existing xml file
More...