在Android 3.0及以上版本中,从res获取XML数据失败。

4

类似问题

我有一些预定义的xml文件,它们保存在res>raw>first.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"));              
            }

使用文档获取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 Method

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返回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"?>

在4.0以上版本中进行测试时,缺少String tag
1个回答

0

终于实现了,在getvalue函数内部进行了更改;

修改的代码如下:

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;

    }

它完美地运作了!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接