在 Java 中生成 XML 时转义特殊字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4301236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Escaping special character when generating an XML in Java
提问by Salman A. Kagzi
I am trying to develop an XML export feature to give my application users to export their data in an XML format. I have got this feature ready and working until it started failing for some cases. Then I realized that it was because of some special characters that needs to be encoded. for example the data might contain & or ! or % or ' or # etc. etc. and this needs to be escaped properly. I was wondering if there is a generic utility available that can escape all of the special characters as per the XML specification. I couldn't find anything on Google.
我正在尝试开发 XML 导出功能,让我的应用程序用户能够以 XML 格式导出他们的数据。我已经准备好这个功能并开始工作,直到它在某些情况下开始失败。然后我意识到这是因为需要编码一些特殊字符。例如,数据可能包含 & 或 ! 或 % 或 ' 或 # 等,这需要正确转义。我想知道是否有可用的通用实用程序可以根据 XML 规范转义所有特殊字符。我在谷歌上找不到任何东西。
Is there something like that already there? or Is there any other way to do it?
那里已经有类似的东西了吗?或者有没有其他方法可以做到?
Here is the code I am using to generate XML
这是我用来生成 XML 的代码
Document xmldoc = new DocumentImpl();
Element root = xmldoc.createElement("Report");
Element name= xmldoc.createElement((exportData.getChartName() == null) ? "Report" : exportData.getChartName());
if (exportData.getExportDataList().size() > 0
&& exportData.getExportDataList().get(0) instanceof Vector) {
// First row is the HEADER, i.e name
Vector name = exportData.getExportDataList().get(0);
for (int i = 1; i value = exportData.getExportDataList().get(i);
Element sub_root = xmldoc.createElement("Data");
//I had to remove a for loop from here. StackOverflow description field would not take that. :(
// Insert header row
Element node = xmldoc.createElementNS(null, replaceUnrecognizedChars(name.get(j)));
Node node_value = xmldoc.createTextNode(value.get(j));
node.appendChild(node_value);
sub_root.appendChild(node);
chartName.appendChild(sub_root);
}
}
}
root.appendChild(name);
// Prepare the DOM document for writing
Source source = new DOMSource(root);
// Prepare the output file
Result result = new StreamResult(file);
// Write the DOM document to the file
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(source, result);`
Sample XML:
示例 XML:
<Data>
<TimeStamp>2010-08-31 00:00:00.0</TimeStamp>
<[Name that needs to be encoded]>0.0</[Name that needs to be encoded]>
<Group_Average>1860.0</Group_Average>
</Data>
采纳答案by gigadot
You can use apache common lang libraryto escape a string.
您可以使用apache 公共 lang 库来转义字符串。
org.apache.commons.lang.StringEscapeUtils
String escapedXml = StringEscapeUtils.escapeXml("the data might contain & or ! or % or ' or # etc");
But what you are looking for is a way to convert any string into a valid XML tag name. For ASCII characters, XML tag name must begin with one of _:a-zA-Z and followed by any number of character in _:a-zA-Z0-9.-
但是您正在寻找的是一种将任何字符串转换为有效 XML 标记名称的方法。对于 ASCII 字符,XML 标记名称必须以 _:a-zA-Z 之一开头,后跟 _:a-zA-Z0-9 中任意数量的字符。-
I surely believe there is no library to do this for you so you have to implement your own function to convert from any string to match this pattern or alternatively make it into a value of attritbue.
我当然相信没有库可以为您执行此操作,因此您必须实现自己的函数以从任何字符串转换以匹配此模式,或者将其转换为 attritbue 的值。
<property name="no more need to be encoded, it should be handled by XML library">0.0</property>
回答by Chintan Raghwani
public class RssParser {
int length;
URL url;
URLConnection urlConn;
NodeList nodeList;
Document doc;
Node node;
Element firstEle;
NodeList titleList;
Element ele;
NodeList txtEleList;
String retVal, urlStrToParse, rootNodeName;
public RssParser(String urlStrToParse, String rootNodeName){
this.urlStrToParse = urlStrToParse;
this.rootNodeName = rootNodeName;
url=null;
urlConn=null;
nodeList=null;
doc=null;
node=null;
firstEle=null;
titleList=null;
ele=null;
txtEleList=null;
retVal=null;
doc = null;
try {
url = new URL(this.urlStrToParse);
// dis is path of url which v'll parse
urlConn = url.openConnection();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
String s = isToString(urlConn.getInputStream());
s = s.replace("&", "&");
StringBuilder sb =
new StringBuilder
("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.append("\n"+s);
System.out.println("STR: \n"+sb.toString());
s = sb.toString();
doc = db.parse(urlConn.getInputStream());
nodeList = doc.getElementsByTagName(this.rootNodeName);
// dis is d first node which
// contains other inner element-nodes
length =nodeList.getLength();
firstEle=doc.getDocumentElement();
}
catch (ParserConfigurationException pce) {
System.out.println("Could not Parse XML: " + pce.getMessage());
}
catch (SAXException se) {
System.out.println("Could not Parse XML: " + se.getMessage());
}
catch (IOException ioe) {
System.out.println("Invalid XML: " + ioe.getMessage());
}
catch(Exception e){
System.out.println("Error: "+e.toString());
}
}
public String isToString(InputStream in) throws IOException {
StringBuffer out = new StringBuffer();
byte[] b = new byte[512];
for (int i; (i = in.read(b)) != -1;) {
out.append(new String(b, 0, i));
}
return out.toString();
}
public String getVal(int i, String param){
node =nodeList.item(i);
if(node.getNodeType() == Node.ELEMENT_NODE)
{
System.out.println("Param: "+param);
titleList = firstEle.getElementsByTagName(param);
if(firstEle.hasAttribute("id"))
System.out.println("hasAttrib----------------");
else System.out.println("Has NOTNOT NOT");
System.out.println("titleList: "+titleList.toString());
ele = (Element)titleList.item(i);
System.out.println("ele: "+ele);
txtEleList = ele.getChildNodes();
retVal=(((Node)txtEleList.item(0)).getNodeValue()).toString();
if (retVal == null)
return null;
System.out.println("retVal: "+retVal);
}
return retVal;
}
}
回答by Abhishek Jha
Use the below code to escapes the characters in a String using XML.StringEscapeUtils is available in apche commons lang3 jar
使用下面的代码使用 XML.StringEscapeUtils 转义字符串中的字符在 apche commons lang3 jar 中可用
StringEscapeUtils.escapeXml11("String to be escaped");