A reading of the contents of the documents xml
Abstract: A reading of the contents of the documents xml
: 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 document information
*
Description: XML configuration file from the access configuration information. Excerpt form jdom.
*
Copyright: Copyright (c) 2004
*
Company: Harmonious
* @ Author TZL
* @ Version 1.0
* /
(Public class XMLReader
/ *
# Set root output configuration, format "info [2004-05-01 22:35:30] [name] logname (bc) [line] 86 msglog 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] per cent of L msg% m% n
* /
Static public Logger log = Logger.getLogger (XMLReader.class);
Protected Element m_RootElement = null;
Protected String m_webAppPath = null;
/ **
* Constructor function.
* @ Param xmlFile to read configuration files absolute path.
* /
Public XMLReader (String xmlFile) (
M_webAppPath = null;
Try (
PatternLayout layout = new PatternLayout ( "% d%-5p (yyyy-MM-dd HH: mm: ss) [name]% c (2)% L [line] [msg]% m% n");
ConsoleAppender appender = new ConsoleAppender (/ * new SimpleLayout (),*/ layout, "System.err");
Log.addAppender (appender);
SAXBuilder builder = new SAXBuilder ();
Document doc = null;
Doc = builder.build (new FileInputStream (xmlFile));
M_RootElement = doc.getRootElement ();
)
Catch (IOException ex) (
Log.error ( "XMLReader structure occurs when IO Error:" + ex.toString ());
)
Catch (JDOMException ex1) (
Log.error ( "XMLReader structure of XML documents when mistakes:" + ex1.toString ());
)
Catch (Exception ex) (
Log.error ( "XMLReader tectonic fault:" + ex.toString ());
)
)
/ **
* Constructor function. Configuration file must be specified for the release of the application's root directory / XmlConfig / Config.xml.
* @ Param servletObj a HttpServlet casually object.
* /
Public XMLReader (HttpServlet servletObj) (
M_webAppPath = servletObj.getServletContext (). GetRealPath ("/");
String configFileName m_webAppPath + = "XmlConfig / Config.xml";
Try (
PatternLayout layout = new PatternLayout ( "% d%-5p (yyyy-MM-dd HH: mm: ss) [name]% c (2)% L [line] [msg]% m% n");
ConsoleAppender appender = new ConsoleAppender (/ * new SimpleLayout (),*/ layout, "System.err");
Log.addAppender (appender);
SAXBuilder builder = new SAXBuilder ();
Document doc = null;
Doc = builder.build (new FileInputStream (configFileName));
M_RootElement = doc.getRootElement ();
)
Catch (IOException ex) (
Log.error ( "XMLReader structure occurs when IO error (/ XmlConfig / Config.xml):" + ex.toString ());
)
Catch (JDOMException ex1) (
Log.error ( "XMLReader structure of XML documents when mistakes (/ XmlConfig / Config.xml):" + ex1.toString ());
)
Catch (Exception ex) (
Log.error ( "XMLReader tectonic fault (/ XmlConfig / Config.xml):" + ex.toString ());
)
)
/ **
* Application posted on the web web server root directory absolute path, the final list has been split at.
* @ Return to return to the web application posted on the web server absolute path of the root directory.
* /
Public String getWebAppPath () (
Return m_webAppPath;
)
/ **
* Configuration file access configuration information.
* @ Param key to access the profile name.
* @ Param curRootName View the initial node name, and if the null start from the root View.
* @ Return the string configuration.
* /
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) (
KeyNode.getTextTrim value = ();
)
Return value;
)
/ **
* Based on the access node name. Breadth traversal, recursive call.
* @ Param nodeName node name.
* @ Param curRoot View from the beginning of the initial nodes, if it is null start from the root View.
* @ Return from the specified node to return to find the first node. If there is no return null.
* /
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 ();
= NestElements.iterator Iterator iterator ();
While (iterator.hasNext () & & retElement == null) (
GetElement retElement = ((Element) iterator.next (), nodeName);
)
)
)
Return retElement;
)
/ **
* Was designated node attributes.
* @ Param elementName node name.
* @ Param attName to obtain the names of the properties.
* @ Return looking for the value of the attribute.
* /
Public String getElementAtrribute (String elementName, String attName)
(
Element el = getElement (null, elementName);
If (null == el)
Return null;
Return el.getAttributeValue (attName);
)
)
Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd
↑ Back
Tags: java documents, java xml






