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);
}
}