How to getElementById to use the DOM?

I have the part of the HTML page below and you want to extract the contents of the div tag, its id hiddenDivHL using DOM Parser:

Part of the HTML page:

 <div id='hiddenDivHL' style='display:none'>http://74.127.61.106/udayavaniIpad/details.php?home=0&catid=882&newsid=123069[InnerSep]http://www.udayavani.com/udayavani_cms/gall_content/2012/1/2012_1$thumbimg117_Jan_2012_000221787.jpg[InnerSep]ಯುವಜನತೆಯಿಂದ ಭವ್ಯಭಾರತ[OuterSep] 

So far I have used the code below, but I cannot use getElementById . How to do it?

DOM Parser:

  try { URL url = new URL("http://74.127.61.106/udayavaniIpad/details_android.php?home=1&catid=882&newsid=27593"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new InputSource(url.openStream())); doc.getDocumentElement().normalize(); NodeList nodeList = doc.getElementsByTagName("item"); /** Assign textview array lenght by arraylist size */ name = new TextView[nodeList.getLength()]; website = new TextView[nodeList.getLength()]; category = new TextView[nodeList.getLength()]; for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); name[i] = new TextView(this); Element fstElmnt = (Element) node; NodeList nameList = fstElmnt.getElementsByTagName("hiddenDivHL"); Element nameElement = (Element) nameList.item(0); nameList = nameElement.getChildNodes(); name[i].setText("Name = " + ((Node) nameList.item(0)).getNodeValue()); layout.addView(name[i]); } } catch (Exception e) { System.out.println("XML Pasing Excpetion = " + e); } /** Set the layout view to display */ setContentView(layout); } 
+6
source share
7 answers

XPath is IMHO the most common and easiest way to navigate the DOM in Java.

 try{ URL url = new URL("http://74.127.61.106/udayavaniIpad/details_android.php?home=1& catid=882&newsid=27593"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new InputSource(url.openStream())); doc.getDocumentElement().normalize(); XPath xpath = XPathFactory.newInstance().newXPath(); String expression = "/item/div[@id='hiddenDivHL']"; Node node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE); } catch (Exception e) { System.out.println("XML Pasing Excpetion = " + e); } 

I'm not sure the XPath expression is right, but the link is here: http://developer.android.com/reference/javax/xml/xpath/package-summary.html

+2
source

There are two differences between getElementById and getElementsByName:

  • getElementById requires one unique identifier in your document, while getElementsByName can retrieve multiple instances with the same name.
  • getElementById is a method (or function) of a document object. You can access it only with document.getElementById (..).

Your code seems to violate both of these requirements, you seem to go through a loop of nodes and expect each node list to contain the hiddenDivHL identifier. Thus, the identifier is not unique. Secondly, your root point is not a document, but the root point of each node in this list.

If you know that you have one instance with this identifier, try document.getElementById.

+1
source

I really don't get it.

a) Do you mean getting more document.getElementById ('hiddenDivHL') elements?

  • so my answer would be that in an HTML document, an identifier should only be reserved for one element.

b) If you just want to catch this element?

  • What exactly doesn’t work? what are you trying to achieve? I'm afraid I really don't get it.
0
source

You must call fstElmnt.getElementsByTagName("div"); to get all div elements and they check if their hiddenDivHL attribute id is hiddenDivHL .

0
source

The easiest way I can come up with is to use jSoup , what it does is analyze the DOM for you and allow you to select elements using the css (or jQuery style) style selector.

in this example you would do something like this

 Document doc = Jsoup.connect("http://74.127.61.106/udayavaniIpad/details_android.php?home=1&catid=882&newsid=27593").get(); String divContents = doc.select("#hiddenDivHL").first().text(); 
0
source

Why can't you use getElementById ()? It is in JavaSE 7 and JavaSE6 / 5 / 1.4.2, since it is "DOM Level 2".

0
source

Get the contents of an element in JavaScript:

 var el = document.getElementById('hiddenDivHL'); var contents = el.innerHTML; alert("Found " + contents.length + " characters of content."); 

See your example in jsfiddle .

I think the confusion is due to the fact that JavaScript is flagged for your question, but you sent Java. These are different languages, and JavaScript users will only get confused by this parser. I have not used Java for many years, so I can not help you.

-5
source

Source: https://habr.com/ru/post/906195/


All Articles