本文實例講述了Java使用正則表達式提取XML節點內容的方法。分享給大家供大家參考,具體如下:
現在有類似<doc>abc</doc><title>3232</title> <doc>只要內容</doc>這么一個串,需要提取abc,3232,只要內容的節點內容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public static List getContext(String html) { List resultList = new ArrayList(); Pattern p = Pattern.compile( ">([^</]+)</" ); //正則表達式 commend by danielinbiti Matcher m = p.matcher(html ); // while (m.find()) { resultList.add(m.group( 1 )); // } return resultList; } /** * @param args */ public static void main(String[] args) { String a = "<doc>abc</doc><title>3232</title> <doc>只要內容</doc>" ; List list = getContext(a); System.out.println(list); } |
希望本文所述對大家java程序設計有所幫助。
原文鏈接:http://blog.csdn.net/danielinbiti/article/details/44965691