简体   繁体   English

getElementById为java中的xml文档返回null

[英]getElementById return null for xml document in java

I am generating Document from string xml.我正在从字符串 xml 生成文档。

javax.xml.parsers.DocumentBuilderFactory dbf =javax.xml.parsers.DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc =  dBuilder.parse(new InputSource(new StringReader(xml)));

I want to digitally sign xml request, I am using javax.xml library.我想对 xml 请求进行数字签名,我正在使用 javax.xml 库。 Inorder to generate references, I need to pass uri of element that i neeed to digitally sign.为了生成引用,我需要传递我需要进行数字签名的元素的 uri。 Code i am using to generate reference is this :我用来生成参考的代码是这样的:

Reference ref = fac.newReference(id, fac.newDigestMethod(DigestMethod.SHA256, null), transformList, null, null);

Inside above function, I am getting error resolver.ResourceResolverException: Cannot resolve element with ID .在上面的函数中,我收到错误resolver.ResourceResolverException: Cannot resolve element with ID When i went inside the function newReference .当我进入函数newReference 时 Its calling它的召唤

Element e = doc.getElementById(idValue);

doc.getElementById(idValue) is returning null. doc.getElementById(idValue)返回 null。 I wrote one test case to test getElementById我写了一个测试用例来测试getElementById

String xmlString = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?><request> <myxml id=\"122\" ></myxml></request>";
String cleanXml = xmlString.replaceAll("\\r", "").replaceAll("\\n", "");
Document doc = convertXmlStringToDoc(cleanXml);
Element e = doc.getElementById("122");
System.out.println(e);

this is also showing null.这也显示为空。 Any idea, what i am doing wrong here ?任何想法,我在这里做错了什么?

The way you parse xmlString to Document is right.您将 xmlString 解析为 Document 的方式是正确的。 The document contains the elements.该文档包含元素。 But the attribute "id" in your String is not ID (doesn't matter case sensitive) of Document element.但是 String 中的属性“id”不是 Document 元素的ID (不区分大小写)。 The doc of Document.getElementById(String elementId) said:文档Document.getElementById(String elementId)说:

Returns the Element that has an ID attribute with the given value.返回具有给定值的 ID 属性的元素。 If no such element exists, this returns null .如果不存在这样的元素,则返回 null 。 ... The DOM implementation is expected to use the attribute Attr.isId to determine if an attribute is of type ID. ... DOM 实现应使用属性 Attr.isId 来确定属性是否属于类型 ID。 Note: Attributes with the name "ID" or "id" are not of type ID unless so defined.注意:除非如此定义,否则名称为“ID”或“id”的属性不是 ID 类型。

There should be another DTD file to define which attribute is ID in this xml.应该有另一个 DTD 文件来定义这个 xml 中哪个属性是ID Here is an example, in that example, the ID is artistID .是一个示例,在该示例中, IDartistID

So the right way you reach an element is like this:所以你到达一个元素的正确方式是这样的:

        Element element = doc.getElementById("122"); // null
        //System.out.println(element.getNodeValue());
        NodeList e = doc.getElementsByTagName("myxml");
        NamedNodeMap namedNodeMap = e.item(0).getAttributes();
        String value = namedNodeMap.getNamedItem("id").getNodeValue();
        System.out.println(value); // 122

In your case, "id" is not a special element, it's just a simple attribute to contain information.在您的情况下,“id”不是一个特殊元素,它只是一个包含信息的简单属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM