This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes how to remove spaces while updating xml and How to get  indenting? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "how to remove spaces while updating xml and How to get  indenting?" Watch "how to remove spaces while updating xml and How to get  indenting?" New topic
Author

how to remove spaces while updating xml and How to get indenting?

kesava chaitanya
Ranch Hand

Joined: Aug 15, 2001
Posts: 140
hi

i am able to update the xml using below code ;but i when i see the xml there is no indenting and some square blocks r coming in xml;how to remove those blocks and how to achieve indenting;i think these blocks r coming because of spaces in xml;

<?xml version = '1.0'?>

<India>
<IndianPlayer>
<Name>1111111</Name>
<Age>1111111111</Age>
</IndianPlayer><IndianPlayer> <Name>wrw</Name> <Age>rwrwr</Age> </IndianPlayer><IndianPlayer> <Name>55555555555</Name>
<Age>555555555</Age>

</IndianPlayer>

</India>



String fname="name.xml";
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();


try {


DocumentBuilder db = fac.newDocumentBuilder();


Document doc = db.parse(new FileInputStream("name.xml"));

Element root = doc.getDocumentElement();

Element main1 = doc.createElement("IndianPlayer");


Element productnames1 = doc.createElement("Name");

Text product1 = doc.createTextNode(request.getParameter("name1"));


Element price1 = doc.createElement("Age");
Text priceValues1 = doc.createTextNode(request.getParameter("age1"));



productnames1.appendChild(product1);
price1.appendChild(priceValues1);



main1.appendChild(productnames1);
main1.appendChild(price1);

root.appendChild(main1);



saveDocAsFile(doc,fname);


}
catch (ParserConfigurationException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}


public static void saveDocAsFile(Document doc, String fname) {
try {
TransformerFactory tfFac = TransformerFactory.newInstance();
// use null trandformation

Transformer tf = tfFac.newTransformer();
tf.setOutputProperty(OutputKeys.INDENT,"yes");
tf.transform(new DOMSource(doc), new StreamResult(fname));
}
/* catch (IOException ioe) {
ioe.printStackTrace();
}*/
catch (TransformerException e) {
e.printStackTrace();
}
[ June 17, 2004: Message edited by: kesava chaitanya ]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
there is no indenting

Yes, that's just the way Transformer works... If you want "true" indentation, you need to use a non-standard API such as Xerces' XMLSerializer or something from JDOM, or try to find a parser of which Transformer implementation performs true indentation (if I remember correctly, neither Xerces or Crimson do). If you find such a parser, please let us know


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
 
wood burning stoves
 
subject: how to remove spaces while updating xml and How to get indenting?
 
Similar Threads
problem while appending child nodes
how to append nodes to the xml document
crimson error encountered
how to remove particular child element
how to use insertBefore method