我想使用 Apache XMLBeans 转义字符串以将其嵌入 XML 文档中,例如编码所有 XML 实体。
XmlString 确实提供了此功能,但坚持将输出包装在 xml-fragment 标记中,我想摆脱它。
但是,我对建议不感兴趣
- 使用 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);
}
}