Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从xml字符串中提取元素_Java_Xml_Extract - Fatal编程技术网

Java 从xml字符串中提取元素

Java 从xml字符串中提取元素,java,xml,extract,Java,Xml,Extract,如何从这个xml中提取URL?如果我将上述内容作为字符串?假设您将XML字符串存储在string str中,那么检索URL(如果您总是希望使用相同的输入文本格式)的一种简单而懒惰的方法可能是: <media:thumbnail url="http:// mysite.com/wp-content/uploads/2013/11/mes1-300x186.png" width="320" length="125399" type="image/jpg"/> 有时,作为一种快速的“一次

如何从这个xml中提取URL?如果我将上述内容作为字符串?

假设您将XML字符串存储在string str中,那么检索URL(如果您总是希望使用相同的输入文本格式)的一种简单而懒惰的方法可能是:

<media:thumbnail url="http:// mysite.com/wp-content/uploads/2013/11/mes1-300x186.png" width="320" length="125399" type="image/jpg"/>
有时,作为一种快速的“一次性解决方案”,您可以使用正则表达式

String url = str.split("\"")[1];
stringxml=”“;
Pattern=Pattern.compile(“url\\s*=\\s*\\\”(.*?\\”);
Matcher m=pattern.Matcher(xml);
if(m.find()){
字符串urlValue=m.group(1);
System.out.println(urlValue);
}
看看这里:看看这里
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);
}