Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android rss无法解析具有属性的XML Document doc=getdoelement(响应);//获取DOM元素 NodeList nl=doc.getElementsByTagName(键项); //循环通过所有项目节点 对于(int i=0;i_Java_Android_Xml_Dom_Rss - Fatal编程技术网

Java Android rss无法解析具有属性的XML Document doc=getdoelement(响应);//获取DOM元素 NodeList nl=doc.getElementsByTagName(键项); //循环通过所有项目节点 对于(int i=0;i

Java Android rss无法解析具有属性的XML Document doc=getdoelement(响应);//获取DOM元素 NodeList nl=doc.getElementsByTagName(键项); //循环通过所有项目节点 对于(int i=0;i,java,android,xml,dom,rss,Java,Android,Xml,Dom,Rss,在上面,响应是一个XML rss提要,下面是一个子项。现在发生的事情是我能够得到标题,出版,更新。但当我使用getValue(e,“content”)时,我得到的是空字符串。我还想知道作者的名字 Document doc = getDomElement(response); // getting DOM element NodeList nl = doc.getElementsByTagName(KEY_ITEM); // looping thro

在上面,响应是一个XML rss提要,下面是一个子项。现在发生的事情是我能够得到标题,出版,更新。但当我使用getValue(e,“content”)时,我得到的是空字符串。我还想知道作者的名字

Document doc = getDomElement(response); // getting DOM element
            NodeList nl = doc.getElementsByTagName(KEY_ITEM);
            // looping through all item nodes <item>
            for (int i = 0; i < nl.getLength(); i++) {
                Element e = (Element) nl.item(i);
                String name = getValue(e, KEY_NAME);
                String description = getValue(e, KEY_DESC);
                Log.e("description:", description);
            }

public String getValue(Element item, String str) {
    NodeList n = item.getElementsByTagName(str);
    return this.getElementValue(n.item(0));
}

public final String getElementValue(Node elem) {
    Node child;
    if (elem != null) {
        if (elem.hasChildNodes()) {
            for (child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
                if ((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.ELEMENT_NODE) ) {
                    return child.getNodeValue();
                }
            }
        }
    }
    return "";
}

标题1
身份证件
2012-09-08T18:45:40Z
2012-09-08T18:43:01Z
作者姓名
http://www.example.com
pTest试验
在代码中

<entry>
  <title>Title1</title>
  <link rel="alternate" type="text/html" href="http://www.example.com" />
  <id>ID</id>

  <published>2012-09-08T18:45:40Z</published>
  <updated>2012-09-08T18:43:01Z</updated>
  <author>
      <name>Author name</name>
      <uri>http://www.example.com</uri>
  </author>
  <content type="html" xml:lang="en" xml:base="http://www.example.com/">
      &lt;p&gt;Test Test</content>
</entry>
您仅从第一个子文本节点获取文本。内容可以拆分为多个文本节点。您可能希望从所有子文本节点收集文本

public final String getElementValue(Node elem) {
    Node child;
    if (elem != null) {
        if (elem.hasChildNodes()) {
            for (child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
                if ((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.ELEMENT_NODE) ) {
                    return child.getNodeValue();
                }
            }
        }
    }
    return "";
}
要获取Author name值,您需要首先在层次结构中的另一个级别下一步,因为“name”标记嵌套在“Author”标记中。这意味着在遍历顶级节点以定位“author”节点,然后获取其子节点“name”节点时需要进行一些特殊处理

public final String getElementValue(Node elem) {
    Node child;
    StringBuilder sb = new StringBuilder();
    if (elem != null) {
        if (elem.hasChildNodes()) {
            for (child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
                if ((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.ELEMENT_NODE) ) {
                    sb.append(child.getNodeValue());
                }
            }
        }
    }
    return sb.toString();
}