无法在Android的3.0及以上从获取资源的XML数据 [英] Failed to fetch xml data from res in Android 3.0 and above

查看:118
本文介绍了无法在Android的3.0及以上从获取资源的XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似的问题

有几个predefined XML 这是我一直在水库>原料> first.xml 现在的我取在运行时displying数据如下图所示:

Having few predefined xml which I kept under res>raw>first.xml now I am fetch at runtime and displying data like below:

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方法

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>

以上整个code的工作远低于4.0而不是4.0以上,getElementsByTagName`回到4.0以上空的结果。

Above entire code work well below 4.0 but not above 4.0, getElementsByTagName` return null result above 4.0.

导致低于4.0

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

在结果4.0以上。

Result in Above 4.0

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

字符串标记丢失时测试在4.0以上,

String tag is missing while testing in above 4.0,

推荐答案

我终于实现了,里面提出的GetValue 修改功能;

Finally I achieved, made changes inside getvaluefunction;

更改code:

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;

    }

它的作品完美!

这篇关于无法在Android的3.0及以上从获取资源的XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆