tags:

views:

80

answers:

4

I am converting an xml string to nodelist using the code,

InputSource inputSource = new InputSource(new ByteArrayInputStream(
    uploadFormBean.getXhtmlResponse().getBytes()));
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
Document document;
document = documentBuilderFactory.newDocumentBuilder().parse(inputSource);

I do the above inorder to iterate over the node list and replace of the one node elements by using the setTextContent.

I then convert the Document to string using the below API,

 ByteArrayOutputStream byteOutput = new java.io.ByteArrayOutputStream();
 Result result = new StreamResult(byteOutput); 
 Source source = new DOMSource(document); 
 // write the DOM document to the file 
 Transformer transformer;
 transformer = TransformerFactory.newInstance().newTransformer();
 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "YES");
 transformer.transform(source, result);
 String resultText = byteOutput.toString();
 System.out.println("resultText::" + resultText);

When i display the string I find that the resulting xml has got new lines introduced.

Why is this happening? The source xml String does not have these new lines. How can this be resolved?

When i use str.replaceAll("(\r|\n)", ""); it removes all the new lines. I do not want this to happen. I want the String back in the same way as the input. I am looking for a way to avoid the unneccesary new lines introduced in the processing.

A: 

transformer.setOutputProperty(OutputKeys.INDENT, "no"); might work

Xr
I tried this but it does not work.
Rachel
A: 

In JDK 1.6 I pasted the following code and it does not add any new lines

InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("digest.xml");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
Document document = documentBuilderFactory.newDocumentBuilder().parse(resourceAsStream);
ByteArrayOutputStream byteOutput = new java.io.ByteArrayOutputStream();
Result result = new StreamResult(byteOutput); 
Source source = new DOMSource(document); 
// write the DOM document to the file 
Transformer transformer;
transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "YES");
transformer.transform(source, result);
String resultText = byteOutput.toString();
System.out.println("resultText::" + resultText);

Are you doing something in your setTextContent ? perhaps adding new lines in that code inadvertently?

Calm Storm
No. It comes even if after commented the node element replace logic.
Rachel
Like @Max has asked, what is your JDK Version ?
Calm Storm
I am also using JDK 1.6. As you say when i tried another sample xml it does not introduce new line. The input content is not an xml content but an xhtml content got after transformation of an xml using a xsl.
Rachel
My guess is that the XSL transformation has introduced some new lines (in the xhtml). My code just read an XML file and outputted it after transformation :)
Calm Storm
You are right. The output from XSL does it. Is there a way to get rid of it.
Rachel
You could update your question to say what the XSL is doing. Perhaps post the XSL (or a snippet). Check if you could have used "xsl:strip-space" to remove spurious spaces/lines.
Calm Storm
When i used the below method to transform, the new line is not getting added. Thanks.public String getStringFromDoc(org.w3c.dom.Document doc) { DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation(); LSSerializer lsSerializer = domImplementation.createLSSerializer(); return lsSerializer.writeToString(doc); }
Rachel
A: 

Once you have a DOM object try getting a node list using getChildNodes(). Then iterate thru all the child nodes using item() getting the text content of each node that has text content and then adding that content to your string. This may work better for you than trying to figure out what the transformer is doing to your document.

mjh2007
A: 

So you want your entire output XML on 1 line?
If so this might to the trick:

String separator = System.getProperty("line.separator");

System.setProperty("line.separator", "");

transformer.transform(source, result); // Remember to re-set it to it's original value! System.setProperty("line.separator", separator);