java读取xml节点元素,主要使用java提供的解析xml的工具类SAXParserFactory,如下代码:
package xml.xmlreaderimport java.io.File
import java.net.URL
import java.util.Properties
import javax.xml.parsers.SAXParser
import javax.xml.parsers.SAXParserFactory
public class CFGParser {//解析xml文件的工具类
private Properties props
public Properties getProps() {
return props
}
public void setProps(Properties props) {
this.props = props
}
public void parse(String filename) throws Exception
{
CFGHandler handler = new CFGHandler()
SAXParserFactory factory = SAXParserFactory.newInstance()
factory.setNamespaceAware(false)
factory.setValidating(false)
SAXParser parser = factory.newSAXParser()
URL confURL = super.getClass().getClassLoader().getResource(filename)
if (confURL == null) {
System.out.println("Can't find configration file.")
return
}
try
{
parser.parse(confURL.toString(), handler)
this.props = handler.getProps()
}
finally {
factory = null
parser = null
handler = null
}
}
public void parseFile(String filename)
throws Exception
{
CFGHandler handler = new CFGHandler()
SAXParserFactory factory = SAXParserFactory.newInstance()
factory.setNamespaceAware(false)
factory.setValidating(false)
SAXParser parser = factory.newSAXParser()
File f = new File(filename)
if ((f == null) || (!f.exists()))
return
try
{
parser.parse(f, handler)
this.props = handler.getProps()
}
finally {
factory = null
parser = null
handler = null
}
}
}
package xml.xmlreader
import java.util.Properties
import org.xml.sax.Attributes
import org.xml.sax.SAXException
import org.xml.sax.helpers.DefaultHandler
public class CFGHandler extends DefaultHandler
{
private Properties props
private String currentSet
private String currentName
private StringBuffer currentValue = new StringBuffer()
public CFGHandler()
{
this.props = new Properties()
}
public Properties getProps() {
return this.props
}
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException
{
this.currentValue.delete(0, this.currentValue.length())
this.currentName = qName
}
public void characters(char[] ch, int start, int length) throws SAXException
{
this.currentValue.append(ch, start, length)
}
public void endElement(String uri, String localName, String qName)
throws SAXException
{
this.props.put(qName.toLowerCase(), this.currentValue.toString().trim())
}
}
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<xml-body>
<refresh_userlist desc="用户列表刷新间隔时间(秒)">6</refresh_userlist>
<refresh_message desc="短消息刷新间隔时间(秒)">10</refresh_message>
<morningbegin desc="上午上班时间">23:00</morningbegin>
<morningend desc="上午下班时间">12:00</morningend>
<afternoonbegin desc="下午上班时间">18:00</afternoonbegin>
</xml-body>
jsp获取各个节点的值:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<jsp:useBean id="cfgp" scope="page" class="xml.xmlreader.CFGParser"></jsp:useBean>
<body>
<%
cfgp.parse("kaoqin.xml")
Properties pro = cfgp.getProps()
String stTime = pro.getProperty("morningbegin")
String edTime = pro.getProperty("morningend")
String afternoonbegin = pro.getProperty("afternoonbegin")
out.println(stTime+"\n"+edTime+"\n"+afternoonbegin)
System.out.println(stTime+"\n"+edTime+"\n"+afternoonbegin)
%>
</body>
</html>
CF1000gp抽奖活动的e礼包包含物品如下:AWM-TIGER(30天)、M1014(7天)、求生圈背包(7天)、黄金MK5(7天)、P228(7天)、牛仔帽(7天)、VZ58(7天)、GALIL(7天)、战斗护目镜(7天)、SG540(7天)、Remington870(7天)、3号背包(7天)、MP5K-A4、L85A1(7天)、2强赛比赛创建卡(7天)、巧克力手雷(7天)等。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)