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. 如何指定vim 的查找是从上往下还是从下往上[z]

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

  2. vue slot插槽的使用方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. c# ?. 空值传播运算符

    当左侧为空时不执行右侧代码,避免出现为null的错误,同时也避免了判断是否为null,可以和??一起连用,省了好多事.举例如下: 以前:var res=obj==null?5:obj.a; 现在:va ...

  4. 扩展、委托、Lambda、linq

    1.扩展 扩展是一个很有用的功能.如果你有一个类.不能修改,同时你又想给他加一个方法.这个过程就是扩展.扩展就是扩展方法. 例1: 类People public class People { publ ...

  5. get(0).tagName获得作用标签

    <script type="text/javascript" src="jquery1.4.js"></script><scrip ...

  6. javascript 高级程序设计 一

    前言: 作为一个即将毕业.正在实习的大学生,我也默默的进入了开发者的行列.从一开始的c#编码狗到java程序员再到现在的JS开发者,我一直 希望自己可以在这个'万恶'的互联网时代走的更远.但是我还是一 ...

  7. LocalStorage的一些使用

    LocalStorage是什么 LocalStorage 是在Html5中出现的一种本地存储.说到本地存储,大家立马会联想到Cookie,还有SqlLite. LocalStorage 中的数据不会像 ...

  8. Codeforces 798C. Mike and gcd problem 模拟构造 数组gcd大于1

    C. Mike and gcd problem time limit per test: 2 seconds memory limit per test: 256 megabytes input: s ...

  9. 如果CocoaPods 导入的库需要修改代码

      如果经常要修改第三方框架的话,可以将需要修改的第三方库fork一份到自己的github,在里面做完修改之后,将podfile修改为: platform :ios, '7.0' pod '要导入的库 ...

  10. 去掉tableView空白区域的分割线

    //把多余的分割线去掉 UIView * footerView = [[UIView alloc] initWithFrame:CGRectZero]; self.tableView.tableFoo ...