如何删除标记在XML之间的空格 [英] How to remove the white spaces between tags in XML

查看:361
本文介绍了如何删除标记在XML之间的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造了我的Andr​​oid应用程序使用Java的XML文档。我打电话给我的应用程序的Web服务,并通过该XML因为有一个说法。但我的问题是没有建立在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();

的XML是:

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.

推荐答案

当然,这取决于你的XML本身,但是你可以尝试定期EX pressions。

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

作为一个例子:

yourXmlAsString.replaceAll(">\\s*<", "><");

将取消所有的XML元素之间的所有空格。

Would remove all whitespace between every XML element.

这篇关于如何删除标记在XML之间的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆