doc.getElementById(id); null
Open Declaration Element org.w3c.dom.Document.getElementById(String elementId) Returns the Element that has an ID attribute with the given value. If no such element exists, this returns null . If more than one element has an ID attribute with that value, what is returned is undefined.
The DOM implementation is expected to use the attribute Attr.isId to determine if an attribute is of type ID. Note: Attributes with the name "ID" or "id" are not of type ID unless so defined. Parameters:
elementId The unique id value for an element.
Returns:
The matching element or null if there is none.
Since:
DOM Level 2
demo:
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-matchers</artifactId>
<version>2.3.0</version>
</dependency>
import java.io.StringReader; import javax.xml.transform.stream.StreamSource; import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xmlunit.transform.Transformation; public class TT { public static void main(String[] args) {
String xmlStr = "<table><tr id=\"aaa\">这是一行</tr></table>";
// String xmlStr = "<!DOCTYPE table [<!ATTLIST tr id ID #REQUIRED>]><table><tr id=\"aaa\">这是一行</tr></table>"; Transformation transformation = new Transformation(new StreamSource(new StringReader(xmlStr)));
Document doc = transformation.transformToDocument();
Element tr = doc.getElementById("aaa");
System.out.println(tr);
}
}
输出结果是:null
原因见红色字内容
如何解决:
为该文档声明DTD
其中有一种内部声明方法如下:
<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>
还有一种外部声明方法:
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
其中内部声明方法的解释是:
https://www.w3schools.com/xml/xml_dtd_elements.asp
本例的解决方案:
<!DOCTYPE table [<!ATTLIST tr id ID #REQUIRED>]>
语法:
<!ATTLIST element-name attribute-name attribute-type attribute-value>
https://www.w3schools.com/xml/xml_dtd_intro.asp
https://www.w3schools.com/xml/xml_dtd_attributes.asp
doc.getElementById(id); null的更多相关文章
- document.getElementById(...) is null
<html> <head> <script type="text/javascript"> document.getElementById('b ...
- Id.value与document.getElementById("Id").value的区别
如果标签Id在Form表单里面的话,直接Id.value就不能用了,而是要用Form.Id.value来取值或设置值 所以最好用document.getElementById("Id&quo ...
- jquery中的$("#id")与document.getElementById("id")的区别
以前一直认为jquery中的$("#id")和document.getElementByIdx_x("id")得到的效果是一样的,今天做特效的时候才发现并不是这 ...
- 获得输入框的文本document.getElementById('id').value;
<input id="demo" type="text" value="" > x=document.getElementByI ...
- function $(id){ return document.getElementById(id); }导致所有的js不能用解决办法。。。。
function $(id){ return document.getElementById(id); } document.getElementById(id) 是获得id这个元素的. 相当于定义了 ...
- java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"报错
出现问题的原因: 内存用户验证时,Spring boot 2.0.1引用的security 依赖是 spring security 5.X版本,此版本需要提供一个PasswordEncorder的实例 ...
- Spring Security 无法登陆,报错:There is no PasswordEncoder mapped for the id “null”
编写好继承了WebSecurityConfigurerAdapter类的WebSecurityConfig类后,我们需要在configure(AuthenticationManagerBuilder ...
- Document.write和 getElementById(ID)
在javascript中,document.write()方法:常用来网页向文档中输出内容. 示例:通过document.write()方法,向网页文档中输出了一段文字. document.write ...
- document.getElementById(“id”)与$("#id")的区别
document.getElementById("id")可以直接获取当前对象, jQuery利用$("#id")获取的是一个[object Object],需 ...
随机推荐
- C语言可以开发哪些项目?(转)
原文地址:https://www.cnblogs.com/shiyanlou/p/6098661.html 知乎:https://www.zhihu.com/question/20564904 C语言 ...
- Extjs如何添加多个Vtype
http://devjs.eu/en/multiple-validation-multi-vtype-for-textfield-in-ext-js-4/
- node.js中事件触发器events的使用
node.js是基于事件驱动的,通过events,我们可以方便的创建事件,并通过触发事件来调用我们自定义的监听函数. 所有能触发事件的对象都应该是 EventEmitter 类的实例,一般我们自定义一 ...
- React-router4 第二篇url-params url参数
官方文档 以下代码均来自于官方文档 上来一步走 import React from 'react' import { BrowserRouter as Router, Route, Link } fr ...
- c# usercontrol 用户自定义控件无法显示在工具箱的解决办法
由于最开始x86平台,后来换成anyCPU 重新编译也没有删除x86目录下的dll ,也没重新生成 费x86 下的dll , 导致工具箱无法找到目录下的dll 无法加载自定义控件 右击工具栏中的内容选 ...
- Oracle_高级功能(4) 数据库存储结构
数据库存储结构分为:物理存储结构和逻辑存储结构.物理结构和逻辑结构分开,对物理数据的存储不会影响对逻辑结构的访问.1.物理存储结构 数据库文件 os block2.逻辑存储结构 tablespace ...
- 织梦文章里面的图片alt和title属性,用文章标题自动替换
把{dede:field.body/}改成{dede:field.body runphp=yes}global $dsql,$id,$aid;$myid = isset($id) ? $id : $a ...
- 操作系统之CPU管理的直观想法
计算机:是工具,帮助解决实际问题 操作系统,是为了方便使用硬件 计算机模型: 图灵机,模拟人类计算 起初的图灵机就像一个只会做一道菜的厨师 通用图灵机,核心是设置控制器动作(修改控制器),把逻辑读入控 ...
- elasticsearch权威指南
elasticsearch权威指南 https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/
- MAC/Xcode简单操作命令
快捷键: command(windows) + c: 复制 command + V : 粘贴 command + x: 剪切(只在当前应用程序内有效) 在mac系统下表示剪切功能: 先command ...