Reputation: 103
I want to remove white space from a string and then convert it to XML. This is my string:
<XMLDoc>
<Envelope>
<Header>
<header>
<msg-id>000C2990-2FBD-11E5-E6CF-FB0900F491A5</msg-id>
</header>
</Header>
<Body>
<GetWorkItemsResponse>
<cursor
numRows="0"
sameConnection="false"
/>
<tuple
>
<old>
<TaskInfo>
...
I can remove the white space using str.replaceall("\\s+","")
. But while converting from string to XML, it is showing an error, because it removes the space between element and attribute. It gives a result of <cursornumRows="0" sameConnection="false"/>
when the actual element is <cursor numRows="0" sameConnection="false"/>
. The space between element cursor
and attribute numRows
is removed.
Can anyone help me?
Upvotes: 1
Views: 751
Reputation: 22631
You don't need to remove whitespace before converting it to XML. Simply
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(xmlString)));
will work. When you convert the XML document back to a string, the whitespace will disappear (by default - there are some options for pretty-printing as well):
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
String outputString = writer.getBuffer().toString();
Upvotes: 1