Парсинг xml только получение CDATA внутри тега
Я пытаюсь получить текст из 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 ответа
Решение
Я смог избавиться от 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));
}
Добавьте тег записи в переменную, затем используйте строку replace, чтобы избавиться от cdata, а затем покажите весь текст внутри тегов. Работает отлично.
Просто поменяй
if(child.getNodeType() == Node.TEXT_NODE)
в
if (child.getNodeType() == Node.CDATA_SECTION_NODE || child.getNodeType() == Node.TEXT_NODE)
в файле XMLParser.java.