Two ways to parse XML files (strings)-----Saxreader and Documenthelper

Source: Internet
Author: User

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 ();
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.