Miya
Miya

Reputation: 547

How to remove the white spaces between tags in XML

I created an XML document using Java in my android application. I have to call a web service in my application and pass this XML as an argument there. But my problem is there created a white space between each tag in the XML.

DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.newDocument();

Element root = doc.createElement("subscriber");
doc.appendChild(root);

//creating child node for username
EditText txtusername=(EditText)findViewById(R.id.txtUserName);
subscriber[0]=String.valueOf(txtusername.getText());
Element UserName=doc.createElement("UserName");             
UserName.setTextContent(subscriber[0]);
root.appendChild(UserName);

//creating child node for PASSWORD 
EditText txtPassword=(EditText)findViewById(R.id.txtPassword);
subscriber[1]=String.valueOf(txtPassword.getText());
Element Password=doc.createElement("Password");
Password.setTextContent(subscriber[1]);
root.appendChild(Password);

//set up a transformer
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");

//create string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
String xmlString =sw.toString();

url = new URL("http://192.168.70.14/NewsLetter/subscribing.php?register= " + xmlString);

conn = (HttpURLConnection) url.openConnection();
conn.addRequestProperty("Content-Type", "text/xml; charset=UTF-8"); 
dis = conn.getInputStream();

The XML is:

<subscriber> <UserName>miya</UserName> <Password>today</Password> </subscriber>

Please give the solution for how to remove the white spaces between the UserName and Password tags.

Upvotes: 10

Views: 56228

Answers (7)

Knossos
Knossos

Reputation: 16038

Of course, it depends on your XML itself. However, you could try using regular expressions.

As an example:

yourXmlAsString.replaceAll(">[\\s\r\n]*<", "><");

Would remove all whitespace between every XML element.

Upvotes: 20

Luis Banegas Saybe
Luis Banegas Saybe

Reputation: 33

You can create a copy of all nodes in a document, and trims the nodeValue of each node if it exists.

const copyChildrenNodesWithoutWhiteSpace = (document) => {
  const clone = document.cloneNode();

  for (const child of document.childNodes) {
    const childCopy = copyChildrenNodesWithoutWhiteSpace(child);
    clone.appendChild(childCopy);

    if (childCopy.nodeValue) {
      childCopy.nodeValue = childCopy.nodeValue.trim();
    }
  }

  return clone;
};

const result = copyChildrenNodesWithoutWhiteSpace(anyDocument);

Upvotes: 0

Nick M
Nick M

Reputation: 1

This worked for me, thank you. As a caveat, although this is actually useful for my purpose, I noticed that it can also remove text content if that consists only of whitespace. For example, running the following:

String xmlIn = "<tag> </tag> <tag>\t</tag>\t <tag>\r\n</tag><tag> text </tag>";
String xmlOut = xmlIn.replaceAll("(?:>)(\\s*)<", "><");
System.out.println(xmlOut);

gives the following:

<tag></tag><tag></tag><tag></tag><tag> text </tag>

Upvotes: 0

Dinu94
Dinu94

Reputation: 165

None of the other answers worked for me. I had to use the below code, to remove both additional whitespaces and new lines.

xmlString.trim().replace("\n", "").replaceAll("( *)<", "<")

Upvotes: 0

dilipktr
dilipktr

Reputation: 81

This is the regular expression (?:>)(\s*)<

When you use it in the code for Java use "(?:>)(\\s*)<" and replace with "><"

String xmlString = "<note>    <to>Tove</to>    <from>Jani</from <heading>Reminder</heading> <title>Today</title>    <body>Don't forget me this weekend!</body>    </note>";

String str = xmlString.replaceAll("(?:>)(\\s*)<", "><");

This will remove the spaces between the tags and maintain the spaces for the value.

Input:

<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading> <title>Today</title>
    <body>Don't forget me this weekend!</body>
</note>

Output:

<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><title>Today</title><body>Don't forget me this weekend!</body></note>

Upvotes: 2

Peter Knego
Peter Knego

Reputation: 80340

Method documentBuilderFactory.setIgnoringElementContentWhitespace() controls whitespace creation. Use this before you create a DocumentBuilder.

dbfac.setIgnoringElementContentWhitespace(true);

Upvotes: 8

Gaʀʀʏ
Gaʀʀʏ

Reputation: 4540

I was able to remove whitespace/tabs/newlines from my transformation using the following property:

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

You had it set to yes. I'm sure this question is old enough that it doesn't matter now; but if anyone runs into this in the future, setting the property to no saved me.

Upvotes: 6

Related Questions