需要解析如下的XML:
<iq id="EHS8g-5" to="123" from="456" type="set">
<command xmlns="http://jabber.org/protocol/commands" node="send-sms" action="execute">
<property name="message">
<message id="EHS8g-4" from="123" type="chat">
<body>hello world</body>
</message>
</property>
<property name="targets">
<list>
<value>15615627018</value>
<value>12990792706</value>
<value>14293115658</value>
<value>12969840178</value>
</list>
</property>
</command>
</iq>
public IQ parseIQ(XmlPullParser parser) throws Exception {
SendSmsIQ smsIQ = new SendSmsIQ();
// parse node attr value
smsIQ.setNode((parser.getAttributeValue("", "node")));
boolean done = false;
while (!done) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("property")) {
if (parser.getAttributeValue("", "name").equals("message")) {
// parse property->message
smsIQ.setMessage(parseMessage(parser));
} else if (parser.getAttributeValue("", "name").equals("targets")) {
// parse property->targets
smsIQ.setTargetList(parseList(parser));
}
}
}
if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals("command")) {
done = true;
}
}
}
System.out.println("smsIQ.toXML(): " + smsIQ.toXML());
return smsIQ;
}
private Message parseMessage(XmlPullParser parser) throws Exception {
Message message = new Message();
boolean done = false;
while (!done) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("message")) {
message.setPacketID(parser.getAttributeValue("", "id"));
message.setFrom(parser.getAttributeValue("", "from"));
message.setType(Type.valueOf(parser.getAttributeValue("",
"type")));
message.setBody(parseBody(parser));
}
}
if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals("message")) {
done = true;
}
}
}
System.out.println("Message:" + message.toXML());
return message;
}
private String parseBody(XmlPullParser parser) throws Exception {
String body = "";
boolean done = false;
while (!done) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) {
} else if (eventType == XmlPullParser.TEXT) {
body = parser.getText();
} else if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals("body")) {
done = true;
}
}
}
return body;
}
private List<String> parseList(XmlPullParser parser) throws Exception {
List<String> list = null;
boolean done = false;
while (!done) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("list")) {
list = parseValue(parser);
}
} else if (eventType == XmlPullParser.END_TAG) {
String name = parser.getName();
if (name.equals("property")) {//parseValue(parser) 需要</list>来终止,所以这里要用property来终止
done = true;
}
}
}
return list;
}
private List<String> parseValue(XmlPullParser parser) throws Exception {
List<String> list = new ArrayList<String>();
boolean done = false;
while (!done) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) {
} else if (eventType == XmlPullParser.TEXT) {
String value = parser.getText();
System.out.println(value);
list.add(value);
}
else if (eventType == XmlPullParser.END_TAG) {
if(!parser.getName().equals("value")){//当解析到list的时候结束,否则不结束,一致循环解析value
done = true;
}
System.out.println("=======end:"+parser.getName());
}
}
return list;
}