java 获取xml节点_java如何读取xml节点元素值?

展开全部

java读取xml节点元素,主要使用java提供的解析xml的工具类32313133353236313431303231363533e59b9ee7ad9431333337623461SAXParserFactory,如下代码:package xml.xmlreader;

import 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文件

6

10

23:00

12:00

18:00

jsp获取各个节点的值:

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);

%>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值