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