Android使用DOM解析xml-同名标记

Android使用DOM解析xml-同名标记,android,dom,xml-parsing,rss,Android,Dom,Xml Parsing,Rss,我有一个rss提要,在每个条目标记中有两个名为category的标记。我想得到第一个值,但不幸的是我得到了第二个值。这是我的代码: // Create required instances DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder();

我有一个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);
                            }
                        }
                    }
//创建所需的实例
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
//解析xml
Document doc=db.parse(新的输入源(url.openStream());
doc.getDocumentElement().normalize();
//获取所有标签。
NodeList nl=doc.getElementsByTagName(“项目”);
int length=nl.getLength();
//到埃姆法尼塞岛的长度,从东到西
//弗里斯凯
for(int i=0;i
编辑:

我的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

我的头衔
体育
世界
我的标题2
新闻
娱乐圈
等

如果您想从当前的
项目
节点中首先获取
类别
,则使用
元素。getElementsByTagName(“类别”)
获取
节点列表中的所有类别节点
之后,使用
节点列表.item(0)
节点列表
获取第一个类别
元素

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

plz显示您的rss xml以获取更多帮助您是否将其作为
Element Element=(Element)currentNode;NodeList NodeList=Element.getElementsByTagName(“类别”);Element element1=(Element)NodeList.item(0);NodeList category=element1.getChildNodes();System.out.print(“类别:+(category.item(0)).getNodeValue())
你是最棒的!!这很有效!!请将其作为答案发布!我如何在上面的rss提要中同时获得这两个类别???(category.item(1))。getNodeValue()是这样吗??