使用Regex删除检索到的XML数据的特殊字符

时间:2013-07-25 01:53:54

标签: java android regex xml tags

我检索了一些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);




        }

2 个答案:

答案 0 :(得分:0)

如果我理解正确,问题是HTML标记会导致您生成的XML出错。这个答案将有助于:How to encode XML on Android?

如果您只是想删除尖括号,那么在没有正则表达式的情况下执行它可能同样容易。在最坏的情况下,你可以循环遍历字符串字符,测试哪些是'&lt;'或'&gt;',并更改或删除它们。

答案 1 :(得分:0)

如果需要,请使用此正则表达式</{0,1}.+?>代替<.*?/>,您可以看到my example here

最好首先删除该文本中的所有\n

someStrings.replaceAll("\n", " ").replaceAll("</{0,1}.+>", "");