大家好,XML解析不依赖任何Jar包的编写模式,关键词:Pattern、Matcher使用。
工具类如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
/** * Xml工具类
* @author 章力
* @微信 zl4828
*/
public class XmlUtil {
/**
* 从消息message中提取出指定的tagName节点,包括他得子节点.
*
* @param xmlMessage
* @param tagName
* @return
* @throws Exception
*/
public static String pareXml(String xmlMessage, String tagName)
{
String regex = ".*?(<" +tagName+ ">.*?</" +tagName+ ">)|(<" +tagName+ "/>)" ;
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
Matcher matcher = pattern.matcher(xmlMessage);
if (matcher.find())
{
xmlMessage = matcher.group( 1 );
return xmlMessage;
}
else
{
throw new RuntimeException( "无法提取xml消息体.tagName = " + tagName);
}
}
/**
* 从消息message中提取出指定的tagName节点中间的内容
*
* @param xmlMessage
* @param tagName
* @return
* @throws Exception
*/
public static String pareXmlContent(String xmlMessage, String tagName)
{
String regex = "\\<" + tagName + ">(.*?)\\</" + tagName + ">" ;
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
Matcher matcher = pattern.matcher(xmlMessage);
if (matcher.find())
{
xmlMessage = matcher.group( 1 );
return xmlMessage;
}
else
{
throw new RuntimeException( "无法提取xml消息体.tagName = " + tagName);
}
}
/**
* 返回匹配到的所有节点
*
* @param xmlMessage
* @param tagName
* @return
* @throws Exception
*/
public static Matcher pareXmlMatcher(String xmlMessage, String tagName)
{
String regex = ".*?(<" +tagName+ ">.*?</" +tagName+ ">)|(<" +tagName+ "/>)" ;
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
Matcher matcher = pattern.matcher(xmlMessage);
return matcher;
}
} |
使用实例
1
2
3
4
5
6
7
|
/**
* 解析返回的XML,沫沫金提供支持微信@zl4828
*/
Matcher matcher = XmlUtil.pareXmlMatcher(resultXml, "Result" );
while (matcher.find()) {
System.out.println(XmlUtil.pareXmlContent(matcher.group(), "hosName" ));
}
|
示例XML内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
<? xml version = "1.0" encoding = "UTF-8" ?> < Response >< MessageHeader >
< code >0</ code >
< desc >成功</ desc >
</ MessageHeader >< List >
< Result >
< hosOrgCode >43523202X61010311A1001</ hosOrgCode >
< hosName >西安市第九医院</ hosName >
< hospitalAdd >沫沫金</ hospitalAdd >
< hospitalRule ></ hospitalRule >
< hospitalWeb ></ hospitalWeb >
< trafficGuide ></ trafficGuide >
< hospitalDesc >专注网页设计、Web前端工程</ hospitalDesc >
< hospitalTel >微信:zl4828</ hospitalTel >
< hospitalGrade ></ hospitalGrade >
< payMode >|3|</ payMode >
< orderMode >|2|</ orderMode >
< isSpTime >1</ isSpTime >
</ Result >
< Result >
< hosOrgCode >43720037161011311A5211</ hosOrgCode >
< hosName >西安市第八医院</ hosName >
< hospitalAdd ></ hospitalAdd >
< hospitalRule ></ hospitalRule >
< hospitalWeb ></ hospitalWeb >
< trafficGuide ></ trafficGuide >
< hospitalDesc >网络营销</ hospitalDesc >
< hospitalTel >15319419526</ hospitalTel >
< hospitalGrade >1</ hospitalGrade >
< payMode >|3|</ payMode >
< orderMode >|2|</ orderMode >
< isSpTime >1</ isSpTime >
</ Result >
</ List ></ Response >
|
总结:不想千篇一律,以上是你尝鲜的动力。
本文转自 沫沫金 51CTO博客,原文链接:http://blog.51cto.com/zl0828/1717160,如需转载请自行联系原作者