A class for reading XML file content

Source: Internet
Author: User
A class for reading XML file content
Author: javazealot (post) Source: justjsp.com warehouse receiving time: hits: 360

A class for reading XML file content
Package project. util. xml;

Import java. Io .*;
Import java. util .*;
Import javax. servlet. http .*;
Import org. Apache. log4j .*;
Import org. JDOM .*;
Import org. JDOM. Input .*;

/**
*

title: Read XML file information


*

description: get the configuration information from the xml configuration file. Excerpt form JDOM.


*

copyright: copyright (c) 2004


*

company: harmonious


* @ author tzl
* @ version 1.0
*/

public class xmlreader {
/*
# Set the root output configuration in the format of "info [22:35:30] [name] LOGNAME (B. c) [Line] 86 MSG --> log information "
log4j. rootlogger = debug, rootappender
log4j. appender. rootappender = org. apache. log4j. rollingfileappender
log4j. appender. rootappender. file = E:/mapxtremesmpl. log
log4j. appender. rootappender. maxfilesize = 1000kb
log4j. appender. rootappender. layout = org. apache. log4j. patternlayout
log4j. appender. rootappender. layout. conversionpattern = %-5 p [% d {yyyy-mm-dd hh: mm: SS}] [name] % c {2} [Line] % l msg --> % m % n
*/
static public logger log = logger. getlogger (xmlreader. class);
protected element m_rootelement = NULL;
protected string m_webapppath = NULL;

/**
* <Font color = "orange"> constructor. </Font>
* @ Param xmlfile <font color = "steelblue"> the absolute path of the configuration file to be read. </Font>
*/
Public xmlreader (string xmlfile ){
M_webapppath = NULL;
Try {
Patternlayout layout = new patternlayout ("%-5 p % d {yyyy-mm-dd hh: mm: SS} [name] % c {2} [Line] % L [MSG] % m % N ");
Consoleappender appender = new consoleappender (/* New simplelayout (), */layout, "system. Err ");
Log. addappender (appender );

Saxbuilder builder = new saxbuilder ();
Document. nbspdoc = NULL;
Doc = builder. Build (New fileinputstream (xmlfile ));
M_rootelement = Doc. getrootelement ();
}
Catch (ioexception ex ){
Log. Error ("Io error during xmlreader Construction:" + ex. tostring ());
}
Catch (jdomexception ex1 ){
Log. Error ("XML file analysis error during xmlreader Construction:" + ex1.tostring ());
}
Catch (exception ex ){
Log. Error ("xmlreader construction error:" + ex. tostring ());
}
}

/**
* <Font color = "orange"> constructor. The configuration file must be specified as/xmlconfig/config. xml under the root directory of the published application. </Font>
* @ Param servletobj <font color = "steelblue"> An httpservlet object. </Font>
*/
Public xmlreader (httpservlet servletobj ){
M_webapppath = servletobj. getservletcontext (). getrealpath ("/");
String configfilename = m_webapppath + "xmlconfig/config. xml ";

Try {
Patternlayout layout = new patternlayout ("%-5 p % d {yyyy-mm-dd hh: mm: SS} [name] % c {2} [Line] % L [MSG] % m % N ");
Consoleappender appender = new consoleappender (/* New simplelayout (), */layout, "system. Err ");
Log. addappender (appender );

Saxbuilder builder = new saxbuilder ();
Document. nbspdoc = NULL;
Doc = builder. Build (New fileinputstream (configfilename ));
M_rootelement = Doc. getrootelement ();
}
Catch (ioexception ex ){
Log. Error ("Io error during xmlreader construction (/xmlconfig/config. XML):" + ex. tostring ());
}
Catch (jdomexception ex1 ){
Log. Error ("XML file analysis error during xmlreader construction (/xmlconfig/config. XML):" + ex1.tostring ());
}
Catch (exception ex ){
Log. Error ("xmlreader construction error (/xmlconfig/config. XML):" + ex. tostring ());
}
}

/**
* <Font color = "orange"> the absolute path root directory where the web application is published on the Web server. A directory delimiter is provided. </Font>
* @ Return <font color = "tomato"> returns the root directory of the absolute path where the web application is published on the Web server. </Font>
*/
Public String getwebapppath (){
Return m_webapppath;
}

/**
* <Font color = "orange"> obtain the configuration information from the configuration file. </Font>
* @ Param key <font color = "steelblue"> name of the configuration to be obtained. </Font>
* @ Param currootname <font color = "steelblue"> the name of the Start Node to be searched. If it is null, It is searched from the root. </Font>
* @ Return <font color = "tomato"> the configured string. </Font>
*/
Public String getelementvalue (string currootname, string key ){
String value = NULL;
Element curroot = getelement (null, currootname );
If (null = curroot ){
Curroot = m_rootelement;
}
Element keynode = getelement (curroot, key );
If (null! = Keynode ){
Value = keynode. gettexttrim ();

}
Return value;
}

/**
* <Font color = "orange"> get the node by name. SPAN traversal and recursive calling. </Font>
* @ Param nodename <font color = "steelblue"> name of the node. </Font>
* @ Param curroot <font color = "steelblue"> Start node from which the query starts. If it is null, start from the root node. </Font>
* @ Return <font color = "tomato"> return the first node found under the specified node. If no value is returned, null is returned. </Font>
*/
Private element getelement (element curroot, string nodename ){
Element retelement = NULL;

If (null = nodename)
Return m_rootelement;

If (null = curroot ){
Curroot = m_rootelement;
}

If (null! = Curroot ){
Retelement = curroot. getchild (nodename );
If (null = retelement ){
List nestelements = curroot. getchildren ();
Iterator = nestelements. iterator ();
While (iterator. hasnext () & null = retelement ){
Retelement = getelement (element) iterator. Next (), nodename );
}
}
}

Return retelement;
}

/**
* <Font color = "orange"> obtain the attributes of a specified node. </Font>
* @ Param elementname <font color = "steelblue"> name of the node. </Font>
* @ Param attname <font color = "steelblue"> name of the property to be obtained. </Font>
* @ Return <font color = "tomato"> value of the property to be searched. </Font>
*/
Public String getelementatrriname (string elementname, string attname)
{
Element El = getelement (null, elementname );
If (null = El)
Return NULL;

Return El. getattributevalue (attname );
}

}

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.