SAX reads XML files

zhaozj2021-02-12  149

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: tzl * @version 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_rootellement = null; protected string m_webapppppppppppppppppPath = null; / ** * constructor. * @Param XMLFILE The absolute path to the configuration file to be read.

* / public xmlreader (string xmlfile) {m_webapppp_ = null; try {patternLayout Layout = new patternLayout ("% - 5P% D {YYYY-MM-DD HH: mm: SS} [Name]% c {2 } [Line]% L [MSG]% m% N "); consolerappender appender = new consolerappender (/ * new simpleness (), * / layout," system.err "); log.addappender; saxbuilder builder = New saxbuilder (); document. Nbspdoc = null; doc = builder.build (new fileinputstream (xmlfile)); m_rootellement = doc.getrootElement ();} catch (ioException ex) {log.error ("IO error in XmlReader configuration:" ex. Tnowtring )));} Catch (JDMEXCEPTION EX1) {log.error ("Analysis of XML file when the XMLReader constructor is wrong:" EX1.TOSTRING ());} catch (Exception EX) {log.error ("XmlReader construct error:" EX.TOSTRING ());}} / ** * Constructor. The configuration file must be specified as the root of the published application /Xmlconfig/config.xml. * @Param servletobj Any httpservlet object.

* / 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 "); consOLLAPPENDER APPENDER = New ConsoleAppender (/ * New SimpleLayout (), * / layout, "system.err"); log.addappender; Saxbuilder Builder = new saxbuilder (); document. nbspdoc = null; doc = builder.build (new FileInputStream (configFileName)); m_RootElement = doc.getRootElement ();} catch (IOException ex) {log.error ( "IO error when XMLReader configuration (/XmlConfig/Config.xml : " EX.TOSTRING ());} catch (jdomException ex1) {log.error (" XMLReader configuration Analysis XML file error (/Xmlconfig/config.xml): " EX1.TOSTRING ());} catch (Exception ex) {log.error ("xmlreader constructed error (/Xmlconfig/config.xml):" ex.Tostring ());}} / ** * web application is published in Web The absolute path of the server is the root directory, and it has been a directory split. * @return Returns the root directory of the web application posted on the absolute path of the web server. * / public string getWebAppPath () {return m_webapppppppp_} / ** * Get configuration information from the configuration file. * @Param Key The configuration name to get. * @Param CurrootName Find the start node name, if you start looking for NULL from the root. * @return Configure strings.

* / 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;} / ** * obtain nodes according to the name. Side traversal, recursive call. * @Param nodename Name of the node. * @Param Curroot From the starting node that starts looking up, if you start looking for null from the root. * @return Returns the first node found under the specified node. If NULL is not returned. * / 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;} / ** * Get the properties of the specified node. * @Param ElementName Name of the node. * @Param attname The name of the attribute to get. * @return The value of the properties to find.

转载请注明原文地址:https://www.9cbs.com/read-6699.html

New Post(0)