xml文件解析之sax解析

本文介绍使用SAX解析XML文件的方法,通过四个示例展示了如何解析XML文档中的元素及属性,并将数据存储到对象中。

package org.itfuture.netoa.admin;

import java.io.FileInputStream;
import java.io.FileReader;

import org.xml.sax.*;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;//解析器
import org.xml.sax.helpers.DefaultHandler; //确省的拦截句柄
import org.xml.sax.helpers.XMLReaderFactory;
import java.io.InputStreamReader;



//该类来充当拦截器
public class SAXSampleO1 extends DefaultHandler {

public SAXSampleO1() {

}

//该方法负责拦截starDocument相应解析事件
public void startDocument() throws SAXException
{
System.out.println("[开始xml解析:]");
}

public void startElement(String namespaceUri, String ename, String qname, Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
System.out.println("[解析的元素为:]"+ename);
if(ename.equals("account")){
for(int i=0;i<attributes.getLength();i++)
{
String name=attributes.getLocalName(i);
String value=attributes.getValue(i);

System.out.println("该元素的属性:"+name+"---"+value);
}


// String type=attributes.getValue("type");
// String number=attributes.getValue("number");
// System.out.println("该元素的属性:type-"+type+":number-"+number);
}


}

public void characters(char[] chars, int start, int length) throws SAXException
{
String value= new String(chars,start,length);
System.out.println(value);
}


public void endElement(String namespaceUri, String ename, String qname) throws SAXException {
// TODO Auto-generated method stub
System.out.println("[解析的元素结束:]"+ename);
}



public void endDocument() throws SAXException {
System.out.println("[结束xml文档解析]");
}

/**
* @param args
*/
public static void main(String[] args)
{

try {
XMLReader parser=XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
parser.setContentHandler(new SAXSampleO1());;
//InputSource a=;
//parser.parse("D:/database.xml");
parser.parse(new InputSource(new FileReader("D:/database.xml")));
//parser.parse(new InputSource(new FileInputStream("D:/database.xml")));
}catch(SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}

}

}



package org.itfuture.netoa.admin;

import java.io.FileInputStream;
import java.io.FileReader;

import org.xml.sax.*;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;//解析器
import org.xml.sax.helpers.DefaultHandler; //确省的拦截句柄
import org.xml.sax.helpers.XMLReaderFactory;
import java.io.InputStreamReader;


//该类来充当拦截器
public class SAXSample02 extends DefaultHandler {
DBInfo info=null;
//StringBuffer sb=new StringBuffer();//用来存读到相应元素的值
String str="";
public SAXSample02()
{
info=new DBInfo();
}

//该方法负责拦截starDocument相应解析事件
public void startDocument() throws SAXException
{
System.out.println("[开始xml解析:]");
}

public void startElement(String namespaceUri, String ename, String qname, Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
System.out.println("[解析的元素为:]"+ename);
if(ename.equals("account"))
{
for(int i=0;i<attributes.getLength();i++)
{

String name=attributes.getLocalName(i);
String value=attributes.getValue(i);
if(name.equals("type"))
info.setType(value);
if(name.equals("number"))
info.setNumber(Integer.parseInt(value));
System.out.println("该元素的属性:"+name+"---"+value);
}
}
//sb.delete(0,sb.length());
str="";
}

public void characters(char[] chars, int start, int length) throws SAXException
{
String value= new String(chars,start,length);
System.out.println(value);
//sb.append(chars,start,length);
str=new String(chars,start,length);
}


public void endElement(String namespaceUri, String ename, String qname) throws SAXException
{
// TODO Auto-generated method stub
System.out.println("[解析的元素结束:]"+ename);
if(ename.equals("username"))
{
//info.setUsername(sb.toString());
info.setUsername(str);
}else if(ename.equals("password")){
//info.setPassword(sb.toString());
info.setPassword(str);
}else if(ename.equals("url")){
//info.setUrl(sb.toString());
info.setUrl(str);
}else if(ename.equals("driver")){
//info.setDriver(sb.toString());
info.setDriver(str);
}
}



public void endDocument() throws SAXException {
System.out.println("[结束xml文档解析]");
}

/**
* @param args
*/
public static void main(String[] args)
{

try {
SAXSample02 sample= new SAXSample02();
XMLReader parser=XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
parser.setContentHandler(sample);;
//InputSource a=;
//parser.parse("D:/database.xml");
parser.parse(new InputSource(new FileReader("D:/database.xml")));
//parser.parse(new InputSource(new FileInputStream("D:/database.xml")));
DBInfo info=sample.info;
System.out.println(info.getType()+"--"+info.getNumber()+"---"+info.getUsername()+"---"+info.getPassword()+"--"+info.getUrl()+"---"+info.getDriver());
}catch(SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}

}

}



package org.itfuture.netoa.admin;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;


public class SAXSample03 extends DefaultHandler{
List list=new ArrayList();
StringBuffer sb=new StringBuffer();
Customer cust=null;

public SAXSample03()
{
super();
// TODO Auto-generated constructor stub
}
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
}

public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
// TODO Auto-generated method stub
if(arg1.equals("customer"))
{
cust=new Customer();
list.add(cust);
}
sb.delete(0,sb.length());
}

public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
// TODO Auto-generated method stub
sb.append(arg0,arg1,arg2);
}

public void endElement(String arg0, String arg1, String arg2) throws SAXException {
// TODO Auto-generated method stub
if(arg1.equals("FirstName"))
{
cust.setFirstname(sb.toString());
}else if(arg2.equals("LastName")){
cust.setLasetname(sb.toString());
}else if(arg2.equals("CustId")){
cust.setCustid(sb.toString());
}
}

public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}


/**
* @param args
*/
public static void main(String[] args)
{
try {
XMLReader xr=XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
SAXSample03 sample=new SAXSample03();
xr.setContentHandler(sample);
xr.parse(new InputSource(new FileInputStream("D:/Example3.xml")));
List list=sample.list;
for(int i=0;i<list.size();i++)
{
Customer one= (Customer)list.get(i);
System.out.println(one.getCustid()+"---"+one.getFirstname()+"---"+one.getLasetname());
}
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}


}

}


package org.itfuture.netoa.admin;

import java.io.CharArrayWriter;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class SAXSample04 extends DefaultHandler {
private List custlist=new ArrayList();
private List orderlist=new ArrayList();
StringBuffer sb=new StringBuffer();
CharArrayWriter caw=new CharArrayWriter();
Customer cust=null;
OrderItem item=null;



public SAXSample04() {
super();
// TODO Auto-generated constructor stub
}

public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
}

public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
if(arg1.equals("Customer"))
{
cust=new Customer();
custlist.add(cust);
}else if(arg1.equals("OrderItem")){
item=new OrderItem();
item.setCustid(cust.getCustid());
orderlist.add(item);
}
//sb.delete(0,sb.length());
caw.reset();

}
public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
//sb.append(arg0,arg1,arg2);
caw.write(arg0,arg1,arg2);
}

public void endElement(String arg0, String arg1, String arg2) throws SAXException {
if(arg1.equals("FirstName")){
cust.setFirstname(caw.toString());
//cust.setFirstname(sb.toString());
}else if(arg1.equals("LastName")){
//cust.setLasetname(sb.toString());
cust.setLasetname(caw.toString());
}else if(arg1.equals("CustId")){
//cust.setCustid(sb.toString());
cust.setCustid(caw.toString());
}
if(arg1.equals("ProductCode")){
item.setProductcode(caw.toString());
//item.setProductcode(sb.toString());
}else if(arg1.equals("Quantity")){
item.setQuantity(Integer.parseInt(caw.toString().trim()));
//item.setQuantity(Integer.parseInt(sb.toString().trim()));
}else if(arg1.equals("Price")){
item.setPrice(Double.parseDouble(caw.toString().trim()));
//item.setPrice(Double.parseDouble(sb.toString().trim()));
}else if(arg1.equals("Description")){
item.setDescription(caw.toString());
//item.setDescription(sb.toString());

}


}

public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}

/**
* @param args
*/
public static void main(String[] args) throws Exception
{
SAXSample04 sample=new SAXSample04();
XMLReader parse=XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
parse.setContentHandler(sample);
parse.parse(new InputSource(new FileReader("D:/Example4.xml")));
List custlist=sample.custlist;
List orderlist=sample.orderlist;
for(int i=0;i<custlist.size();i++)
{
Customer cust=(Customer)custlist.get(i);
cust.print(System.out);
}
for(int i=0;i<orderlist.size();i++)
{
OrderItem item=(OrderItem)orderlist.get(i);
item.print(System.out);
}
}

}

MATLAB代码实现了一个基于多种智能优化算法优化RBF神经网络的回归预测模型,其核心是通过智能优化算法自动寻找最优的RBF扩展参数(spread),以提升预测精度。 1.主要功能 多算法优化RBF网络:使用多种智能优化算法优化RBF神经网络的核心参数spread。 回归预测:对输入特征进行回归预测,适用于连续值输出问题。 性能对比:对比不同优化算法在训练集和测试集上的预测性能,绘制适应度曲线、预测对比图、误差指标柱状图等。 2.算法步骤 数据准备:导入数据,随机打乱,划分训练集和测试集(默认7:3)。 数据归一化:使用mapminmax将输入和输出归一化到[0,1]区间。 标准RBF建模:使用固定spread=100建立基准RBF模型。 智能优化循环: 调用优化算法(从指定文件夹中读取算法文件)优化spread参数。 使用优化后的spread重新训练RBF网络。 评估预测结果,保存性能指标。 结果可视化: 绘制适应度曲线、训练集/测试集预测对比图。 绘制误差指标(MAE、RMSE、MAPE、MBE)柱状图。 十种智能优化算法分别是: GWO:灰狼算法 HBA:蜜獾算法 IAO:改进天鹰优化算法,改进①:Tent混沌映射种群初始化,改进②:自适应权重 MFO:飞蛾扑火算法 MPA:海洋捕食者算法 NGO:北方苍鹰算法 OOA:鱼鹰优化算法 RTH:红尾鹰算法 WOA:鲸鱼算法 ZOA:斑马算法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值