【问题标题】:How to get XML data from assets in android?如何从android中的资产中获取XML数据?
【发布时间】:2013-03-27 05:16:58
【问题描述】:

我在 assets 文件夹中存储了一个 XML 文件,我想检索存储在 android 中 assets 中的数据 XML 文件,给我一个解决方案。

<Response>
    <RequestID>1001</RequestID>
    <FunctionStatus></FunctionStatus>   
    <Message></Message>

<Result>    
    <Banner>

    <Banner_List>
        <Banner_id>3794</Banner_id>
        <Banner_url>http://www.metromatinee.com/agile_dogs/05.jpg</Banner_url>
        <Movie_name>Rose Guitarinaal</Movie_name>
        <gender>Movie</gender>
    </Banner_List> 
</Response>  

我试过了
InputStream is = getAssets().open("demo.xml");

但异常导致
任何帮助..???

03-27 11:03:04.939: E/AndroidRuntime(432): FATAL EXCEPTION: main
03-27 11:03:04.939: E/AndroidRuntime(432): java.lang.NullPointerException
03-27 11:03:04.939: E/AndroidRuntime(432):  at android.content.ContextWrapper.getAssets(ContextWrapper.java:74)
03-27 11:03:04.939: E/AndroidRuntime(432):  at com.service.example.DemoBussinessExecutePage.executeBussinessFunction(DemoBussinessExecutePage.java:230)
03-27 11:03:04.939: E/AndroidRuntime(432):  at com.service.example.Service_Manager.executeBussinessFunction(Service_Manager.java:38)
03-27 11:03:04.939: E/AndroidRuntime(432):  at com.service.example.Service_class$1.onClick(Service_class.java:57)
03-27 11:03:04.939: E/AndroidRuntime(432):  at android.view.View.performClick(View.java:2408)
03-27 11:03:04.939: E/AndroidRuntime(432):  at android.view.View$PerformClick.run(View.java:8816)
03-27 11:03:04.939: E/AndroidRuntime(432):  at android.os.Handler.handleCallback(Handler.java:587)
03-27 11:03:04.939: E/AndroidRuntime(432):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-27 11:03:04.939: E/AndroidRuntime(432):  at android.os.Looper.loop(Looper.java:123)
03-27 11:03:04.939: E/AndroidRuntime(432):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-27 11:03:04.939: E/AndroidRuntime(432):  at java.lang.reflect.Method.invokeNative(Native Method)
03-27 11:03:04.939: E/AndroidRuntime(432):  at java.lang.reflect.Method.invoke(Method.java:521)
03-27 11:03:04.939: E/AndroidRuntime(432):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-27 11:03:04.939: E/AndroidRuntime(432):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-27 11:03:04.939: E/AndroidRuntime(432):  at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

    标签: android android-xml


    【解决方案1】:

    最后我找到了解决方案。

    AssetManager am = Class.getAssets();
            try 
            {
                InputStream is = am.open("1001.xml");
                int length = is.available();
                byte[] data = new byte[length];
                is.read(data);
                demoInput = new String(data).toString();
            }
            catch (IOException e1)
            {
                e1.printStackTrace();
            }
    

    【讨论】:

      【解决方案2】:

      这对我有用。

        InputSource is = new InputSource(getResources().openRawResource(R.raw.config));
              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
              DocumentBuilder db = dbf.newDocumentBuilder();
              Document doc=db.parse(new InputSource(is.getByteStream()));
              doc.getDocumentElement().normalize();
        NodeList nodeList = doc.getElementsByTagName("woeid");
              for (int i = 0; i < nodeList.getLength(); i++) 
              {
                  Node node = nodeList.item(i);
                  Element fstElmnt = (Element) node;
                  fstElmnt.getElementsByTagName("countrywoeid").item(0).getTextContent();
                  fstElmnt.getElementsByTagName("countryname").item(0).getTextContent();
              }
      

      这是项目原始文件夹中的 XML

       <settings>
       <title>Configuration</title>
       <Version>1.0</Version>
       <Gadgets>6</Gadgets>
       <woeid>
        <countrywoeid>2211096</countrywoeid>
        <countryname>Asia/Karachi</countryname>
       </woeid>
       <woeid>
        <countrywoeid>2211177</countrywoeid>
        <countryname>Asia/Karachi</countryname>
       </woeid>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-30
        • 2016-11-27
        • 2019-11-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多