【问题标题】:Android Parse xml with DOM - same named tagsAndroid Parse xml with DOM - 同名标签
【发布时间】:2013-05-30 17:47:08
【问题描述】:

我有一个 rss 提要,在每个项目标签中都有两个名为 category 的标签。我想得到第一个值,但不幸的是我得到了第二个值。这是我的代码:

   // Create required instances
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();

                // Parse the xml
                Document doc = db.parse(new InputSource(url.openStream()));
                doc.getDocumentElement().normalize();

                // Get all <item> tags.
                NodeList nl = doc.getElementsByTagName("item");
                int length = nl.getLength();

                // to length einai posa nea tha emfanisei.Edw tou lew ola pou
                // vriskei
                for (int i = 0; i < length; i++) {
                    Node currentNode = nl.item(i);
                    RSSItem _item = new RSSItem();

                    NodeList nchild = currentNode.getChildNodes();
                    int clength = nchild.getLength();

                    // Get the required elements from each Item
                    for (int j = 0; j < clength; j = j + 1) {

                        Node thisNode = nchild.item(j);
                        String theString = null;

                        if (thisNode != null && thisNode.getFirstChild() != null) {
                            theString = thisNode.getFirstChild().getNodeValue();

                        }

                        if (theString != null) {

                            String nodeName = thisNode.getNodeName();
                      ...
....
...

                            if ("category".equals(nodeName)) {

                                /*
                                 * NodeList nlList = nl.item(0).getChildNodes();
                                 * Node nValue2 = (Node) nlList.item(0);
                                 * _item.setCategory(nValue2.getNodeValue());
                                 */

                                _item.setCategory(theString);
                            }
                        }
                    }

编辑:

我的 RSS 提要是这样的:

<item>
<title>my title</title>
<category>Sports</category>
<category>World</category>
</item>

<item>
<title>my title 2</title>
<category>News</category>
<category>Showbiz</category>
</item>
...etc

【问题讨论】:

  • 请显示您的 rss xml 以获得更多帮助
  • 你试过Element element = (Element) currentNode; NodeList nodelist = element.getElementsByTagName("category"); Element element1 = (Element) nodelist.item(0); NodeList category = element1.getChildNodes(); System.out.print("category : " + (category.item(0)).getNodeValue());
  • 你是最棒的!!那行得通!!请把它作为答案发布以接受它!

标签: android dom xml-parsing rss


【解决方案1】:

如果您想从当前的item 节点获取第一个category,则使用Element.getElementsByTagName("category") 获取NodeList 中的所有类别节点,然后使用NodeList.item(0) 从NodeList 获取第一个类别Element这样做:

Element element = (Element) currentNode;
NodeList nodelist = element.getElementsByTagName("category");
Element element1 = (Element) nodelist.item(0);
NodeList category = element1.getChildNodes();
System.out.print("category : " + (category.item(0)).getNodeValue());

【讨论】:

  • 我怎样才能在上面的 rss 提要中同时获得这两个类别? (category.item(1)).getNodeValue() 是这样吗??
猜你喜欢
  • 2013-06-29
  • 2012-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多