如何使用 Java XML 中的引用 URI 加载外部资源
我正在尝试从 URI 引用中提取 XML 节点。此 URI 引用由 XAdES4j 签名者生成,并且可以引用同一 XML 文件或同一文件夹中的节点。
我希望加载引用会像这样简单,
XMLSignatureInput referencedXML = ResourceResolver.resolveStatic(referenceNode.getAttributeNode("URI"), referenceDoc.getBaseURI());
但是这只会给我一个 NullPointerException ,没有任何有用的消息。两个参数均非空,在简单情况下,URI 属性为“data.xml”,referenceDoc 基本 URI 为“/Users/ryan/.../test-files/signature.xml。data.xml 位于相同的 test-files 目录。
URI 可以指向文件中的一个元素,所以我希望有一个参考加载器可以为我处理这种复杂性?
编辑:
我正在尝试的参考 。负载为:
<ds:Reference Id="xmldsig-c6050e36-d9e7-46d9-ac5f-447d1bbb405f-ref0" URI="lote.xml" xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
I am trying to extract an XML node from a URI reference. This URI reference is generated by an XAdES4j signer, and could reference a node within the same XML file, or in the same folder.
I was hoping that loading the reference would be as simple as
XMLSignatureInput referencedXML = ResourceResolver.resolveStatic(referenceNode.getAttributeNode("URI"), referenceDoc.getBaseURI());
However this just gives me a NullPointerException with no useful message. Both arguments are non-null, and in a simple case the URI attribute was "data.xml" and the referenceDoc base URI was "/Users/ryan/.../test-files/signature.xml. data.xml resides in the same test-files directory.
The URI could point to an element within a file, so I'm hoping there is a reference loader which deals with that complexity for me. What have I missed?!
edit:
The reference I am trying to load is:
<ds:Reference Id="xmldsig-c6050e36-d9e7-46d9-ac5f-447d1bbb405f-ref0" URI="lote.xml" xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
完全在黑暗中刺探(你真的需要粘贴堆栈跟踪......),但这里是。
我快速浏览了 ResourceResolver 的来源:
http://java2s.com/Open-Source/Java-Document/XML/xml-security-1.4.3/org/apache/xml/security/utils/resolver/ResourceResolver.java.htm< /a>
我可以看到,由于 ResourceResolver 中未初始化 _resolverVector 字段,将会出现 NullPointerException,除非已经调用了静态 init() 方法资源解析器。在 init() 方法上方,有一条注释:
ResourceResolver 本身似乎不会触发该调用 - 你会这样做吗?如果没有,您应该在使用任何 org.apache.xml.security 内容之前尝试一下。
Total stab in the dark (you really need to paste stacktraces...), but here goes.
I took a quick look at the source of ResourceResolver at:
http://java2s.com/Open-Source/Java-Document/XML/xml-security-1.4.3/org/apache/xml/security/utils/resolver/ResourceResolver.java.htm
I can see there is going to be a NullPointerException due to a non initialised _resolverVector field in the ResourceResolver, unless the static init() method has already been called on ResourceResolver. Above the init() method, there is a comment:
The ResourceResolver itself does not appear to trigger that call - do you do it ? If not you should try it, prior to using any of the org.apache.xml.security stuff.
首先,您需要了解到底是什么导致了 NullPointerException,因此我建议将此代码分开,以便您可以看到 null 值在哪里。
Firstly you need to understand what exactly is causing the NullPointerException so I would suggest splitting this code out so that you can see where the null value is.