简体   繁体   中英

extract element from xml string

<media:thumbnail url="http:// mysite.com/wp-content/uploads/2013/11/mes1-300x186.png" width="320" length="125399" type="image/jpg"/>

How to extract the URL from this xml ? If I have the above as string ?

假设您有XML字符串存储在String str中,一种懒惰且简单的方法来检索URL(如果您总是期望相同的输入文本格式)可以是:

String url = str.split("\"")[1];

Some times, as a quick "one time solution" you can use regular expressions.

String xml="<media:thumbnail url=\"http:// mysite.com/wp-content/uploads/2013/11/    mes1-300x186.png\" width=\"320\" length=\"125399\" type=\"image/jpg\"/>";
Pattern pattern=Pattern.compile("url\\s*=\\s*\\\"(.*?)\\\"");
Matcher m=pattern.matcher(xml);
if(m.find()){
   String urlValue=m.group(1);
   System.out.println(urlValue);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM