Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement a class that reads the contents of a xml file

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to achieve a class that reads the contents of xml files. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

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

/ * *

*

Title: reading xml file information

*

Description: get 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 output configuration of the root in the format of "info [2004-05-01 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=%-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

/ * *

* Constructor.

The absolute path of the configuration file to be read by * @ param xmlFile.

, /

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 occurred during XMLReader construction:" + ex.toString ())

}

Catch (JDOMException ex1) {

Log.error ("error parsing XML file during XMLReader construction:" + ex1.toString ())

}

Catch (Exception ex) {

Log.error ("XMLReader construction error:" + ex.toString ())

}

}

/ * *

* Constructor. The configuration file must be specified as / XmlConfig/Config.xml under the root directory of the published application.

* @ 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")

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 in XMLReader construction (/ XmlConfig/Config.xml):" + ex.toString ())

}

Catch (JDOMException ex1) {

Log.error ("error parsing XML file during XMLReader construction (/ XmlConfig/Config.xml):" + ex1.toString ())

}

Catch (Exception ex) {

Log.error ("XMLReader construction error (/ XmlConfig/Config.xml):" + ex.toString ())

}

}

/ * *

* the web application is published in the root directory of the absolute path of the web server, and finally there is a directory separator.

* @ return returns the root directory where the web application is published in the absolute path of the web server.

, /

Public String getWebAppPath () {

Return m_webAppPath

}

/ * *

* obtain configuration information from the configuration file.

* @ param key the name of the configuration to get.

The name of the starting node of the * @ param curRootName lookup, if null starts from the root.

The string configured by * @ return.

, /

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

}

/ * *

* get the node by name. Breadth traversal, recursive call.

Name of the * @ param nodeName node.

* @ param curRoot the start node from which to start the search, if it is null, start from the root.

* @ return returns the first node found under the specified node. If no null is 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 attributes of the specified node.

Name of the * @ param elementName node.

* @ param attName the name of the attribute to get.

The value of the attribute that * @ return is looking for.

, /

Public String getElementAtrribute (String elementName, String attName)

{

Element el = getElement (null, elementName)

If (null = = el)

Return null

Return el.getAttributevalue (attName)

}

}

This is the end of this article on "how to achieve a class that reads the contents of xml files". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report