简体   繁体   中英

Removing whitespaces from String and then convert to XML

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM