5

私はAndroidを初めて使用します。アプリケーションではデータを解析する必要があり、画面に表示する必要があります.しかし、ある特定のタグデータでは、そのタグ内にも特殊文字が含まれているため、理由を解析できません.私のコードを表示します。

私のパーサー関数:

  protected ArrayList<String> doInBackground(Context... params) 
    {
//      context = params[0];
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();     
        test = new ArrayList<String>();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new java.net.URL("input URL_confidential").openConnection().getInputStream());
            //Document document = builder.parse(new URL("http://www.gamestar.de/rss/gamestar.rss").openConnection().getInputStream());
            Element root = document.getDocumentElement();
            NodeList docItems = root.getElementsByTagName("item");
            Node nodeItem;
            for(int i = 0;i<docItems.getLength();i++)
            {
                nodeItem = docItems.item(i);
                if(nodeItem.getNodeType() == Node.ELEMENT_NODE)
                {
                    NodeList element = nodeItem.getChildNodes();                    
                    Element entry = (Element) docItems.item(i);
                    name=(element.item(0).getFirstChild().getNodeValue());




//                 System.out.println("description = "+element.item(2).getFirstChild().getNodeValue().replaceAll("&lt;div&gt;&lt;p&gt;"," "));
                    System.out.println("Description"+Jsoup.clean(org.apache.commons.lang3.StringEscapeUtils.unescapeHtml4(element.item(2).getFirstChild().getNodeValue()), new Whitelist()));             


                    items.add(name);


                }
            }
        } 
        catch (ParserConfigurationException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (MalformedURLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (SAXException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return items;
    }

入力:

<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>my application</title>
<link>http:// some link</link>
<atom:link href="http:// XXXXXXXX" rel="self"></atom:link>
<language>en-us</language>
<lastBuildDate>Thu, 20 Dec 2012</lastBuildDate>
<item>
<title>lllegal settlements</title>
<link>http://XXXXXXXXXXXXXXXX</link>
<description> &lt;div&gt;&lt;p&gt;
India was joined by all members of the 15-nation UN Security Council except the US to condemn Israel’s announcement of new construction activity in Palestinian territories and demand immediate dismantling of the “illegal†settlements.
&lt;/p&gt;
&lt;p&gt;
UN Secretary General Ban Ki-moon also expressed his deep concern by the heightened settlement activity in West Bank, saying the move by Israel “gravely threatens efforts to establish a viable Palestinian state.â€
&lt;/p&gt;
&lt;p&gt;
</description>
</item>
</channel>

出力:

 lllegal settlements  ----> title tag text

     India was joined by all members of the 15-nation UN Security Council except the US to condemn Israel announcement of new construction activity in Palestinian territories and demand immediate dismantling of the illegal settlements. -----> description tag text

     UN Secretary General Ban Ki-moon also expressed his deep concern by the heightened settlement activity in West Bank, saying the move by Israel gravely threatens efforts to establish a viable Palestinian state.    ----> description tag text.
4

3 に答える 3

4
于 2012-12-19T10:35:30.657 に答える
0

問題のある文字を単に置き換えるだけで何か問題がありますか?

string = string.replaceAll("&lt;", "");
string = string.replaceAll("div&gt;", "");
string = string.replaceAll("p&gt;", "");
于 2012-12-19T10:32:09.193 に答える