- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有 XML 节点:
<soapenv:Body
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
wsu:Id="Id-223">
....
</soapenv:Body>
我应该将什么值传递给 org.w3c.dom.Element.getAttributeNS(namespaceURI, localName) 来获取属性 Id 的值?
注意我可以使用 Element.getAttribute("wsu:Id") 来获取值,但我必须使用 getAttributeNS 方法。
其他信息: org.apache.wss4j.dom.message.token.Timestamp使用以下代码:
public String getID() {
//WSConstants.WSU_NS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
return element.getAttributeNS(WSConstants.WSU_NS, "Id");
}
我可以确认这段代码不起作用。
[我的代码]是从德米特里的回答中复制的 post 。您可以使用有问题的 XML 和 Dmitry 答案中的 validateSignature 方法轻松重现我的错误
File signatureFile = new File("/home/user1/Downloads/test.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(signatureFile);
Node securityNode = doc.getElementsByTagName("wsse:Security").item(0);
Node bodyNode = doc.getElementsByTagName("soap:Body").item(0);
validateSignature(securityNode, bodyNode, key);
然后
public boolean validateSignature(Node signatureNode, Node bodyTag, PublicKey publicKey) {
boolean signatureIsValid = false;
try {
// Create a DOM XMLSignatureFactory that will be used to unmarshal the
// document containing the XMLSignature
String providerName = System.getProperty
("jsr105Provider", "org.jcp.xml.dsig.internal.dom.XMLDSigRI");
XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM",
(Provider) Class.forName(providerName).newInstance());
// Create a DOMValidateContext and specify a KeyValue KeySelector
// and document context
DOMValidateContext valContext = new DOMValidateContext(new X509KeySelector(publicKey), signatureNode);
String nameSpaceURI = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";
org.w3c.dom.Element bodyEle = (Element) bodyTag;
System.out.println(Main.nodeToString(bodyEle));
valContext.setIdAttributeNS((Element) bodyTag, org.apache.ws.security.WSConstants.WSU_NS, "Id");
// Unmarshal the XMLSignature.
XMLSignature signature = fac.unmarshalXMLSignature(valContext);
// Validate the XMLSignature.
signatureIsValid = signature.validate(valContext);
} catch (Exception ex) {
ex.printStackTrace();
}
return signatureIsValid;
}
最佳答案
原来我漏掉了这段代码
dbFactory.setNamespaceAware(true);
关于Java XML DOM - 如何使用 Element.getAttributeNS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48001894/
我正在用 JavaScript 编写一个小程序,我想在其中解析以下小 XML 片段: 因为我不知道,如果元素和属性有命名空间前缀,我正在使用命名空间感知函数(getEleme
var svg = document.getElementById('test'), use = svg.lastElementChild; alert(use.getAttributeNS(
我在网站上工作,并使用 jQuery 来显示弹出窗口。 当点击网站上的一篇文章时,它应该加载一个弹出窗口。以下代码用于该目的: var popup = jQuery(this).attr('rel')
我有 XML 节点: .... 我应该将什么值传递给 org.w3c.dom.Element.getAttributeNS(namespaceURI, local
我在从 Java DOM 正确调用 getAttributeNS() (和其他 NS 方法)时遇到问题。首先,这是我的示例 XML 文档: Harry Potter
我正在升级一个项目以使用 10.5 SDK。我收到这种形式的警告: warning: 'getAttributeNS::' is deprecated (declared at /Developer/
我很难找到 hasAttributeNS() 和 getAttributeNS() 的规范,它告诉我在“无 namespace ”的情况下为第一个参数提供什么“属性。根据经验,似乎 ""不起作用,而
我是一名优秀的程序员,十分优秀!