A class that reads the contents of an XML file

Source: Internet
Author: User
Tags config constructor key log string tostring root directory log4j
Xml

A class that reads the contents of an XML file
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.*;

/**
* <p>title: <font color= "Steelblue" size= "ten" > read XML file Information </font></p>
* <p>description: <font color= "Steelblue" > obtains configuration information from the XML configuration file. Excerpt form Jdom. </font></p>
* <p>copyright: <font color= "Steelblue" >copyright (c) 2004</font></p>
* <p>company: <font color= "Steelblue" >Harmonious</font></p>
* @author <font color= "Steelblue" >TZL</font>
* @version <font color= "Steelblue" >1.0</font>
*/

public class XMLReader {
/*
#设置根的输出配置, format "info [2004-05-01 22:35:30] [Name]logname (B.C ) [Line] 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=%-5p [%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 read. </font>
*/
Public XMLReader (String xmlFile) {
M_webapppath = null;
try {
Patternlayout layout = new Patternlayout ("%-5p%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 in XmlReader construct: + ex.tostring ());
}
catch (Jdomexception Ex1) {
Log.error (Error parsing xml file at 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 a/xmlconfig/config.xml under the root directory of the published application. </font>
* @param servletobj <font color= "Steelblue" > A random HttpServlet object. </font>
*/
Public XMLReader (HttpServlet servletobj) {
M_webapppath = Servletobj.getservletcontext (). Getrealpath ("/");
String configfilename = M_webapppath + "Xmlconfig/config.xml";

try {
Patternlayout layout = new Patternlayout ("%-5p%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 (/xmlconfig/config.xml) occurred while XmlReader constructs:" + ex.tostring ());
}
catch (Jdomexception Ex1) {
Error parsing xml file (/xmlconfig/config.xml) at Log.error (XmlReader): "+ ex1.tostring ());
}
catch (Exception ex) {
Log.error ("XmlReader construction Error (/xmlconfig/config.xml):" + ex.tostring ());
}
}

/**
* <font color= "Orange" >web application published in the Web server's absolute path root directory, and finally has a directory separator. </font>
* @return <font color= "Tomato" > returns the root directory where Web applications are published in the absolute path of the Web server. </font>
*/
Public String Getwebapppath () {
return m_webapppath;
}

/**
* <font color= "Orange" > Obtain configuration information from the configuration file. </font>
* @param key <font color= "Steelblue" > The name of the configuration to get. </font>
* @param currootname <font color= The start node name of the "Steelblue" > lookup, if null from the root. </font>
* @return <font color= "Tomato" > Configured strings. </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 node by name. Breadth traversal, recursive invocation. </font>
* @param the name of the nodename <font color= "Steelblue" > node. </font>
* @param curroot <font color= "Steelblue" > The starting node from which to start the lookup, if null from the root. </font>
* @return <font color= "Tomato" > Returns the first node found under the specified node. If NULL is not 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 iterator = Nestelements.iterator ();
while (Iterator.hasnext () && null = = Retelement) {
Retelement = GetElement (Element) Iterator.next (), nodename);
}
}
}

return retelement;
}

/**
* <font color= "Orange" > Get the properties of the specified node. </font>
* @param elementname <font color= The name of the "Steelblue" > node. </font>
* @param attname <font color= "Steelblue" > The name of the property to be obtained. </font>
* @return <font color= "Tomato" > The value of the property to find. </font>
*/
public string Getelementatrribute (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.