public class TestXml2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String transMessage = "<?xml version=\"1.0\" encoding=\"GBK\"?><message>" + "<body>" + "<ticketNotify>"
+ "<ticket id=\"6000012007051000000231\" dealTime=\"20070510165423\" status=\"0000\" message=\"成功,系统处理正常\"/>"
+ "<ticket id=\"6000012007051000000232\" dealTime=\"20070510165424\" status=\"2012\" message=\"禁止倍投\"/>"
+ "</ticketNotify>" + "</body></message>";
try {
Document document=DocumentHelper.parseText(transMessage);
Element root=document.getRootElement();
Iterator iterator=root.element("body").element("ticketNotify").elementIterator();
while(iterator.hasNext()) {
Element element=(Element) iterator.next();
System.out.print(element.attributeValue("id")+" ");
System.out.print(element.attributeValue("dealTime")+" ");
System.out.println(element.attributeValue("status")+" ");
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String transMessage = "<?xml version=\"1.0\" encoding=\"GBK\"?><message>" + "<body>" + "<ticketNotify>"
+ "<ticket id=\"6000012007051000000231\" dealTime=\"20070510165423\" status=\"0000\" message=\"成功,系统处理正常\"/>"
+ "<ticket id=\"6000012007051000000232\" dealTime=\"20070510165424\" status=\"2012\" message=\"禁止倍投\"/>"
+ "</ticketNotify>" + "</body></message>";
try {
Document document=DocumentHelper.parseText(transMessage);
Element root=document.getRootElement();
Iterator iterator=root.element("body").element("ticketNotify").elementIterator();
while(iterator.hasNext()) {
Element element=(Element) iterator.next();
System.out.print(element.attributeValue("id")+" ");
System.out.print(element.attributeValue("dealTime")+" ");
System.out.println(element.attributeValue("status")+" ");
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
pom.xml文件添加如下依赖
<!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>