0

我检索了一些 Web 服务 xml 数据,并且一些标签由诸如<br><p>使我的数据无法处理的标签组成。我可以知道如何使用正则表达式删除特殊字符,任何人都可以向我建议为什么我的描述在我运行时没有打印在 UI 上?这是我的数据检索代码:

static final String URL = "http://api.eventful.com/rest/events/search?app_key=42t54cX7RbrDFczc&location=singapore";
    // XML node keys

    static final String KEY_DESC = "description";

    String description = KEY_DESC.replaceAll("<.*?/>", "");

for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            map.put(description, "Description: " + parser.getValue(e, description));            
            // adding HashList to ArrayList
            menuItems.add(map);

        }
ListAdapter adapter = new SimpleAdapter(this, menuItems,
                R.layout.list_item, new String[] {description}, new int[] {R.id.description});

String description1 = ((TextView) view
                        .findViewById(R.id.description)).getText().toString();

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        SingleMenuItemActivity.class);
                in.putExtra(KEY_TITLE, title);
                in.putExtra(KEY_DESC, description1);

                startActivity(in);

以下是特殊字符描述的示例:

<p><strong>All Real Estate Agents!</strong><p><strong>INCREASE Your Sales
Selling Commercial  & Industrial</strong><p><strong>even with the LATEST
MAS MEASURES!</strong><p><strong>by The Comm/Ind Consultant - David
Poh</strong><p><strong> </strong><p>Do you know that:<ul><li>Comm/Ind Real
Estate will be the star performer for the year 2013/2014.<li>Comm/Ind Real
Estate investment enjoys both High Yield and Capital Gain.<li>Comm/Ind Real
Estate investment is not affected by cooling
measures.</li></li></li></ul><br><strong>How You Will
Benefit</strong><ul><li>Break through your income<li>Work-life balance -
office hours only<li>Serve and advice investors better<li>Investor
retention<li>Hot Spots to advice investors to invest<li>Learn more about
technical aspects<li>Common pitfalls to avoid<li>Updates on real estate
market</li></li></li></li></li></li></li></li></ul><br><p> <strong>THE
SPEAKER: DAVID POH</strong><p> <p>David Poh has spent many years studying
and understanding the commercial & industrial real estate market in
Singapore. He manages companies that specialize in commercial & industrial
investments, investments training, and real estate funds. He has more than
15 years of real estate experience and has trained thousands of
practitioners in real estate. s achievement is prominent as he is the first
and Life Long Champion  winner in PropNex, s largest real estate company.
Today he is leading the biggest team in  David Poh & Associates. He is also
a sought-after trainer in this arena. He has trained many top-notch real
estate agents, who remain top producers in the industry today. David is
often interviewed on TV (Channel 5/8/U/NewsAsia), radio, and newspaper for
his views and analysis in real estate market trends and related
issues.<p><strong>Come hear for yourselves on how you could make money from
Comm/Ind Investments.</strong><p> <p><strong>Free 4 Hours Intensive
Workshop Dates </strong><p>Date: 26 July 2013 (Friday), 7 August 2013
(Wednesday)<p>Time:  6.30pm<p>Venue: 10 Anson Road International Plaza
#12-12, S(079903)<p><br><p><a href="http://www.asiawisdom.com.sg/"
rel="nofollow">Visit us at
asiawisdom.com.sg</a></p></p></p></p></p></p></p></p></p></p></p></p></p></p></p></p></p>2.30pm
PropNex SingaporeAwardonly David

这是编辑后的代码:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_EVENT);
        // looping through all item nodes <item>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
            map.put(KEY_URL, parser.getValue(e, KEY_URL));
            map.put(KEY_DESC, "Description: " + parser.getValue(e, KEY_DESC));
            map.put(KEY_START_TIME, parser.getValue(e, KEY_START_TIME));
            map.put(KEY_STOP_TIME, parser.getValue(e, KEY_STOP_TIME));
            map.put(KEY_VENUE_NAME, parser.getValue(e, KEY_VENUE_NAME));
            map.put(KEY_COUNTRY_NAME, parser.getValue(e, KEY_COUNTRY_NAME));
            // adding HashList to ArrayList
            KEY_DESC.replaceAll("\n","").replaceAll("</{0,1}.+?>", "");

            menuItems.add(map);




        }
4

2 回答 2

0

如果我理解正确,问题是 HTML 标记在您生成的 XML 中导致错误。这个答案将对此有所帮助:如何在 Android 上编码 XML?

如果您只是想删除尖括号,那么没有正则表达式可能同样容易。在最坏的情况下,您可以遍历字符串字符,测试哪些是'<'或'>',然后更改或删除它们。

于 2013-07-25T03:06:58.657 回答
0

如果需要,请使用此正则表达式</{0,1}.+?>,您可以在此处查看我的示例<.*?/>

最好先删除\n该文本中的所有内容,例如:

someStrings.replaceAll("\n", " ").replaceAll("</{0,1}.+>", "");
于 2013-07-25T04:33:03.343 回答