Brandon Brandon - 2 months ago 11
Java Question

Fixing format of print statement

I recently just started working with document builder to build a title with my XML. This is the following code I am using right now:

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
InputStream inputStream = new FileInputStream(new File("c:\\staff.xml"));
org.w3c.dom.Document doc = documentBuilderFactory.newDocumentBuilder().parse(inputStream);
StringWriter stw = new StringWriter();
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.transform(new DOMSource(doc), new StreamResult(stw));
String xmldata = (stw.toString());
System.out.println(xmldata);


It builds the title fine, but it starts writing the XML file on the same line due to the parse. Can someone show me how I can alter this code to get
<company>
onto the second line?
Here is my print:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><company>
<comp id="512">
<firstname>Brandon</firstname>
<lastname>Liens</lastname>
<empid>612</empid>
<rqid>51265</rqid>
</comp>
</company>


Staff.XML file:

<company>
<comp id="512">
<firstname>Brandon</firstname>
<lastname>Nyberg</lastname>
<empid>612</empid>
<rqid>51265</rqid>
</comp>
</company>

Answer

re-reference: documentBuilderFactory.newDocumentBuilder().parse(inputStream);