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.