如何的getElementById使用DOM? [英] How to getElementById using DOM?

查看:173
本文介绍了如何的getElementById使用DOM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有HTML页面的一部分给下面并要提取div标签的ID是 hiddenDivHL 使用DOM解析器的内容:

的HTML页面部分:

 < D​​IV 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]
 

到目前为止,我已经使用了低于code,但我无法使用的getElementById 。如何做到这一点?

DOM解析器:

 尝试{

        网址URL =新的URL(http://74.127.61.106/udayavaniIpad/details_android.php?home=1&catid=882&newsid=27593);
        DocumentBuilderFactory的DBF = DocumentBuilderFactory.newInstance();
        DocumentBuilder的DB = dbf.newDocumentBuilder();
        文档的文档= db.parse(新的InputSource(url.openStream()));
        doc.getDocumentElement()归()。

        节点列表节点列表= doc.getElementsByTagName(项目);

        / **指定的TextView阵列lenght由ArrayList的大小* /
        名称=新的TextView [nodeList.getLength()];
        网站=新的TextView [nodeList.getLength()];
        类别=新的TextView [nodeList.getLength()];

        的for(int i = 0; I< nodeList.getLength();我++){

            节点node = nodeList.item(ⅰ);

            名称[I] =新的TextView(本);

            元素fstElmnt =(元)节点;
            节点列表名单= fstElmnt.getElementsByTagName(hiddenDivHL);
            元件nameElement =(元件)nameList.item(0);
            名单= nameElement.getChildNodes();
            名称[I] .setText(NAME =
                    +((节点)nameList.item(0))getNodeValue())。


            layout.addView(名称[I]);



        }
    }赶上(例外五){
        的System.out.println(XML帕兴Excpetion =+ E);
    }

    / **设置布局视图中显示* /
    的setContentView(布局);

}
 

解决方案

有getElementById函数和getElementsByName两点不同:

  1. 的getElementById需要你的文档中一个唯一的ID,而getElementsByName可以取同一个名字的几个occurances。
  2. 的getElementById是文档对象的方法(或功能)。您只可以使用的document.getElementById(..)访问它。

您code似乎违反了这两个要求,你似乎要经过节点的循环,并期望在每一个节点列表中的hiddenDivHL ID。所以ID不是唯一的。第二根点是没有这个文件,但在该列表中的每个节点的根点。

如果你知道你有与该ID的单个实例尝试的document.getElementById。

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

}

解决方案

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.

这篇关于如何的getElementById使用DOM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆