zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    UIAutomator环境搭建
    Appium环境搭建
    Java单元测试 Junit TestNG之介绍
    IDEA操作jdbc总结
    tomcat启动失败的解决办法
    Java 图书管理项目
    某某服-EDR终端任意用户登录 0day
    深X服 EDR终端检测系统RCE漏洞复现
    通达OA任意文件上传+文件包含RCE漏洞复现(附自写EXP)
    Joomla-3.4.6远程代码执行漏洞复现
  • 原文地址:https://www.cnblogs.com/zno2/p/6475796.html
Copyright © 2011-2022 走看看