解析xml只能在Tag中获取CDATA

时间:2013-08-20 05:47:16

标签: android xml-parsing android-query

我正在尝试从xml文件中获取文本,如下所示:

<description>
<p>
<strong>Last updated:</strong>
 Mon, 19 Aug 2013 23:52:31</p>
<p>Incident is 53% contained.</p>
<![CDATA[<p>The American Fire burning in heavy fuels on extreme slopes about 10 air miles northeast of the community of Foresthill, California, and eight air miles south of Interstate 80 has grown to 14,765 acres.</p> <p><strong>The public is invited to an American Fire update meeting at the Foresthill Veteran's Memorial Hall at 24601 Harrison Street in Foresthill beginning at 7 p.m. tonight.</strong></p> <p>Heavy smoke shaded the fire yesterday, moderating fire behavior. Backing fire with single and group tree torching was observed. On the northeast corner a spot fire was quickly contained by firefighters as they made good progress with hand lines and dozer lines. Along the eastern portion of the fire last night, firefighters conducted a firing operation, meaning they used fire to reduce unburned fuel between the fire line and the main fire. The center portion of the east flank was still very active during the day, but indirect containment lines remained secure. On the extreme south end, firefighters will begin building a very steep hand line today, which descends to the river. The west side of the fire was relatively inactive. Mop-up is occurring in this area, which involves checking the interior of the fire to ensure no hot spots remain that may threaten the containment lines.</p> <p>Firefighters continue to be concerned about dry fuels that have not seen fire in over a century, as well as any winds over 5 m.p.h. and rolling burning debris, all of which could cause a rapid spread of the fire.</p> <p>The National Weather Service has issued a Red Flag Warning for the fire area beginning at 11 a.m. today and extending through 11 p.m. Wednesday. This Warning is due to the threat of abundant lightning and gusty, erratic outflow winds. Significant rainfall and flooding in and around the fire is also possible over the next three days.</p> <p>The Robinson Flat Campground is closed. The Tahoe National Forest has issued a voluntary evacuation notice for Big Oak Flat located near the south end of the fire. Forest Road 43 (Robinson Flat Road) is closed at its intersection with Forest Road 96 (Mosquito Ridge Road).</p> <p>An emergency closure order is in place for portions of National Forest System lands within and adjacent to the American Fire. A map and description of the closed area can be obtained at Tahoe National Forest offices as well as online at <a href="http://www.fs.usda.gov/tahoe">http://www.fs.usda.gov/tahoe</a>. Portions of the Foresthill Divide road are closed.</p> <p><strong>At 6 a.m. today, management of the fire was transferred to the California Interagency Management Team 4.</strong></p> <p>Firefighter and public safety are the highest priority.</p>]]>
<p>
<a href="http://inciweb.nwcg.gov/incident/3624/">View American Wildfire web site</a>
</p>
<p>
<strong>NOTE: </strong>
 All fire perimeters and points are approximations.</p>
</description>

当我解析它时,我可以获取CDATA区域内的所有信息,但其余部分将被忽略。我正在解析并将其放入我的文本视图中:

description.setText(extras.getString("desc"));

我正在使用android查询,并且可以让它格式化没问题:

aq.id(R.id.description).text(Html.fromHtml(extras.getString("desc")));

然而,同样的问题,只是获取cdata信息。我的log.v()只显示了cdata之间的信息。所以我想我需要以某种方式逃脱它?为什么忽略cdata之外的文本?

由于 感谢

2 个答案:

答案 0 :(得分:2)

只需更改

if(child.getNodeType() == Node.TEXT_NODE)

if (child.getNodeType()  == Node.CDATA_SECTION_NODE || child.getNodeType() == Node.TEXT_NODE)
XMLParser.java文件中的

答案 1 :(得分:0)

我能够以这种方式摆脱cdata:

for (XmlDom entry : entries) {
            XmlDom description = entry.tag("description");
            String cdatareplace = description.toString();
            String desc = cdatareplace.replace("<![CDATA[", "");
            desc = desc.replace("]]>", "");
            kmllist.add(new KML(entry.text("name"), desc));
        }

将条目标记添加到变量中,然后使用字符串替换来删除cdata,然后显示标记中的所有文本。工作良好。