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的更多相关文章

  1. document.getElementById(...) is null

    <html> <head> <script type="text/javascript"> document.getElementById('b ...

  2. Id.value与document.getElementById("Id").value的区别

    如果标签Id在Form表单里面的话,直接Id.value就不能用了,而是要用Form.Id.value来取值或设置值 所以最好用document.getElementById("Id&quo ...

  3. jquery中的$("#id")与document.getElementById("id")的区别

    以前一直认为jquery中的$("#id")和document.getElementByIdx_x("id")得到的效果是一样的,今天做特效的时候才发现并不是这 ...

  4. 获得输入框的文本document.getElementById('id').value;

    <input id="demo" type="text" value="" > x=document.getElementByI ...

  5. function $(id){ return document.getElementById(id); }导致所有的js不能用解决办法。。。。

    function $(id){ return document.getElementById(id); } document.getElementById(id) 是获得id这个元素的. 相当于定义了 ...

  6. java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"报错

    出现问题的原因: 内存用户验证时,Spring boot 2.0.1引用的security 依赖是 spring security 5.X版本,此版本需要提供一个PasswordEncorder的实例 ...

  7. Spring Security 无法登陆,报错:There is no PasswordEncoder mapped for the id “null”

    编写好继承了WebSecurityConfigurerAdapter类的WebSecurityConfig类后,我们需要在configure(AuthenticationManagerBuilder ...

  8. Document.write和 getElementById(ID)

    在javascript中,document.write()方法:常用来网页向文档中输出内容. 示例:通过document.write()方法,向网页文档中输出了一段文字. document.write ...

  9. document.getElementById(“id”)与$("#id")的区别

    document.getElementById("id")可以直接获取当前对象, jQuery利用$("#id")获取的是一个[object Object],需 ...

随机推荐

  1. 10-okHttp的同步与异步

    我的理解如下: 同步: 实时的在等待返回结果: 异步:可以不是同步执行的,放入到执行队列中. 所以建议:如果需要根绝请求的结构做些判断应当用 同步,异步可能由于时间先后出现问题. /*post异步请求 ...

  2. MultiImageSelector 仿微信选择多张图片回调

    项目可以去github下载 : https://github.com/lovetuzitong/MultiImageSelector 第0步 把模块 multi-image-selector 作为你的 ...

  3. ios NSUserDefaults存储数据(偏好设置)

    ios NSUserDefaults存储数据(偏好设置) 1.NSUserDefaults用于存储数据量小的数据,主要是用户配置,但也可以支持存储一些小数据包括:NSString, NSNumber, ...

  4. Spring 监听

    Spring 中的事件监听的实现 这里我们不讨论事件监听的机制的原理,我们只讨论如何在项目中实现时间监听. spring的事件监听是基于观察者模式.设计开发中.如下类与接口是我们必须要使用的. App ...

  5. Fastdfs 上传图片

    一.fastdfs-client的jar包导入 1.下载地址:https://github.com/happyfish100/fastdfs-client-java 2.将代码使用git下载下来之后, ...

  6. BIO,NIO,AIO(NIO2)的理解

    写在前面,这里所说的IO主要是强调的网络IO 1.BIO(同步并阻塞) 客户端一个请求对应一个线程.客户端上来一个请求(最开始的连接以及后续的IO请求),服务端新建一个线程去处理这个请求,由于线程总数 ...

  7. mysql8.0.4以后修改密码方式变更

    https://blog.csdn.net/qq_38265784/article/details/80915098 use mysql: ALTER USER 'root'@'localhost' ...

  8. SQL截取字符串分隔符中间部门的办法

    需求:实际项目中需要截取第2到第3个逗号中间部分的内容 方案: declare @str nvarchar(50);set @str='11,222,3333,44444';select @str a ...

  9. Java学习笔记:23种设计模式

    设计模式(Design pattern)的定义: In software engineering, a software design pattern is a general, reusable s ...

  10. windows 2003端口80system进程占用的情况

    1.首先是http服务 a. 位置 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\HTTPb. 把 REG_DWORD 类型的项 Start ...