【问题标题】:Failed to fetch xml data from res in Android 3.0 and aboveAndroid 3.0及以上版本无法从res中获取xml数据
【发布时间】:2013-01-29 23:42:38
【问题描述】:

Similar issue

我保留在res>raw>first.xml 下的几个预定义xml 现在我在运行时获取并显示如下数据:

NodeList nodes = MainActivity.commonmethod.GetDocumentFile(ProductActivity.this,_intRowID).getElementsByTagName("string");


for (int i = 0; i < nodes.getLength(); i++) {                           

            Element e = (Element)nodes.item(i);
            e.normalize();

                    _ArrProductName.add( MainActivity.commonmethod.getValue(e, "string"));              
            }

使用 Document 获取 XML 文件(Plist 文件)的方法

public Document GetDocumentFile(Context context, int rawID) {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);
    DocumentBuilder builder = null;
    try {
        builder = builderFactory.newDocumentBuilder();

        document = builder.parse(context.getResources().openRawResource(
                rawID));
        document.getDocumentElement().normalize();
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return document;
}

getValue 方法

public String getValue(Element item, String str) {
        NodeList n = item.getElementsByTagName(str);

        try {
            StringWriter sw = new StringWriter(); 
            Transformer serializer = TransformerFactory.newInstance().newTransformer(); 
            serializer.transform(new DOMSource(n.item(0)), new StreamResult(sw));   
            String result = sw.toString(); 

            System.out.println("result="+result);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return CommonMethod.getElementValue(n.item(0));
    }

    public final static String getElementValue(Node elem) {
        Node kid;
        if (elem != null) {
            if (elem.hasChildNodes()) {
                for (kid = elem.getFirstChild(); kid != null; kid = kid
                        .getNextSibling()) {
                    if (kid.getNodeType() == Node.TEXT_NODE) {
                        return kid.getNodeValue();
                    }
                }
            }
        }
        return "";
    }

test.xml

 <?xml version="1.0" encoding="UTF-8"?>

    <array>
       <!-- PrdocuName -->
       <string>Android ICS 4.0™</string>

     <!-- PrdocutDescription -->
       <string>Mobile</string>

       <!-- PrdocuImage -->
       <string>Mobile.png</string>

      <!-- PrdocuAddress -->
       <string>url</string>

        <!-- Conversion -->
       <integer>400</integer>

      <!-- ThicknessNames -->
       <string>skim</string>

       <!-- ThicknessValues -->
       <string>1</string>

      <!-- LongDescription -->
       <string>Android is the market leader in terms of total number of device sold and soon it will be leader in terms of total number of application available in the market.</string>

    </array>

整个代码在 4.0 以下但不能在 4.0 以上运行良好,getElementsByTagName`在 4.0 以上返回 null 结果。

结果低于 4.0

<?xml version="1.0" encoding="UTF-8"?><string>Android ICS 4.0™</string>

结果高于 4.0

<?xml version="1.0" encoding="UTF-8"?>

String tag 在 4.0 以上测试时丢失,

【问题讨论】:

    标签: android dom xml-parsing


    【解决方案1】:

    我终于实现了,在getvaluefunction 内部进行了更改;

    更改代码:

    public String getValue(Element item, String str) {
    
    
            String strResponse="";
            Node kid;
            if(item!=null)
            {
                if(item.hasChildNodes())
                {
                    for(kid=item.getFirstChild(); kid!=null; kid =kid.getNextSibling())
                    {
                        if (kid.getNodeType() == Node.TEXT_NODE) {
                            strResponse =kid.getNodeValue();
                            return strResponse;
                        }
                    }
                }else
                {
                    NodeList n = item.getElementsByTagName(str);
                    n = item.getChildNodes();
    
                    if(((Node) n.item(0))!=null)
                    {
                        if(((Node) n.item(0)).getNodeValue() !=null)
                        {
                            strResponse =((Node) n.item(0)).getNodeValue();
                            return strResponse;
                        }else
                        {
                            strResponse ="";
                        }
                    }
    
    
                }
            }
    
    
            return strResponse;
    
        }
    

    完美运行!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      相关资源
      最近更新 更多