2012-07-10 42 views
-1

我正在開發一個android項目,我無法解析本地文件(raw/file.xml)。我嘗試了一些我在互聯網上找到的建議,但徒勞無功。 它工作時,我嘗試解析網絡中的文件(使用URL類...),但當我嘗試使用本地文件程序不起作用!請我需要幫助。 (我用薩克斯作爲XML解析器)如何解析android中的本地XML文件?

我更換線URL sourceUrl = new URL ("example.com/w...";);InputSource is = new InputSource(getResources().openRawResource(R.raw.example));

和線xr.parse(new InputSource(sourceUrl.openStream()));xr.parse(new InputSource(is.getByteStream()));

+2

它不工作?精心設計,會發生什麼。 – 2012-07-10 13:55:00

+0

我替換行 URL sourceUrl = new URL(「http://www.example.com/w ...」); 與 InputSource is = new InputSource(getResources()。openRawResource(R.raw.example)); 和行 xr.parse(new InputSource(sourceUrl.openStream())); 與 xr.parse(new InputSource(is.getByteStream())); – giusepe 2012-07-10 14:01:17

+2

@ user1514941,您的評論是不可讀的,請編輯您的問題添加這些代碼行,或者如果可能的話,整個應用程序代碼。 – Egor 2012-07-10 14:10:05

回答

0
You can put the xml file path here and keep parsing the inner nodes usiln self running loop. Save the values in arrays accordingly for each node.. 

    NodeList eNodeList = null; 
eNodeList = Utils.getXMLFromFilePath(your.xml", "firstNOdeNAme", eNodeList); 
getElementsByNodelist(eNodeList) 


    private static getElementsByNodelist(Nodelist nodelist) 
{ 
    if(nodelist!=null) 
    { 
     for(int i = 0;i<nodelist.getLength();i++) 
     { 
      Element element = (Element) nodelist.item(i); 
      String id=getTextValue(element, "id"); 
      //For attribute value 
      element.getAttribute("attributename"); 
      // if u want to parse tjis element again 
      NodeList l=element.getElementsByTagName("innerTag"); 
      // self loop 
      getElementsByNodelist(l); 
     } 
    } 

} 

public static String getTextValue(Element ele, String tagName) { 
    String textVal = ""; 
    NodeList nl = ele.getElementsByTagName(tagName); 
    if(nl != null && nl.getLength() > 0) { 
     Element el = (Element)nl.item(0); 
     if(el.getFirstChild()!=null)  
     { 
      textVal = el.getFirstChild().getNodeValue(); 
     } 
    } 
    return textVal; 
}