2012-08-03 4 views
0

RSS 피드 값 (XML 형식)을 읽는 동안이 오류가 발생합니다.xml 구문 분석 문제

XML 파일에 일반 텍스트 값이 있지만 HTML 요소가있을 때 헤더 및 설명을 성공적으로 가져 오는 중입니다. 즉 <p>, <HTML>, <image>입니다. XML 파일에서 데이터를 표시하지 않습니다.

this URL을 사용하여 XML 데이터를 가져옵니다.

이 설명 태그에서 HTML 개체, 즉 Img 태그를 사용하고 싶습니다. 제발 어떻게 내가 이걸 얻을 수 있는지 알려주시겠습니까? 여기

코드입니다 :

ArrayList<HashMap<String, String>> business_List = new ArrayList<HashMap<String,String>>(); 

XMLParser parser = new XMLParser(); 
String xml = parser.getXmlFromUrl(URL);    
Document doc = parser.getDomElement(xml); // getting DOM element 

NodeList nl = doc.getElementsByTagName(KEY_ITEM);  

// looping through all song nodes <song> 
for(int i=0;i<nl.getLength();i++) 
{ 
    //creating new HashMap 
    HashMap<String, String> map = new HashMap<String, String>();        
    Element e = (Element) nl.item(i);  

     //adding each child node to HashMap key => value 
    //map.put(KEY_ID, parser.getValue(e, KEY_ID)); 
    map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));   
    map.put(KEY_PUB_DATE, parser.getValue(e, KEY_PUB_DATE)); 
    map.put(KEY_DESCRIPTION, parser.getValue(e, KEY_DESCRIPTION));  
    business_List.add(map); 
}  
list = (ListView)findViewById(R.id.list); 

// Getting adapter by passing xml data ArrayList 
adpater = new LazyAdapter(this, business_List); 
list.setAdapter(adpater); 
} 

=====이 당신이 당신의 설명 필드에 파싱되지 않은 HTML을 얻고하고자하는 표시 내 xmlparserclass의 ===

public class XMLParser { 

     // constructor 
     public XMLParser() { 

     } 

     /** 
     * Getting XML from URL making HTTP request 
     * @param url string 
     * */ 
    public String getXmlFromUrl(String url) { 
    String xml = null; 

    try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 
     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     xml = EntityUtils.toString(httpEntity); 

     System.out.println("XML...." + xml); 

     } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
      return xml; 
     } 

     /** 
     * Getting XML DOM element 
     * @param XML string 
     * */ 
    public Document getDomElement(String xml){ 
     Document doc = null; 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     try { 
      DocumentBuilder db = dbf.newDocumentBuilder(); 
      InputSource is = new InputSource(); 
      is.setCharacterStream(new StringReader(xml)); 
      doc = db.parse(is); 

     } catch (ParserConfigurationException e) { 
       Log.e("Error: ", e.getMessage()); 
      return null; 
     } catch (SAXException e) { 
      Log.e("Error: ", e.getMessage()); 
       return null; 
     } catch (IOException e) { 
      Log.e("Error: ", e.getMessage()); 
      return null; 
     } 

       return doc; 
     } 

     /** Getting node value 
      * @param elem element 
      */ 
    public final String getElementValue(Node elem) { 

     Node child;   

     if(elem != null) 
     { 


      if (elem.hasChildNodes()) 
      { 

       for(child = elem.getFirstChild(); child != null; child = child.getNextSibling()) 
       { 
        if(child.getNodeType() == Node.TEXT_NODE ) 
         { 
          return child.getNodeValue(); 
         } 
        } 
       } 
      } 
      return ""; 
     } 

     /** 
      * Getting node value 
      * @param Element node 
      * @param key string 
      * */ 
    public String getValue(Element item, String str) 
    {  

     NodeList n = item.getElementsByTagName(str); 

     return this.getElementValue(n.item(0)); 
    } 
}  
+2

어떤 오류가 있습니까? 그리고 코드를 보여줄 수 있습니까? –

+0

아래 코드는 내 코드입니다 ... –

+0

코드는 질문에 속합니다. 대답은 아닙니다. 나는 거기에 넣었다. 답변이 아닌 답변을 삭제하십시오. –

답변

0

입니다 내부에서 더 많은 데이터를 추출하십시오.

이렇게하려면 html 파서를 사용해야하며 고려해야 할 좋은 것은 jsoup입니다. 당신은 jsoup cookbook을보고 그것을 사용하여 시작할 수 있습니다.

다른 HTML 파서를 사용할 수는 있지만이 앱이 Android에서 작동하는 것으로 확신합니다.

실제 파서를 사용하십시오. trying to parse html using regular expressions을 사용하지 마십시오.