【问题标题】:android DOM parsing with entities in tagsandroid DOM解析与标签中的实体
【发布时间】:2011-06-28 09:39:19
【问题描述】:

我可能想解析以下包含条目的 XML。

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

解析有效。但是在条目之后我没有收到任何输出。无法在该位置使用 CDATA。

我正在使用以下代码:

        urlConnection.getInputStream());
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setExpandEntityReferences(false);
        DocumentBuilder builder = factory.newDocumentBuilder();
        doc = builder.parse(in);

有人知道吗?

提前谢谢!

【问题讨论】:

  • 这是一个无效的 xml。解析器在解析时应该抛出异常。
  • 更准确地说:因为&lt;text&gt; ... &lt;/title&gt;&lt;/text&gt;而无效。开始标签 &lt;text&gt; 和结束标签 &lt;/title&gt; 不匹配。你缺少一个开始标签&lt;title&gt; 或者你有一个错字并且实际上想要一个空的标题元素&lt;title/&gt;。 (注意,斜线是元素名称之后)
  • 我的错,应该是 foo fo <BR>bar bar 但问题是实体标签

标签: android xml dom xml-parsing


【解决方案1】:

永远不需要使用 CDATA;所以这既不是问题也不是解决方案。但是你怎么知道没有文字呢?您很可能只有多个相邻的文本节点——底层解析器通常返回多个文本段(尤其是当有实体时)。您可以使用 DOM 方法“规范化”元素包含的文本内容,将相邻的 Text 节点替换为单个节点。但如果没有这个,你永远不应该假设所有文本都在第一个(也是唯一的)文本节点内。

如果没有节点,则解析器 Android 捆绑包可能存在错误。我认为它们包含旧版本的 xpp 或其他东西,它可能存在问题(与 Xerces 或 Woodstox 等更完善的解析器相比)。但我首先要确保它不仅仅是“隐藏”节点的情况。

【讨论】:

    【解决方案2】:

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

    我发现,其他人也有类似的问题。 Android 2.0.1 和 2.1 中存在 Bug。我通过使用 sax 解析器解决了这个问题。

    【讨论】:

      【解决方案3】:

      我的名字是 Divy Dhiman,我是一名高级安卓开发人员

      我已经完成了 XML 解析,您可以通过实体或节点 aur 进行解析,方法是获取取决于您的文字控制。

      私有类 MyAsyncTask 扩展 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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-16
        • 2022-11-04
        • 2011-06-06
        • 2013-06-29
        • 2011-06-14
        • 1970-01-01
        相关资源
        最近更新 更多