简体   繁体   English

解析XML,如果CDATA不包含HTML标记,则不返回任何字符串

[英]Parsing XML, no string returned if CDATA does not contain an HTML tag

I am using a DOM parser to read rss feeds such as this one within android: 我正在使用DOM解析器来读取android中的rss提要,例如:

...
<item cbc:type="story" cbc:deptid="2.663" cbc:syndicate="true">
<title>
<![CDATA[
Asian carp have reproduced in Great Lakes watershed
]]>
</title>
<link>
http://www.cbc.ca/news/canada/windsor/asian-carp-have-reproduced-in-great-lakes-watershed-1.2286554?cmp=rss
</link>
<guid isPermaLink="false">1.2286554</guid>
<pubDate>Tue, 29 Oct 2013 08:06:48 EDT</pubDate>
<description>
<![CDATA[
<img title='Fisheries and Oceans Canada and the Ontario Ministry of Natural Resources confirmed one grass carp was caught in the Grand River near Lake Erie. ' height='259' alt='hi-20130502-grass_carp-dfo-852' width='460' src='http://i.cbc.ca/1.1663916.1379078358!/httpImage/image.jpg_gen/derivatives/16x9_460/hi-20130502-grass-carp-dfo-852.jpg' /> <p>Scientists said Monday they have documented for the first time that an Asian carp species has successfully reproduced within the Great Lakes watershed, an ominous development in the struggle to slam the door on the hungry invaders that could threaten native fish.</p>
]]>
</description>
</item>
...

xmlParser.class: xmlParser.class:

public class xmlParser {

public Document getDomElement(String rssFilePath, String fileName){
    Log.d("GET", ""+rssFilePath+fileName);
    Document doc = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setCoalescing(true);
    FileInputStream fis;
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        File tmp2 = new File (rssFilePath,"/"+ fileName);
        fis = new FileInputStream(tmp2);

        InputSource is = new InputSource();
            is.setByteStream(fis);
            doc = db.parse(is); 
        } catch (ParserConfigurationException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (SAXException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (IOException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        }
            // return DOM
   // Log.d("DOM", doc.toString());
        return doc;

}

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  ){
                         return child.getNodeValue();
                     }
                 }
             }
         }
         return "";
  } 
}

From my main activity: 从我的主要活动:

//Parse the XML content
            xmlParser parser = new xmlParser();
            Log.d(TAG, "1");
            Document rssDoc = parser.getDomElement(rssFilePath, rssFileName);
            Log.d(TAG, "2");
            final NodeList nl = rssDoc.getElementsByTagName(KEY_ITEM);
            Log.d(TAG, "3");

            //Make it all look nice and strip HTML
            for (int i = 0; i < nl.getLength(); i++){

                Element e = (Element) nl.item(i);

                String noHtmlTitle = parser.getValue(e, KEY_TITLE).toString().replaceAll("\\<.*?>", "");
                noHtmlTitle = noHtmlTitle.replaceAll("/n", "");

                noHtmlTitle = noHtmlTitle.trim();

                titles.add(noHtmlTitle);

                String noHtmlDesc = parser.getValue(e, KEY_DESC).toString().replaceAll("\\<.*?>", "");
                noHtmlDesc = noHtmlDesc.trim(); 
                descs.add("\n" + noHtmlDesc);

            }

However, when this code is presented with the above "title" "/title" tags, it returns a blank string. 但是,当此代码与上述“title”“/ title”标记一起显示时,它将返回一个空白字符串。 This appears to be related to the fact that the "title" tags do not contain any HTML tags. 这似乎与“标题”标签不包含任何HTML标签的事实有关。

How can I retrieve a usable string from the title tags? 如何从标题标签中检索可用的字符串?

Let me know if I have excluded any required data. 如果我已排除任何所需数据,请与我们联系。

Edit: 编辑:

As per blahdiblah, the data type being returned was CDATA_SECTION_NODE. 根据blahdiblah,返回的数据类型是CDATA_SECTION_NODE。 I modified the getElementValue method to include this data type: 我修改了getElementValue方法以包含此数据类型:

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  ){
                         return child.getNodeValue();
                     }else if (child.getNodeType() == Node.CDATA_SECTION_NODE){
                         return child.getNodeValue();
                     }
                 }
             }
         }
         return "";
  } 

Your XMLParser is only returning content for textual nodes ( child.getNodeType() == Node.TEXT_NODE ), but <title> is of type CDATA_SECTION_NODE . 您的XMLParser仅返回文本节点的内容( child.getNodeType() == Node.TEXT_NODE ),但<title>的类型为CDATA_SECTION_NODE

Note that title is almost certainly of being sent as CDATA instead of plain text so that it can include HTML formatting and other odd characters. 请注意,标题几乎肯定是作为CDATA而不是纯文本发送的,因此它可以包含HTML格式和其他奇怪的字符。 Make sure to test with a wide variety of input to make sure that you parse it correctly. 确保使用各种输入进行测试,以确保正确解析它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM