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

</{0,1}.+?>必要に応じて代わりにこの正規表現を使用してください。ここで私の例<.*?/>を見ることができます

\nそして、最初のようにそのテキスト内のすべてを削除するのが最善です:

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

私の理解が正しければ、生成している XML で HTML タグがエラーを引き起こしていることが問題です。この答えはそれを助けるでしょう: AndroidでXMLをエンコードする方法は?

山かっこを削除しようとしているだけの場合は、おそらく正規表現なしで簡単に削除できます。最悪の場合、文字列の文字をループして、どれが '<' または '>' であるかをテストし、それらを変更または削除することもできます。

于 2013-07-25T03:06:58.657 に答える