日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

doc.getElementById(id); null

發(fā)布時(shí)間:2023/12/13 综合教程 29 生活家
生活随笔 收集整理的這篇文章主要介紹了 doc.getElementById(id); null 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
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);
    }
}

輸出結(jié)果是:null

原因見紅色字內(nèi)容

如何解決:

為該文檔聲明DTD

其中有一種內(nèi)部聲明方法如下:

<?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>

其中內(nèi)部聲明方法的解釋是:

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

總結(jié)

以上是生活随笔為你收集整理的doc.getElementById(id); null的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。