handler

--------------------------------------------------

AbcSaxHandler handler = new AbcSaxHandler();

FileInputStream in;

in = new FileInputStream(new File("D:\\abc\\req.xml"));
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
SAXParser saxParser;
saxParser = factory.newSAXParser();
saxParser.parse(in, handler);


----------------------------------------------
public class QueryAlbumSaxHandler extends DefaultHandler
{
private AbcReq req;

private StringBuffer buff = new StringBuffer();
getAbcReq方法获得对象


开始一个Element

public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException
{
buff.setLength(0);//XML结点名称
if (localName.equalsIgnoreCase("AbcReq"))
{
req= new AbcReq ();
}
}

/**
* 得到值
* @param arg0 值的字符数组
* @param arg1 数组的offset
* @param arg2 数组的有效值长度
* @throws SAXException sax解析异常
*/
@Override
public void characters(char[] arg0, int arg1, int arg2)
throws SAXException
{
buff.append(arg0, arg1, arg2);
super.characters(arg0, arg1, arg2);
}

Element结束
public void endElement(String uri, String localName, String qName)
throws SAXException
{
if (localName.equalsIgnoreCase("studentId"))
{
req.setStudentId(buff.toString());
}
else if (localName.equalsIgnoreCase("age"))
{
req.setAge(Integer.parseInt(buff.toString()));
}
else if (localName.equalsIgnoreCase("sex"))
{
req.setSex(buff.toString());
}
}
}

-------------------------------------------------
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns1:AbcReq xmlns:ns1="http://wsi.abc.com">
<studentId>001</studentId>
<age>20</age>
</ns1:AbcReq >
</soapenv:Body>
</soapenv:Envelope>
----------------------------------------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值