如何转换具有无效 XML 字符的源

发布于 2024-09-26 01:24:38 字数 658 浏览 0 评论 0原文

我正在处理一个需要清除从 SharePoint Web 服务收到的无效 XML 字符的情况。 我知道修复源代码是正确的做法 - 然而这个问题早在 2008 年就已经报告过,而且我还没有发现 Microsoft 已经发布了补丁。

现在,我使用 JAX-WS 的 Provider 接口调用 Web 服务。我很好地收到了带有返回 XML 的源,但我一直在努力寻找一种将源转换为有效 XML DOM 的方法。

每次我尝试类似以下伪代码的操作时,我都会收到一个异常,抱怨非法 XML 字符:

public void xmlTranform(javax.xml.transform.Source source) {
     StreamResult sr = new StreamResult(new java.io.StringWriter());
     Transformer tf = TransformerFactory.newInstance().newTransformer();
     tf.transform(source, sr);
     ...

是否可以创建一个自定义 FilterReader 并将其插入到 Transformation 中?

或者我可以定制 Marshaller 吗?

将代码注入到转换中以过滤掉这些无效字符的最佳方法是什么?

I am working on a case where I need to clean invalid XML characters I receive from a SharePoint web service.
I know fixing the source is the right thing to do - however this issue has been reported back in 2008, and I have yet to find that Microsoft has released a patch for it.

For now, I call the Web Service using the Provider interface from JAX-WS. I receive the Source with the returned XML just fine, but I'm stuck trying to find a way to transform the source to valid XML DOM.

I get an exception complaining about the illegal XML character every time I try something like the following pseudo code:

public void xmlTranform(javax.xml.transform.Source source) {
     StreamResult sr = new StreamResult(new java.io.StringWriter());
     Transformer tf = TransformerFactory.newInstance().newTransformer();
     tf.transform(source, sr);
     ...

Would it be possible to create a custom FilterReader and insert it into the Transformation?

Or could I customize the Marshaller maybe?

What would be the best way to inject code into the transformation to filter out those invalid characters?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

一笑百媚生 2024-10-03 01:24:39

回答我自己的问题 - 我完全放弃了 JAX。

当接收 SOAP 消息严格遵循标准时,JAX-WS 效果很好。但我还没有找到一种方法让它处理不符合标准的 SOAP 消息(请阅读“Microsoft SOAP 消息”)。
我使用了直接的 POJO 代码,其中解决这个问题非常简单。


To answer my own question - I abandoned JAX altogether.

JAX-WS works great when the receiving SOAP message is strictly following standards. But I have yet to find a way to make it process SOAP messages that does not conform to standard (read "Microsoft SOAP messages").
I went with straight POJO code, in which solving this issue is quite trivial.

Br
Jan

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文