本文介绍了在Java中生成XML时转义特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发XML导出功能,以使我的应用程序用户可以XML格式导出其数据.我已经准备好了该功能并且可以正常工作,直到在某些情况下它开始失败为止.然后我意识到这是因为某些特殊字符需要编码.例如,数据可能包含&或者 !或%或'或#等.等等,需要正确地进行转义.我想知道是否有通用的实用程序可以按照XML规范转义所有特殊字符.我在Google上找不到任何东西.

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.

已经有类似的东西了吗?或还有其他方法吗?

Is there something like that already there? or Is there any other way to do it?

这是我用来生成XML的代码

Here is the code I am using to generate 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);`

示例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>

推荐答案

您可以使用 apache通用lang库来转义字符串

You can use apache common lang library to escape a string.

org.apache.commons.lang.StringEscapeUtils

String escapedXml = StringEscapeUtils.escapeXml("the data might contain & or ! or % or ' or # etc");

但是,您正在寻找一种将任何字符串转换为有效XML标记名称的方法.对于ASCII字符,XML标记名称必须以_:a-zA-Z之一开头,然后以_:a-zA-Z0-9.-

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.-

我当然相信没有库可以为您执行此操作,因此您必须实现自己的函数以从任何字符串进行转换以匹配此模式,或者将其转换为attritbue的值.

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.

<property name="no more need to be encoded, it should be handled by XML library">0.0</property>

这篇关于在Java中生成XML时转义特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 05:59