Open XML file from res/xml in Android

后端 未结 3 1434
暖寄归人
暖寄归人 2020-12-09 22:16

I created a Java application which opens an xml file that looks something like this:


  

        
3条回答
  •  [愿得一人]
    2020-12-09 22:57

    If it's in the resource tree, it'll get an ID assigned to it, so you can open a stream to it with the openRawResource function:

    InputStream is = context.getResources().openRawResource(R.xml.data);

    As for working with XML in Android, this link on ibm.com is incredibly thorough.

    See Listing 9. DOM-based implementation of feed parser in that link.

    Once you have the input stream (above) you can pass it to an instance of DocumentBuilder:

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document dom = builder.parse(this.getInputStream());
    Element root = dom.getDocumentElement();
    NodeList items = root.getElementsByTagName("TheTagYouWant");
    

    Keep in mind, I haven't done this personally -- I'm assuming the code provided by IBM works.

提交回复
热议问题