Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to getElementById using DOM?

I am having part of HTML page given below and want to extract the content of div tag its id is hiddenDivHL using DOM Parser:

Part Of a 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 below code but I am unable to use getElementById.How to do that?

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);

}
like image 1000
Jack Dsilva Avatar asked Jan 17 '12 11:01

Jack Dsilva


2 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 if the XPath expression is right, but the link is here: http://developer.android.com/reference/javax/xml/xpath/package-summary.html

like image 52
Roman K Avatar answered Sep 24 '22 07:09

Roman K


There are 2 differences between getElementById and getElementsByName:

  1. getElementById requires a single unique id in your document, whereas getElementsByName can fetch several occurances of the same name.
  2. getElementById is a method (or function) of the document object. You can only access it by using document.getElementById(..).

Your code seems to violate both these requirements, you seem to go through a loop of nodes and expect a hiddenDivHL id in each node list. So the id is not unique. Second your root point is not the document but the root point of each node in that list.

If you know you have a single instance with that id try document.getElementById.

like image 22
EtienneSky Avatar answered Sep 25 '22 07:09

EtienneSky