File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Java in General
Write enter/indent with java inside xml file
Joined: Feb 26, 2007
Nov 07, 2009 04:22:48
Hello Ranchers ,
I am trying to edit an xml file using
, 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 ?
Joined: Mar 22, 2005
Nov 07, 2009 04:31:48
Check out the second example here:
Ping & DNS, my free Android app for network diagnostics
Joined: Feb 26, 2007
Nov 07, 2009 05:09:14
Thanks a lot Ulf ... That was quite clear.
It is sorta covered in the
JavaRanch Style Guide
subject: Write enter/indent with java inside xml file
XML Scripting is not working
Is my approach right while inserting a foreign key?
append xml using xslt
'new' operator vs updating database
Problem applying an XPath expression to an org.w3c.dom.Node
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2013