2013-04-12 39 views
7

Tôi đã có xml này:làm thế nào để phân tích file xml từ sdcard trong Android

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?> 
    <alllogs count="5"> 
    <log number="" time="1365589432804" date="Wed Apr 10 15:53:52 GMT+05:30 2013" type="2" duration="0" new="1" name="360 chhotein" numbertype="2" numberlabel="null" /> 
    <log number="" time="1365595887261" date="Wed Apr 10 17:41:27 GMT+05:30 2013" type="2" duration="0" new="1" name="360 chhotein" numbertype="2" numberlabel="null" /> 
    <log number="" time="1365596387590" date="Wed Apr 10 17:49:47 GMT+05:30 2013" type="2" duration="0" new="1" name="360 chhotein" numbertype="2" numberlabel="null" /> 
    <log number="" time="1365596787051" date="Wed Apr 10 17:56:27 GMT+05:30 2013" type="2" duration="0" new="1" name="null" numbertype="null" numberlabel="null" /> 
    <log number="0041786095382" time="1365740469738" date="Fri Apr 12 09:51:09 GMT+05:30 2013" type="2" duration="0" new="1" name="null" numbertype="null" numberlabel="null" /> 
    </alllogs> 

Các phân tích cú pháp Tôi đang sử dụng là XmlPullParser mẹ đẻ android

tôi không thể thay đổi định dạng XML nhưng tôi có thể sử dụng một trình phân tích cú pháp tương thích Android khác nếu nó có giá trị.

Nếu nó không phải là rõ ràng nó phải phù hợp trên một lớp như thế này:

try { 
     String file = Environment.getExternalStorageDirectory() + File.separator + "SmsContactsBackup/logs/calllogs_20130412125502.xml"; 
     XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
     factory.setNamespaceAware(true); 
     XmlPullParser parser = factory.newPullParser(); 
     FileInputStream fis = new FileInputStream(file); 
     parser.setInput(new InputStreamReader(fis)); 
     Log.d("Response", ""+convertStreamToString(fis)); 
     int eventType = parser.getEventType(); 
     String name = null; 
     while (eventType != XmlPullParser.END_DOCUMENT) 
     { 
      //String name = parser.getName(); 

      if(eventType == XmlPullParser.START_DOCUMENT) { 
       System.out.println("Start document"); 
      }else if(eventType == XmlPullParser.START_TAG) { 
       name = parser.getName(); 
       if (name.equalsIgnoreCase("alllogs")){ 
        count = Integer.parseInt(parser.getAttributeValue("", "count")); 
       }else if(name.equalsIgnoreCase("log")) 
       { 
        phone_no.add(parser.getAttributeValue("", "number")); 
       } 
       System.out.println("Start tag "+parser.getName()); 
      }else if(eventType == XmlPullParser.END_TAG) { 
       name = parser.getName(); 
       System.out.println("End tag "+parser.getName()); 
      } 
      eventType = parser.next(); 
     } 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (XmlPullParserException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

eventType là không nhận được đúng đắn và IOException.

+0

không được thông báo ngoại lệ nói gì? – Egor

+0

@Egor i chỉ giải quyết ngoại lệ nhưng có một vấn đề trong looping và eventtype.i có giá trị 4 trong eventtype nhưng couldnt có thể nhận được bất kỳ thẻ. cũng có vấn đề trong parse.getName(); đôi khi nó cho tôi một tên n tất cả các thời gian khác nó cho thấy null.if bạn đang quen thuộc với phân tích cú pháp này sau đó xin vui lòng giúp tôi.i đã cố gắng từ 2 ngày qua. –

Trả lời

3

Cuối cùng tôi đã nhận Giải pháp như thế này tôi đã sử dụng Dom phân tích

public class MainActivity extends Activity { 

    ArrayList<String> mImageLink; 
    ArrayList<String> phone_no; 
    int count; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mImageLink = new ArrayList<String>(); 
     phone_no = new ArrayList<String>(); 

     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); 
    } 
} 
} 
0
try { 
      File SDCardRoot = Environment.getExternalStorageDirectory() 
        .getAbsoluteFile(); 
      File myDir = new File(SDCardRoot.getAbsolutePath() + "/.ABC" 
        + "/.Config"); 

      File file = new File(myDir, "config.xml"); 

      InputStream inputSource = new FileInputStream(file.getPath()); 
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
      Document doc = dBuilder.parse(inputSource); 
      Element element = doc.getDocumentElement(); 
      element.normalize(); 
      NodeList nList = doc.getElementsByTagName("item"); 
      for (int i = 0; i < nList.getLength(); i++) { 
       Node node = nList.item(i); 
       if (node.getNodeType() == Node.ELEMENT_NODE) { 
        Element eElement = (Element) node; 
        String id = eElement.getElementsByTagName("id").item(0) 
          .getTextContent(); 
        eElement.getElementsByTagName("name").item(0) 
          .getTextContent(); 
        eElement.getElementsByTagName("image_updated").item(0) 
          .getTextContent(); 
        eElement.getElementsByTagName("image").item(0) 
          .getTextContent(); 
        eElement.getElementsByTagName("colorcode").item(0) 
          .getTextContent(); 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
Các vấn đề liên quan