文本是这样的:338 <?xml version="1.0" encoding="UTF-8"?><presence>...</presence>339 <?xml version="1.0" encoding="UTF-8"?><presence>...</presence>335 <?xml version="1.0" encoding="UTF-8"?>
<presence></presence>..........实际上就是由若干段 338 <?xml version="1.0" encoding="UTF-8"?><presence>...</presence>组成的,338表示,从其后开始到下一个数字339之间之间的字符串长度,也就是xml文本的长度.我想从两个方法入手:
1. 先找到第一个数字338,读出它后面长度为338的xml文本,但是并不知道到底有多少段,不知道怎么循环,或者第归.
2. 找出文本中含有多少个<?xml version="1.0" encoding="UTF-8"?> 这样的字符串,知道有几个xml文本,然后把介于<?xml version="1.0" encoding="UTF-8"?> 和</presence> 之间xml读取出来.但是我不知道怎么判断到底有几个xml文本???希望大家帮帮哥们!也长你点见识.实际上这段文本是SIP服务器返回的通知,用在网络通信上,我需要解析出内容.
<presence></presence>..........实际上就是由若干段 338 <?xml version="1.0" encoding="UTF-8"?><presence>...</presence>组成的,338表示,从其后开始到下一个数字339之间之间的字符串长度,也就是xml文本的长度.我想从两个方法入手:
1. 先找到第一个数字338,读出它后面长度为338的xml文本,但是并不知道到底有多少段,不知道怎么循环,或者第归.
2. 找出文本中含有多少个<?xml version="1.0" encoding="UTF-8"?> 这样的字符串,知道有几个xml文本,然后把介于<?xml version="1.0" encoding="UTF-8"?> 和</presence> 之间xml读取出来.但是我不知道怎么判断到底有几个xml文本???希望大家帮帮哥们!也长你点见识.实际上这段文本是SIP服务器返回的通知,用在网络通信上,我需要解析出内容.
import java.util.regex.Pattern;public class ReadXML {
private String xml = "338 <?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<presence>...</presence>" +
"339 <?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<presence>...</presence>" +
"335 <?xml version=\"1.0\" encoding=\"UTF-8\"?><presence></presence>";
public static void main(String[] args) {
ReadXML test = new ReadXML();
test.read();
}
public void read() {
//i为符合条件记数
int i = 0;
Pattern p = Pattern.compile("<\\?xml.*?</presence>");
Matcher m = p.matcher(xml);
while (m.find()) {
i++;
System.out.println(m.group());
}
System.out.println("共有 " + i);
}
}刚写的,不知道是不是符合你的要求
entity="pres:[email protected]"><tuple id="all"><status><basic>closed</basic><extension service="im">
offline</extension></status><contact priority="0.9">sip:[email protected]</contact><timestamp>
2005-6-17T11:44:3Z</timestamp></tuple></presence>339 <?xml version="1.0" encoding="UTF-8"?><presence
xmlns="urn:ietf:params:xml:ns:pidf" entity="pres:[email protected]"><tuple id="all"><status><basic>
open</basic><extension service="im">online</extension></status><contact priority="0.9">sip:[email protected]
</contact><timestamp>2005-6-17T11:44:3Z</timestamp></tuple></presence>335 <?xml version="1.0" encoding="UTF-8"?>
<presence xmlns="urn:ietf:params:xml:ns:pidf" entity="pres:[email protected]"><tuple id="all"><status><basic>
open</basic><extension service="im">online</extension></status><contact priority="0.9">sip:[email protected]
</contact><timestamp>2005-6-17T11:44:3Z</timestamp></tuple></presence>
实际上仔细看上面字串,每一段xml文本都描述了一个用户的在线状态,每次请求的用户数不同,所以返回的消息体中xml文本个数也不同。每个<?xml..前的数字,就是xml文本的长度!好像给出这个数字就是为了方便解析的!
<?xml version="1.0" encoding="UTF-8"?><presence xmlns="urn:ietf:params:xml:ns:pidf"entity="pres:[email protected]"><tuple id="all"><status><basic>closed</basic><extension service="im">offline</extension></status><contact priority="0.9">sip:[email protected]</contact><timestamp>2005-6-17T11:44:3Z</timestamp></tuple></presence>
<?xml version="1.0" encoding="UTF-8"?><presencexmlns="urn:ietf:params:xml:ns:pidf" entity="pres:[email protected]"><tuple id="all"><status><basic>open</basic><extension service="im">online</extension></status><contact priority="0.9">sip:[email protected]</contact><timestamp>2005-6-17T11:44:3Z</timestamp></tuple></presence>
<?xml version="1.0" encoding="UTF-8"?><presence xmlns="urn:ietf:params:xml:ns:pidf" entity="pres:[email protected]"><tuple id="all"><status><basic>open</basic><extension service="im">online</extension></status><contact priority="0.9">sip:[email protected]</contact><timestamp>2005-6-17T11:44:3Z</timestamp></tuple></presence>
共有 3
不知道对不对,可以看一下。我会回来看你给不给我分地。嘿嘿 :-)