简体   繁体   English

无法使用document.getElementById获取元素,返回null

[英]not able to get an element using the document.getElementById, returning null

I am trying to get a html node from a file that will later be used to count all of its descendants. 我试图从一个文件中获取一个html节点,该文件稍后将用于计算其所有后代。 I am having issues with retrieving the element from the DOM. 我在从DOM中检索元素时遇到问题。 here are the steps i have taken so far. 这是我到目前为止所采取的步骤。

First here is my html code: 首先是我的HTML代码:

<html>
<head>
    <title></title>
</head>
<body>
<div id="container">
    <a></a>
    <div id="header">
        <div id="firstchild">
            <div>
                <img></img>
            </div>
            <a></a>
            <ul>
                <li>
                    <a>Inbox</a>
                </li>
                <li>
                    <a>Logout</a>
                </li>
            </ul>
            <form></form>
        </div>
        <div id="nextsibling"></div>
    </div>
</div>
</body>
</html>

Second I built this function that will return and parse the file into a document. 其次,我构建了这个函数,它将文件返回并解析为文档。

public static Document buildDocument(String file){
    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document document = docBuilder.parse(file);
        return document;
    } catch (ParserConfigurationException | SAXException | IOException ex) {
        System.out.println("the exception is: " + ex.toString());
    }
    return null;
}

Next in my main method I tried to set a Node object to a document elemet by way of getElementById like: 接下来在我的main方法中,我尝试通过getElementById将Node对象设置为文档elemet,如:

public Document doc = buildDocument("myHTMLFile");
org.w3c.dom.Node node = doc.getElementById("header");//the id of an html element

Correct me if I am wrong but this should result in the retreival of the node. 如果我错了,请纠正我,但这应该导致节点的恢复。 However it is returning a null value. 但是它返回一个空值。 I do not understand why it is not returning the correct value. 我不明白为什么它没有返回正确的值。 NOTE: that when debugging the code the document does contain all of the correct data as far as I can tell. 注意:在调试代码时,文档确实包含所有正确的数据,据我所知。

You do it wrong. 你做错了。 Javadoc javadoc of getElementById said: getElementById的 Javadoc javadoc说:

Returns the Element that has an ID attribute with the given value. 返回具有给定值的ID属性的Element。 If no such element exists, this returns null . 如果不存在此类元素,则返回null。 ... The DOM implementation is expected to use the attribute Attr.isId to determine if an attribute is of type ID. ... DOM实现应该使用属性Attr.isId来确定属性是否为ID类型。 Note: Attributes with the name "ID" or "id" are not of type ID unless so defined. 注意:除非如此定义,否则名称为“ID”或“id”的属性不是ID类型。

In your case the best solution is using XPath (simple query language to XML): 在您的情况下,最好的解决方案是使用XPath (XML的简单查询语言):

XPath xpath = XPathFactory.newInstance().newXPath();
Node node = (Node) xpath.evaluate("//*[@id='header']", document, XPathConstants.NODE);

Expression //*[@id='header'] - select all nodes in document which has attribute id with 'header' value. 表达式// * [@ id ='header'] - 选择文档中具有'header'值的属性id的所有节点。

It appears you are working with the generic XML DOM. 您似乎正在使用通用XML DOM。 XML expects IDs to be defined as such, so an element with an attribute, even if named "id", won't work unless designated as such. XML期望ID被定义为这样,因此具有属性的元素(即使名为“id”)将不起作用,除非指定为此类。

Try finding an HTML-specific interface or adding a DOCTYPE which defines the id attribute as an ID type. 尝试查找特定于HTML的界面或添加将ID属性定义为ID类型的DOCTYPE。 (I wouldn't recommend the latter though because HTML5 has moved away from attempting an XHTML compatible approach even if it technically supports an XHTML serialization.) See Parse Web Site HTML with JAVA for recommendations on HTML-specific parsers. (我不推荐后者,因为HTML5已经远离尝试XHTML兼容的方法,即使它在技术上支持XHTML序列化。)请参阅使用JAVA解析网站HTML以获取有关HTML特定解析器的建议。

Try doing the following two things: 尝试做以下两件事:

  1. In your buildDocument() function, add the following line: 在buildDocument()函数中,添加以下行:

      Element element = document.getDocumentElement(); 
  2. Change the return type of the function to "Element" and return "element" 将函数的返回类型更改为“Element”并返回“element”

  3. Create an "Element" object instead of a "Document" object and call "getElementById()" on it. 创建一个“Element”对象而不是“Document”对象,并在其上调用“getElementById()”。
  4. Also check the name of the file, if you are missing .html extension 如果缺少.html扩展名,请检查文件名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM