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

最新文章

  1. Visual Studio中没有为此解决方案配置选中要生成的项目
  2. 2012-09-10 23:30 如何解决HtmlAgilityPack得到的InnerText中有残留的script、样式的问题
  3. 自适应滤波原理及simulink
  4. C语言中的break、continue和goto三者的区别与用法
  5. 错误:不存在类型或命名空间名称 &quot;Control&rdquo;
  6. UINavagationController页面跳转
  7. 将DataTable格式化为json字符串返回
  8. (原创)LAMP教程2-安装虚拟机软件VirtualBox
  9. Discuz论坛下载与安装
  10. OpenCV-Python教程(10、直方图均衡化)
  11. Centos更换yum源,安装ssh server
  12. Android 开发笔记___复选框__checkbox
  13. 利用postman 实现Get和Post测试
  14. php7-编译安装参数
  15. Anatomy of a Database System学习笔记 - 公共模块、结语
  16. BeanFactory中Bean的生命周期
  17. 20172325 2018-2019-2 《Java程序设计》第五周学习总结
  18. 【Zookeeper】源码分析之持久化(一)之FileTxnLog
  19. webpack window下配置的hello world
  20. A SQL to insert continuous values

热门文章

  1. 【RabbitMQ】——5种队列(转)
  2. Socket(转自 阿里云)
  3. iconfont项目成员添加不进去的问题
  4. Java并发-ConcurrentModificationException原因源码分析与解决办法
  5. c#Md5 32位加密结果少了两个0的原因
  6. IIS站点工作原理与ASP.NET工作原理
  7. Liunx read
  8. PAT 1076 Wifi密码(15)(代码)
  9. How to install VCM 2 Ford IDS 109 software
  10. oracle主键修改&amp;设置某一字段可以为null