2012-12-18 13 views
8

पर प्रिंटिंग मैंने कुछ कोड बनाया जो एक्सएमएल के लिए एक नक्शा लिखता है। ऐसा लगता है कि यह काम कर रहा है लेकिन फ़ाइल को कोई नई लाइन के साथ मुद्रित किया गया है। तो किसी भी एक्सएमएल संपादक में केवल एक पंक्ति पर। मैं इसे प्रत्येक बच्चे के लिए एक नई लाइन पर कैसे प्रिंट कर सकता हूं?एक्सएमएल बनाना केवल एक पंक्ति

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
DocumentBuilder db = dbf.newDocumentBuilder(); 
Document doc = db.newDocument(); 

Element vdata = doc.createElement("vouchdata"); 
doc.appendChild(vdata); 

for (Entry<String, String> entry : vouchMap.entrySet()) { 
    Element udata = doc.createElement("vouch"); 

    Attr vouchee = doc.createAttribute("name"); 
    vouchee.setValue(entry.getKey()); 
    udata.setAttributeNode(vouchee); 

    Attr voucher = doc.createAttribute("vouchedBy"); 
    voucher.setValue(entry.getValue()); 
    udata.setAttributeNode(voucher); 

    vdata.appendChild(udata); 
} 

// write the content into xml file 
TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
Transformer transformer = transformerFactory.newTransformer(); 
DOMSource source = new DOMSource(doc); 
StreamResult result = new StreamResult(new File("vouchdata.xml")); 

// Output to console for testing 
// StreamResult result = new StreamResult(System.out); 

transformer.transform(source, result); 
+3

देखें http://stackoverflow.com/questions/1264849/pretty-printing-output-from-javax-xml-transform -transformer-with-only-standard-j –

उत्तर

19

मैं

Transformer tf = TransformerFactory.newInstance().newTransformer(); 
tf.setOutputProperty(OutputKeys.INDENT, "yes"); 
tf.setOutputProperty(OutputKeys.METHOD, "xml"); 
tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); 

ठीक काम करने लगता है कौन सा प्रयोग करते हैं।

+0

यह मेरे लिए काम करता है। धन्यवाद – Tony

+0

@ टोनी गुगलिंग और परीक्षण और त्रुटि के लिए मेरे लिए यह छोटा हैक। खुशी है कि मैंने आपको हैसल बचाया;) – MadProgrammer

संबंधित मुद्दे