Android DOM Parser - 从描述标签中检索图像链接

时间:2012-11-02 10:35:27

标签: android xml parsing dom

我使用DOM Parser解析此XML Feed:http://loc.grupolusofona.pt/index.php/?format=feed

我让Parser适用于所有标签,我只是缺少能够从描述标签中检索图像的想法。

Feed上的说明标记如下所示:

<description><![CDATA[<div class="K2FeedIntroText"><p><img style="margin: 10px;"
alt="joana soares rostos" src="http://loc.grupolusofona.pt/images/stories/varios/joana%20soares%20rostos.jpg"
height="110" width="120" />Quis ser veterinária mas deixou-se seduzir pela magia
de retratar o real. Joana Soares frequenta o primeiro ano do curso de Fotografia
da Lusófona e descreve a sua paixão pelas fotos como «inexplicável».</p>
</div>]]></description>

我想检索图片链接:

http://loc.grupolusofona.pt/images/stories/varios/joana%20soares%20rostos.jpg

我的解析器:

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);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // return XML
    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 ) {

     if( elem != null){
         return elem.getTextContent();
             }


     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));
    }
}

我的主要活动:

public class Noticias extends ListActivity {

// All static variables
static final String URL = "http://loc.grupolusofona.pt/index.php/?format=feed";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_DESC = "description";
static final String KEY_LINK = "link";
static final String KEY_PUBDATE = "pubDate";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_noticias);

    ArrayList<HashMap<String, Spanned>> menuItems = new ArrayList<HashMap<String, Spanned>>();

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

    NodeList nl = doc.getElementsByTagName(KEY_ITEM);
    // looping through all item nodes <item>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, Spanned> map = new HashMap<String, Spanned>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_ID, Html.fromHtml(parser.getValue(e, KEY_ID)));
        map.put(KEY_TITLE, Html.fromHtml(parser.getValue(e, KEY_TITLE)));
        map.put(KEY_DESC, Html.fromHtml(parser.getValue(e, KEY_DESC)));
        map.put(KEY_PUBDATE, Html.fromHtml(parser.getValue(e, KEY_PUBDATE)));
        map.put(KEY_LINK, Html.fromHtml(parser.getValue(e, KEY_LINK)));

        // adding HashList to ArrayList
        menuItems.add(map);
    }

    // Adding menuItems to ListView
    ListAdapter adapter = new SimpleAdapter(this, menuItems,
            R.layout.linha_feed,
            new String[] { KEY_TITLE, KEY_DESC, KEY_PUBDATE, KEY_LINK }, new int[] {
                    R.id.title, R.id.desc, R.id.pub, R.id.link});

    setListAdapter(adapter);

    // selecting single ListView item
    ListView lv = getListView();

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem

            String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
            String description = ((TextView) view.findViewById(R.id.desc)).getText().toString();
            String pub = ((TextView) view.findViewById(R.id.pub)).getText().toString();
            String link = ((TextView) view.findViewById(R.id.link)).getText().toString();

            // Starting new intent

            System.out.println("Title: " + title);
            System.out.println("Link: " + link);
            System.out.println("Description:" + description);
            System.out.println("Pubdate: " + pub);

            Intent in = new Intent(Intent.ACTION_VIEW);
            in.setData(Uri.parse(link));

            startActivity(in);

        }
    });
}
}

有什么想法吗?

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

使用RegEx这样的简单模式:

<\s*img\s*[^>]+src\s*=\s*(['"]?)(.*?)\1

这里使用这些功能:

public static String getMatch(String patternString, String text, int groupIndex){
    Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE | Pattern.DOTALL );
    return RegEx.getMatch(pattern, text, groupIndex);
}

public static String getMatch(Pattern pattern, String text, int groupIndex){
    if(text!=null){
        Matcher matcher = pattern.matcher(text);
        String match = null;
        while(matcher.find()){
            match = matcher.group(groupIndex);
            break;
        }
        return match;           
    }else{
        return null;
    }
}

然后你可以像这样插入那个模式:

String imageSource = getMatch("<\\s*img\\s*[^>]+src\\s*=\\s*(['\"]?)(.*?)\\1", description, 2);
相关问题