使用 Apache XMLBeans 对字符串中的 XML 实体进行编码

发布于 2024-11-18 23:39:18 字数 1072 浏览 2 评论 0原文

我想使用 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 &amp; me]> 
    //             but was:<[<xml-fragment>You &amp; 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦萦几度 2024-11-25 23:39:18

在将 XmlOptions 传递给 xmlText() 方法之前使用 setSaveOuter() 选项。像这样。

XmlOptions opts = new XmlOptions();
opts.setSaveOuter();
return value.xmlText(opts);

我想澄清一下,如果您只是凭空创建一个新元素,那么元素名称似乎不会被序列化。例如,如果我有一个 Model 元素,上面有一个 Property 元素,则以下内容将不会显示属性标签,而是显示 xml 片段。

Property property = Property.Factory.newInstance(); 

XmlOptions opts = new XmlOptions();
opts.setSaveOuter();
return property.xmlText(opts);

要显示属性元素,我必须执行以下操作。

ModelDocument modelDoc = ModelDocument.Factory.newInstance();
ModelType model = modelDoc.addNewModel();
PropertyType propertyType = model.addNewProperty();
Property property = Property.Factory.newInstance(); 

XmlOptions opts = new XmlOptions();
opts.setSaveOuter();
return property.xmlText(opts);

Use the setSaveOuter() option before passing the XmlOptions to the xmlText() method. Like this.

XmlOptions opts = new XmlOptions();
opts.setSaveOuter();
return value.xmlText(opts);

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.

Property property = Property.Factory.newInstance(); 

XmlOptions opts = new XmlOptions();
opts.setSaveOuter();
return property.xmlText(opts);

To have the Property Element show up I must do the following.

ModelDocument modelDoc = ModelDocument.Factory.newInstance();
ModelType model = modelDoc.addNewModel();
PropertyType propertyType = model.addNewProperty();
Property property = Property.Factory.newInstance(); 

XmlOptions opts = new XmlOptions();
opts.setSaveOuter();
return property.xmlText(opts);
っ〆星空下的拥抱 2024-11-25 23:39:18

您期望的字符串不正确。通过调用 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.

一张白纸 2024-11-25 23:39:18

创建 xml reader 作为普通 xml 字符串,然后删除 xml 片段,
我已经尝试过这样的

if(request != null && request.contains("<xml")){
XMLInputFactory xif = XMLInputFactory.newFactory();
StringReader reader = new StringReader(request);
StreamSource xml = new StreamSource(reader);
XMLStreamReader xsr = xif.createXMLStreamReader(xml);
xsr.nextTag();          
request = xsr.getElementText();
System.out.println("updated request is \n"+request);
}

然后

JAXBContext jaxbContext = JAXBContext.newInstance(YourClass.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(request);

create xml reader as a normal xml string then remove the xml-fragment,
i have tried like this,

if(request != null && request.contains("<xml")){
XMLInputFactory xif = XMLInputFactory.newFactory();
StringReader reader = new StringReader(request);
StreamSource xml = new StreamSource(reader);
XMLStreamReader xsr = xif.createXMLStreamReader(xml);
xsr.nextTag();          
request = xsr.getElementText();
System.out.println("updated request is \n"+request);
}

then

JAXBContext jaxbContext = JAXBContext.newInstance(YourClass.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(request);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文