Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中xml文档的getElementById返回null_Java_Xml_Security_W3c - Fatal编程技术网

java中xml文档的getElementById返回null

java中xml文档的getElementById返回null,java,xml,security,w3c,Java,Xml,Security,W3c,我正在从字符串xml生成文档 javax.xml.parsers.DocumentBuilderFactory dbf =javax.xml.parsers.DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFa

我正在从字符串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)));
我想对xml请求进行数字签名,我使用的是javax.xml库。为了生成引用,我需要传递我需要数字签名的元素的uri。我用于生成引用的代码如下:

Reference ref = fac.newReference(id, fac.newDigestMethod(DigestMethod.SHA256, null), transformList, null, null);
在上面的函数中,我得到错误解析器。ResourceResolvexException:无法解析ID为的元素。当我进入函数newReference时。它的召唤

Element e = doc.getElementById(idValue);
doc.getElementById(idValue)返回null。我编写了一个测试用例来测试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);
String xmlString=”“;
String cleanXml=xmlString.replaceAll(“\\r”和“).replaceAll(“\\n”和“);
Document doc=convertXmlStringToDoc(cleanXml);
元素e=doc.getElementById(“122”);
系统输出打印ln(e);

这也显示为空。知道我做错了什么吗?

将xmlString解析为文档的方法是正确的。文档包含这些元素。但是字符串中的属性“id”不是Document元素的
id
(不区分大小写)。
Document.getElementById(String elementId)

返回具有给定值的ID属性的元素。如果不存在这样的元素,则返回null。。。DOM实现需要使用属性Attr.isId来确定属性是否为ID类型。注意:除非定义了“ID”或“ID”名称,否则属性不是ID类型。

应该有另一个DTD文件来定义此xml中哪个属性是
ID
。是一个示例,在该示例中,
ID
artistID

因此,到达元素的正确方式如下所示:

        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
在您的例子中,“id”不是一个特殊的元素,它只是一个包含信息的简单属性