Using SAX parsing to read XML files

Source: Internet
Author: User
XML This is my first blog, today in the "Java Application Development detailed" a book, the book is about XML programming, so according to the steps of the book to test themselves, but how to test is not successful, and then I looked over the source, found in the reading of the location of the XML file is wrong, and then made changes, Also great, the heart gush out a sense of achievement, is to post the source code and give a bit to share:

Using an XML file to connect to the MySQL database, the Database.conf.xml file is as follows:

<database-conf>
<datasource>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://127.0.0.1:3306/j2ee14</url>
<user>bn</user>
<password>bn</password>
</datasource>
</database-conf>

Create a new handler to resolve the XML configuration (Configparser.java):

Package COM.J2EE14.CH4;
Import org.xml.sax.Attributes;
Import Org.xml.sax.helpers.DefaultHandler;
Import org.xml.sax.SAXException;

Import java.util.Properties;

/**
*configparser extends DefaultHandler, which is used to obtain connection properties for the database
*/
public class Configparser extends DefaultHandler
{
Define a property to hold the attribute value
Private Properties props;
Private String Currentname;
Private StringBuffer CurrentValue =new stringbuffer ();

Builder Initialization Props

Public Configparser ()
{
This.props=new Properties ();
}
Public Properties GetProps ()
{
return this.props;
}

Defines a method that begins parsing an element. This is to extract the name xxx from <xxx>

public void Startelement (String uri,string localname,string qname,attributes Attributes) throws Saxexception
{
Currentvalue.delete (0,currentvalue.length ());
This.currentname=qname;
}

This is to add the value between <xxx></xxx> to CurrentValue

public void characters (char[] ch, int start, int length) throws Saxexception {

Currentvalue.append (CH, start, length);

}

/**
* Save the related attributes in the XML configuration file to the Properties object
*/

public void EndElement (String uri,string localname,string qName) throws Saxexception
{
Props.put (Qname.tolowercase (), currentvalue.tostring (). Trim ());
}
}

A class used to parse XML, which other programs use to obtain the connection properties of the database (Parsedatabaseconfig
. Java):

Package COM.J2EE14.CH4;

Import java.util.Properties;
Import Javax.xml.parsers.SAXParser;
Import Javax.xml.parsers.SAXParserFactory;

/**
* Other programs through the Parsedatabaseconfig to obtain the configuration information of the database,
* This makes the coupling between classes loosely
*/
public class Parsedatabaseconfig
{
Define a property to hold the attribute value
Private Properties props;

Public Properties GetProps ()
{
return this.props;
}
/**
* Parse XML configuration file, save attributes
*/
public void Parse (String filename) throws Exception
{
The object of our parser
Configparser handler=new Configparser ();

Get Sax Factory Objects
SAXParserFactory factory=saxparserfactory.newinstance ();
Factory.setnamespaceaware (FALSE);
Factory.setvalidating (FALSE);

Get Sax parsing
SAXParser Parser=factory.newsaxparser ();

Try
{
To associate the parser with parsing object XML, start parsing
Parser.parse (Filename,handler);

Gets the properties after the resolution is successful
Props=handler.getprops ();
}
Finally
{
Factory=null;
Parser=null;
Handler=null;
}
}

}

[1] [2] Next page



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.