开发者

How do i remove the new lines introduced when converting Document to String?

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 pr开发者_StackOverflow中文版ocessing.


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

  • setOutputProperty()
  • OutputKeys.INDENT


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?


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.


May I offer you the FilteredWriter class:

private class FilteredWriter extends FilterWriter
{
    protected char[] filter = null;

    protected FilteredWriter(Writer out) {
        super(out);
    }

    public void setFilter(String filteredChars) {
        filter = filteredChars.toCharArray();
    }

    public void write(String str, int off, int len) throws IOException
    {
        write(str.toCharArray(), off, len);
    }

    public void write(char[] cbuf, int off, int len) throws IOException
    {
        for (int i = off; i < off + len; i++)
            write(cbuf[i]);
    }

    public void write(int c) throws IOException
    {
        for (char f : filter)
        {
            if (f == (char)c)
                return;
        }
        out.write(c);
    }
}

And this is how to use it:

FilteredWriter filteredWriter = new FilteredWriter(writer);
filteredWriter.setFilter("\r\n\t");
StreamResult result = new StreamResult(filteredWriter);

Hope this helps...


If you are using xerces implementation for your DOM, while serializing make sure that indent flag is set to false.

org.apache.xml.serialize.OutputFormat format = 
new org.apache.xml.serialize.OutputFormat(n.getOwnerDocument());
format.setIndenting(true);
format.setPreserveSpace(false);
format.setLineWidth(80);
format.setMethod(Method.XML);

Varun Jangidi


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜