开发者

android DOM parsing with entities in tags

I might like to parse the following XML that contains entrities.

<node>
    <text><title>foo fo &lt;BR&gt;bar bar </title></text>
</node>

The parsing works. But after the entrities I do not receive any output. Using CDATA is not possible at the position.

I'm using the following code:

        urlConnection.getInputStream());
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setExpandEntityReferences(false);
        DocumentBuilder builder = factory.newDocumentBuilder();
        doc = builder.parse(in);开发者_如何学运维

Does anyone got an idea?

Thx in advance!


CDATA usage is never necessary; so that's neither problem or a solution. But how do you actually tell there is no text? It is quite possible that you just have multiple adjacent text nodes -- underlying parsers often return multiple text segements (and esp. when there are entities). You can use a DOM method to "normalize" text content that an Element contains, to replace adjacent Text nodes with just a single one. But without this you should never assume all text is within the first (and only) Text node.

If there are no nodes, it is possible that the parser Android bundles is buggy. I think they include an old version of xpp or something, and it might have issues (compared to more polished parsers like Xerces or Woodstox). But I would first make sure it's not just case of "hidden" nodes.


http://code.google.com/p/android/issues/detail?id=2607

I found out, other have a similar problem. There Bug in Android 2.0.1 and 2.1. I solved this Problem by using the sax parser.


My name is Divy Dhiman i am a senior android developer

I had done the XML parsing by the same thing you can do it by entity or by node aur by fetching literal control that's depend on you.

private class MyAsyncTask extends AsyncTask {

    @Override
    protected String doInBackground(String... abc) {

        try {

            URL url = new URL(jksbvlds);

            URLConnection connection;
            connection = url.openConnection();

            HttpURLConnection httpConnection = (HttpURLConnection) connection;
             int responseCode = httpConnection.getResponseCode();
             if(responseCode == HttpURLConnection.HTTP_OK)
             {

                 InputStream in = httpConnection.getInputStream();
                 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                 DocumentBuilder db = dbf.newDocumentBuilder();
                 Document dom = db.parse(in);

                 Element docEle = dom.getDocumentElement();
                 NodeList nl = docEle.getElementsByTagName("quote");
                 if(nl != null && nl.getLength()>0)
                 {
                     for(int i = 0; i<nl.getLength();i++ )
                     {
                         StockInfo theStock = getStockInformation(docEle);


                         name = theStock.getName();
                         yearLow = theStock.getYearLow();
                         yearHigh = theStock.getYearHigh();
                         daysLow = theStock.getDaysLow();
                         daysHigh = theStock.getDaysHigh();
                         lastTradePriceonly = theStock.getLastTradePriceonly();
                         change = theStock.getChange();
                         daysRange = theStock.getDaysRange();

                     }
                 }



             }

        } 

        catch(MalformedURLException e)
        {
            Log.d(TAG,"MalformedURLException",e);
        }
        catch(IOException e)
        {
            Log.d(TAG,"IOException",e);
        }
        catch (ParserConfigurationException e) {
            Log.d(TAG,"ParserConfigurationException", e);
            }
        catch (SAXException e) {
            Log.d(TAG,"SAXException",e);

        }
        finally{}

        return null;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜