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. SI和DI

    si和di是8086CPU中和bx功能相近的寄存器,si和di不能够分成两个8位寄存器来使用. 用si和di实现将字符串"welcome to masm!"复制到它后面的数据区中. ...

  2. vue tab切换

    <template> <div class="box"> <ul> <li v-for="(item,index) in arr ...

  3. python 使用内置方法读取注册表

    #coding:utf-8 import _winreg key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,r"键值") #获取该键的 ...

  4. Socket(转自 阿里云)

    摘要:Socket-C/S Socket又称"套接字",应用程序 通常通过"套接字"向网络发出请求或者应答网络请求,使主机间或者一台计算机上的进程间可以通讯. ...

  5. 如何指定vim 的查找是从上往下还是从下往上[z]

    vim 搜索可以是 / 或者 ?,前者是往下找,后者是往前找. 用 n 查找下一个的时候,就和这两个指令指定的方向相同.如果你想改变方向的话,比如想往下找,那么 / 完了直接回车就行了.表示再次使用上 ...

  6. 如何在Fragment中获取context

    文章转载自http://blog.csdn.net/demonliuhui/article/details/51511136 这里仅供自己学习参考: Context,中文直译为“上下文”,SDK中对其 ...

  7. UI和View 三种控制方式

    AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xm ...

  8. Mongodb数据导出工具mongoexport和导入工具mongoimport介绍(转)

    原文地址:http://chenzhou123520.iteye.com/blog/1641319 一.导出工具mongoexport Mongodb中的mongoexport工具可以把一个colle ...

  9. Spring Environment(三)生命周期

    Spring Environment(三)生命周期 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Envi ...

  10. solr之创建core(搜索核心,包括索引和数据)的方法

    我的solrhome为D:\solrHome\solr step1:进入solrHome会看到collection1文件夹,创建该文件夹的副本,重命名为product 进入product文件夹,进入d ...