Reading XML files using Java

Source: Internet
Author: User

Before xmlreader accepts XML documents and parses the read XML files, it needs to register a contenthandler with xmlreader, which is equivalent to an event listener, contenthandler defines many methods. When xmlreader reads the appropriate content, it will throw the corresponding event and delegate the event processing permission to contenthandler, and call the corresponding method to respond.

Contenthandler is an interface. when processing a specific XML file, you need to create a class that implements this interface for it to process specific events. The Org. xml. Sax. helpers. defaulthandler class implements this interface, so we only need to inherit defaulthandler and override the method. Several common methods are described in the following program:

Package XML;

Import java. Io. filereader;
Import java. Io. ioexception;

Import javax. xml. parsers. parserconfigurationexception;

Import org. xml. Sax. attributes;
Import org. xml. Sax. inputsource;
Import org. xml. Sax. saxexception;
Import org. xml. Sax. xmlreader;
Import org. xml. Sax. helpers. defaulthandler;
Import org. xml. Sax. helpers. xmlreaderfactory ;;

Public class saxtest extends defaulthandler {

Public static void main (string [] ARGs) throws parserconfigurationexception, saxexception, ioexception {

Xmlreader xr = xmlreaderfactory. createxmlreader ("org. Apache. xerces. parsers. saxparser ");
XR. setcontenthandler (New saxtest ());
XR. parse (New inputsource (New filereader ("test. xml ")));
}

// Call this method at the beginning of the document
Public void startdocument () throws saxexception {

System. Out. println ("startdocument :");
 
}
// Call this method at the end of the document
Public void enddocument () throws saxexception {

System. Out. println ("enddocument :");
}

// Call this method when a start label is encountered,

// The namespaceuri parameter indicates the domain name, localname indicates the tag name, And QNAME indicates the prefix of the tag. If the domain name is not used, neither of the two parameters is null. ATTS is the list of attributes contained in this tag. Through ATTS, you can get all the attribute names and corresponding values.

Public void startelement (string Uri, string localname, string QNAME, attributes) throws saxexception {

System. Out. println ("startelement:" + localname );

}

// Call this method when an end label is encountered

Public void endelement (string Uri, string localname, string QNAME) throws saxexception {

System. Out. println ("endelement:" + localname );
}

// When a string in the tag is encountered, this method is called. Its parameter is a character array, and the start position and length of the string read in this array.

Public void characters (char [] CH, int start, int length) throws saxexception {

String data = new string (CH, start, length );
System. Out. println (data );
 
}
}

When we create the following test. xml file

Test. xml

-------------------

<? XML version = "1.0"?>
<School>
<Student>
<ID> 123456 </ID>
<Address> 408 </address>
<Name> wxm </Name>
<Sex> male </sex>
</Student>
</School>

-------------------

Run the program and the output result is as follows:

Startdocument:
Startelement: School

 
Startelement: Student


Startelement: ID
123456
Endelement: ID


Startelement: Address
408
Endelement: Address


Startelement: Name
Wxm
Endelement: Name


Startelement: Sex
Male
Endelement: Sex

 
Endelement: Student

Endelement: School
Enddocument:

----------------

End!

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.