2012-07-10 2 views
-1

나는 안드로이드 프로젝트에서 일하고 있는데 로컬 파일 (raw/file.xml)을 파싱 할 수 없다. 나는 인터넷에서 찾은 조언을 헛되이 시도했다. 그것은 (URL 클래스를 사용하여 ...) net에서 파일을 분석하려고 할 때 작동하지만 로컬 파일 프로그램을 시도하면 작동하지 않습니다 !!! 제발 도와주세요. 안드로이드에서 로컬 XML 파일을 구문 분석하는 방법은 무엇입니까?

내가 xr.parse(new InputSource(is.getByteStream()));

+2

그것은 작동하지 않습니다와 InputSource is = new InputSource(getResources().openRawResource(R.raw.example));

및 라인 xr.parse(new InputSource(sourceUrl.openStream()));와 라인 URL sourceUrl = new URL ("example.com/w...";); 교체 (나는 삭스는 XML 파서로 사용)? 그것을 포착하십시오, 어떻게됩니까? –

+0

줄 바꾸기 URL sourceUrl = 새 URL ("http : //www.example.com/w ..."); with InputSource is = 새 InputSource (getResources(). openRawResource (R.raw.example)); 및 행 xr.parse (새 InputSource (sourceUrl.openStream())); 과 xr.parse (새 InputSource (is.getByteStream())); – giusepe

+2

@ user1514941, 귀하의 의견을 읽을 수 없습니다. 코드 줄을 추가하거나 가능한 경우 전체 응용 프로그램 코드를 추가하여 질문을 편집하십시오. – Egor

답변

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; 
} 
관련 문제