A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
JavaRanch
»
Java Forums
»
Java
»
Java in General
Author
Write enter/indent with java inside xml file
Hesham Gneady
Ranch Hand
Joined: Feb 26, 2007
Posts: 66
posted
Nov 07, 2009 04:22:48
0
Hello Ranchers ,
I am trying to edit an xml file using
java
, my only problem is that all data written appears in the xml file in 1 line.
<library> <category name="tragedy"> <book>book1 name</book> <book>book2 name</book> </category> </library> // when i want to write the above xml it appears without enters & indents like this : <library> <category name="tragedy"><book>book1 name</book><book>book2 name</book></category> </library>
Here is my java code :
Document libraryDomObject = settingsController.getXMLFile( libraryXML ); // Load the xml file. Node libraryNode = libraryDomObject.getDocumentElement(); // Get the <library> node. Element newCategoryElement = libraryDomObject.createElement( "category" ); // Create the category node. newCategoryElement.setAttribute( "name", "tragedy" ); categoriesNode.appendChild( newCategoryElement ); // I want to insert an enter + an indent here ..................... ?? Element newBookElement = libraryDomObject.createElement( "book" ); // Create a book node. newBookElement.appendChild( libraryDomObject.createTextNode( "book1 name" ) ); newCategoryElement.appendChild( newBookElement );
I saw some answers saying i can do this using the Transformer class.
Do i have to change my code now, or is there a way i can do this here ?
Thanks ,
Hesham
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35224
7
posted
Nov 07, 2009 04:31:48
0
Check out the second example here:
http://faq.javaranch.com/java/HowToPrettyPrintXmlWithJava
Android apps
–
ImageJ plugins
–
Java web charts
Hesham Gneady
Ranch Hand
Joined: Feb 26, 2007
Posts: 66
posted
Nov 07, 2009 05:09:14
0
Thanks a lot Ulf ... That was quite clear.
I agree. Here's the link:
http://aspose.com/file-tools
subject: Write enter/indent with java inside xml file
Similar Threads
XML Scripting is not working
Is my approach right while inserting a foreign key?
Problem applying an XPath expression to an org.w3c.dom.Node
'new' operator vs updating database
append xml using xslt
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter