简体   繁体   中英

how to parse an XML file in Android (using navigation drawer as layout)

I'm developing an Android application. I would use navigation drawer as layout.

Actually I'm taking parts from autogenerated code of Eclipse IDE. Problem is that I can't correctly read my XML file because compiler keeps say me that file is not reachable due to an uncorrect path (could not open file:///MYPATH).

I put my XML file in xml directory that is subdirectory of "res".

How can I access to this directory from my utility class that is not linked to any activity class of my project?

What is the best way to parse and print an XML file within an Android application?

Is there a specialized API for doing this?

(sample taken from my XML file:

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



<data>

      <zname id = "breast" name="Breast">

    <cancer cancer_name="">

      <t_parameter t_level="" t_desc="">
      </t_parameter>

      <n_parameter n_level="" n_desc="">
      </n_parameter>

      <m_parameter m_level="" m_desc="">
      </m_parameter>      

      <stage stage_level="" stage_desc="">
      </stage>

      <guideline url="">
      </guideline>  

    </cancer>   

      </zname>

</data>

)

Create a InputStream:

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

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(is);
Element root = dom.getDocumentElement();
NodeList items = root.getElementsByTagName("zname");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM