I am trying to get a html node from a file that will later be used to count all of its descendants. I am having issues with retrieving the element from the DOM. here are the steps i have taken so far.
First here is my html code:
<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:
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.