使用 Apache XMLBeans 对字符串中的 XML 实体进行编码
我想使用 Apache XMLBeans 转义字符串以将其嵌入 XML 文档中,例如对所有 XML 实体进行编码。
XmlString 确实提供了此功能,但坚持将输出包装在 xml 片段标记中,我想摆脱它。
不感兴趣
- 但是,我对使用 XMLBeans 以外的任何内容(如 org.apache.commons.lang.StringEscapeUtils)
- 在转义后删除封闭标签(例如使用正则表达式)
。这是一个测试用例。你能帮我解决它吗?
import org.apache.xmlbeans.*;
public class Test {
@Test public void test(){
String input = "You & me";
String expected = "You & me";
String actual = escape(input);
Assert.assertEquals(expected, actual);
// Fails with: ComparisonFailure: expected:<[You & me]>
// but was:<[<xml-fragment>You & me</xml-fragment>]>
}
private String escape(String str){
XmlString value = XmlString.Factory.newInstance();
value.setStringValue(input);
XmlOptions opts = new XmlOptions();
// do I need to set one of the 54 available options?
// see http://xmlbeans.apache.org/docs/2.4.0/reference/org/apache/xmlbeans/XmlOptions.html
return value.xmlText(opts);
}
}
I'd like to use Apache XMLBeans to escape a String for embedding it in an XML document, e.g. encode all XML entities.
XmlString does provide this functionality, but insists on wrapping the output in xml-fragment tags, which I'd like to get rid of.
However, I'm not interested in sugestions
- to use anything other than XMLBeans (like org.apache.commons.lang.StringEscapeUtils)
- to remove the enclosing tag after escaping (e.g. using a regex)
Here's a test case. Can you help me fix it?
import org.apache.xmlbeans.*;
public class Test {
@Test public void test(){
String input = "You & me";
String expected = "You & me";
String actual = escape(input);
Assert.assertEquals(expected, actual);
// Fails with: ComparisonFailure: expected:<[You & me]>
// but was:<[<xml-fragment>You & me</xml-fragment>]>
}
private String escape(String str){
XmlString value = XmlString.Factory.newInstance();
value.setStringValue(input);
XmlOptions opts = new XmlOptions();
// do I need to set one of the 54 available options?
// see http://xmlbeans.apache.org/docs/2.4.0/reference/org/apache/xmlbeans/XmlOptions.html
return value.xmlText(opts);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在将 XmlOptions 传递给 xmlText() 方法之前使用 setSaveOuter() 选项。像这样。
我想澄清一下,如果您只是凭空创建一个新元素,那么元素名称似乎不会被序列化。例如,如果我有一个 Model 元素,上面有一个 Property 元素,则以下内容将不会显示属性标签,而是显示 xml 片段。
要显示属性元素,我必须执行以下操作。
Use the setSaveOuter() option before passing the XmlOptions to the xmlText() method. Like this.
I would like to clarify that if you just create a new element out of thin air it seems like the element name will not be serialized. For example If I have a Model element that has a Property element on it the following will not display the property tag, it will display the xml fragment.
To have the Property Element show up I must do the following.
您期望的字符串不正确。通过调用 xmlText,它将返回一个 XML,因此该字符串必须包装在 xml-fragment 元素中。
your expected string is incorrect. by virtue of calling xmlText, it will return an XML, hence the string must be wrapped in xml-fragment element.
创建 xml reader 作为普通 xml 字符串,然后删除 xml 片段,
我已经尝试过这样的
然后
create xml reader as a normal xml string then remove the xml-fragment,
i have tried like this,
then