| 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
|
|
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]
|
 |
 |
|
|
subject: how to remove spaces while updating xml and How to get indenting?
|
|
|