First, use the Saxreader way to parse: instance test class Testxml
Package *
Import Java.io.ByteArrayInputStream;
Import Java.util.Iterator;
Import org.dom4j.Document;
Import org.dom4j.Element;
Import Org.dom4j.io.SAXReader;
public class Testxml {
/**
* @param args
*/
public static void Main (string[] args) {
try {
TODO auto-generated Method Stub
Defines the XML string to parse
String transmessage = "<?xml version=/" 1.0/"encoding=/" gbk/"?><message>"
+ "<body>"
+ "<ticketNotify>"
+ "<ticket id=/" 6000012007051000000231/"dealtime=/" 20070510165423/"status=/" 0000/"message=/" successful, System handling normal/"/>"
+ "<ticket id=/" 6000012007051000000232/"dealtime=/" 20070510165424/"status=/" 2012/"message=/" Prohibit double cast/"/>"
+ "</ticketNotify>" + "</body></message>";
To create an XML parsing object
Saxreader reader = new Saxreader ();
Define a document
Document document = NULL;
Converts a string to a
Document = Reader.read (new Bytearrayinputstream (transmessage
. GetBytes ("GBK"));
Get the root node of XML (Message)
Element root = Document.getrootelement ();
Variables that define the body of a child loop
Element Ticket=null;
Iterator tickets = null;
for (tickets = root.element ("body"). Element ("Ticketnotify"). Elementiterator (); Tickets.hasnext ();) {
Ticket = (Element) tickets.next ();
System.out.print (Ticket.attributevalue ("id") + "");
System.out.print (Ticket.attributevalue ("dealtime") + "");
System.out.println (Ticket.attributevalue ("status"));
}
Element.asxml method to get all the XML data that includes the label
System.out.println (Root.element ("Body"). Asxml ());
catch (Exception e) {
E.printstacktrace ();
}
}
}
Second, Documenthelper mode analysis: Instance test class Testxml
Package *;
Import Java.io.ByteArrayInputStream;
Import Java.util.Iterator;
Import org.dom4j.Document;
Import Org.dom4j.DocumentHelper;
Import org.dom4j.Element;
Import Org.dom4j.io.SAXReader;
public class Testxml {
/**
* @param args
*/
public static void Main (string[] args) {
try {
TODO auto-generated Method Stub
Defines the XML string to parse
String transmessage = "<?xml version=/" 1.0/"encoding=/" gbk/"?><message>"
+ "<body>"
+ "<ticketNotify>"
+ "<ticket id=/" 6000012007051000000231/"dealtime=/" 20070510165423/"status=/" 0000/"message=/" successful, System handling normal/"/>"
+ "<ticket id=/" 6000012007051000000232/"dealtime=/" 20070510165424/"status=/" 2012/"message=/" Prohibit double cast/"/>"
+ "</ticketNotify>" + "</body></message>";
To create an XML parsing object
Saxreader reader = new Saxreader ();
Define a document
Document document = NULL;
Converts a string to a
Document = Reader.read (new Bytearrayinputstream (transmessage
. GetBytes ("GBK"));
Document = Documenthelper.parsetext (transmessage);
Get the root node of XML (Message)
Element root = Document.getrootelement ();
Variables that define the body of a child loop
Element Ticket=null;
Iterator tickets = null;
for (tickets = root.element ("body"). Element ("Ticketnotify"). Elementiterator (); Tickets.hasnext ();) {
Ticket = (Element) tickets.next ();
System.out.print (Ticket.attributevalue ("id") + "");
System.out.print (Ticket.attributevalue ("dealtime") + "");
System.out.println (Ticket.attributevalue ("status"));
}
Element.asxml method to get all the XML data that includes the label
System.out.println (Root.element ("Body"). Asxml ());
catch (Exception e) {
E.printstacktrace ();
}
}
}