how to parse xml file from Sdcard in Android

前端 未结 3 792
别那么骄傲
别那么骄傲 2020-12-15 11:50

I\'ve got this xml:


  
    

        
3条回答
  •  臣服心动
    2020-12-15 12:50

    Finally i got Solution like this I have used Dom parsing

    public class MainActivity extends Activity {
    
        ArrayList mImageLink;
        ArrayList phone_no;
        int count;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
             mImageLink = new ArrayList();
             phone_no = new ArrayList();
    
             try {
             File file = new File("mnt/sdcard/Backup_Apps/call_logs/calllog_35777569.xml");
             InputStream is = new FileInputStream(file.getPath());
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             DocumentBuilder db = dbf.newDocumentBuilder();
             Document doc = db.parse(new InputSource(is));
             doc.getDocumentElement().normalize();
    
             NodeList nodeList = doc.getElementsByTagName("alllogs");
    
             for (int i = 0; i < nodeList.getLength(); i++) {
    
                 Node node = nodeList.item(i);
    
                 Element fstElmnt = (Element) node;
    
                 mImageLink.add(fstElmnt.getAttribute("count"));
                 count = Integer.parseInt(mImageLink.get(i));
    
             }
             NodeList n = doc.getElementsByTagName("log");
    
             for (int j = 0; j < count; j++) {
                 Node node = n.item(j);
    
                 Element fstElmnt = (Element) node;
    
                 phone_no.add(fstElmnt.getAttribute("number"));
    
            }
         } catch (Exception e) {
             System.out.println("XML Pasing Excpetion = " + e);
         }
     }
    }
    

提交回复
热议问题