Home > Back-end >  Three methods of Java to parse the XML file, equipped with comments.
Three methods of Java to parse the XML file, equipped with comments.

Time:05-05

For example, to parse the XML file
 
<? The XML version="1.0" encoding="utf-8"?>

Tony Blair
10 Downing Street, London, UK

(061) 98765 & lt;/tel>
(061) 98765 & lt;/fax>
[email protected] & lt;/email>

Bill Clinton
The White House, USA

(001) 6400 98765 & lt;/tel>
(001) 6400 98765 & lt;/fax>
[email protected] & lt;/email>



]
Java is one of three ways to parse the XML file
First of all, we first analyze the us to parse the XML file
People have two person, with the name, address, tel, fax and email,
When we resolve to step by step according to the classification by foreach parsing,

First of all we should according to the XML file to create a Person class
The
tag defines the XML filesAnd defines the attributes of the get set method and toString method
 
Package XML;

Public class Person {
Private String personid.
private String name;
Private String address;
Private String tel;
Private String fax;
Private String email;

@ Override
Public String toString () {
Return "Person {+"
"Personid='" + personid +' \ ' '+
"And name='" + name +' \ ' '+
", address='" + address +' \ ' '+
", tel='" + tel +' \ ' '+
='" + ", fax fax +' \ '+'
", email='" + email +' \ ' '+
'} ';
}

Public String getPersonid () {
Return personid.
}

Public void setPersonid (String personid) {
Enclosing personid=personid;
}

Public String getName () {
return name;
}

Public void elegantly-named setName (String name) {
this.name=name;
}

Public String getAddress () {
The return address.
}

Public void setAddress (String address) {
This. The address=address;
}

Public String getTel () {
Return tel;
}

Public void setTel (String tel) {
This. Tel=tel;
}

Public String getFax () {
Return fax;
}

Public void setFax (String fax) {
This. Fax=fax;
}

Public String getEmail () {
Return email;
}

Public void setEmail (String email) {
This. Email=email;
}
}



Establish good Person class we begin to implement three kinds of ways to parse the XML file,

Directly on the code, the supplementary analytic:
 
Package XML;

The import org. Jdom2. Element;
The import org. Jdom2. JDOMException;
The import org. Jdom2. Input. SAXBuilder;
import org.junit.Test;
The import org. W3c. Dom. Document;
The import org. W3c. Dom. Node;
The import org. W3c. Dom. NodeList;
The import org. XML. Sax. SAXException;

Import javax.mail, XML parsers. *;
Import the Java. IO. IOException;
Import the Java. IO. InputStream;
Import the Java. Util. ArrayList;
import java.util.Arrays;
import java.util.List;

Public class XMLDemo {

//SAX parsing XML file
@ Test
Public void saxParseXML () throws a ParserConfigurationException, SAXException, IOException {
//1, to create a SAX parser factory object
SAXParserFactory SAXParserFactory=SAXParserFactory. NewInstance ();
//2, through the factory object to create the SAX parser
SAXParser SAXParser=saxParserFactory. NewSAXParser ();
//3, create a data processor (we need to write your own)
PersonHandler PersonHandler=new PersonHandler ();
//4, start parsing
InputStream is=Thread. CurrentThread (). GetContextClassLoader () getResourceAsStream (" XML/person. XML ");
SaxParser. Parse (is, personHandler);
List Persons=personHandler. GetPersons ();
For (the Person p: persons) {
System. The out. Println (p);
}
}




//the DOM to parse the XML file
/*
1, based on tree structure, through the parser one-time document loaded into memory, so the memory is bigger, can random access, more flexible, more suitable for use in web development
*/
@ Test
Public void domParseXML () throws a ParserConfigurationException, IOException, SAXException {
//1, create a DOM parser factory object
DocumentBuilderFactory factory=DocumentBuilderFactory. NewInstance ();
//2, create a parser object through a factory object
DocumentBuilder DocumentBuilder=factory. NewDocumentBuilder ();
//3, parse the document
InputStream is=Thread. CurrentThread (). GetContextClassLoader () getResourceAsStream (" XML/person. XML ");
Document doc=documentBuilder. Parse (is);//this code, after the completion of the entire XML document has been loaded into memory, stored in a tree form
Read data from memory//4,
//get the data from the doc, and XML it doesn't matter, because in the code above is completed, the XML document have been loaded into the memory
//get all the nodes of node name for a person to return to the node set
NodeList personNodeList=doc. GetElementsByTagName (" person ");
ArrayList Persons=new ArrayList<> (a);
Person p=null;
//the loop iteration two
For (int I=0; I & lt; PersonNodeList. GetLength (); I++) {
The Node personNode=personNodeList. Item (I);
P=new Person ();
//get the node attribute values
String personid=personNode. GetAttributes (). The getNamedItem (" personid "). The getNodeValue ();
P. etPersonid (personid);
//get all the child nodes of the current node
NodeList.childnodes=personNode. GetChildNodes ();
For (int j=0; J & lt; .childnodes. GetLength (); J++) {
The Node item=.childnodes. Item (j);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related