Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
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:
Java in General
Write enter/indent with java inside xml file
Hesham Gneady
Ranch Hand
Posts: 66
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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
Rancher
Posts: 43081
77
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Check out the second example here:
http://faq.javaranch.com/java/HowToPrettyPrintXmlWithJava
Hesham Gneady
Ranch Hand
Posts: 66
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks a lot Ulf ... That was quite clear.
Hesham
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
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
XML Scripting is not working
append xml using xslt
More...