dom4j的sax解析!

<?xmlversion="1.0"encoding="UTF-8"?><RES><HEAD><CODE>000000</CODE></HEAD><BODY><JOBID... <?xml version="1.0" encoding="UTF-8"?>
<RES>
<HEAD>
<CODE>000000</CODE>
</HEAD>
<BODY>
<JOBID>03</JOBID>
<MAX_FILES>10</MAX_FILES>
<HEADRECORDS>
<RECORD><IMGID>9999999900124014</IMGID><NAME>046800012012052525000030111001</NAME><EXTNAME>TIF</EXTNAME></RECORD>
</HEADRECORDS>
<RECORDS>
<RECORD><IMGID>9999999600042815</IMGID><NAME>046800012012052525014030111369001</NAME><EXTNAME>TIF</EXTNAME><CATEID></CATEID></RECORD>
<RECORD><IMGID>9999999600042816</IMGID><NAME>046800012012052525014030111369002</NAME><EXTNAME>TIF</EXTNAME><CATEID></CATEID></RECORD>
<RECORD><IMGID>9999999600042817</IMGID><NAME>046800012012052525014030111370001</NAME><EXTNAME>TIF</EXTNAME><CATEID></CATEID></RECORD>
<RECORD><IMGID>9999999600042818</IMGID><NAME>046800012012052525014030111370002</NAME><EXTNAME>TIF</EXTNAME><CATEID></CATEID></RECORD>
<RECORD><IMGID>9999999600042819</IMGID><NAME>046800012012052525014030111371001</NAME><EXTNAME>TIF</EXTNAME><CATEID></CATEID></RECORD>
<RECORD><IMGID>9999999600042820</IMGID><NAME>046800012012052525014030111371002</NAME><EXTNAME>TIF</EXTNAME><CATEID></CATEID></RECORD>
</RECORDS>
</BODY>
</RES>

我要取所有IMGID的值!
我的解析方式是:
Iterator iter1 = rootElt.elementIterator("BODY");
while (iter1.hasNext()) {
org.dom4j.Element body = (org.dom4j.Element) iter1.next();
Iterator iter = body.elementIterator("RECORDS");
while (iter.hasNext()) {
org.dom4j.Element recordEle = (org.dom4j.Element) iter.next();
Iterator iters = recordEle.elementIterator("RECORD");
while (iters.hasNext()) {
org.dom4j.Element itemEle = (org.dom4j.Element) iters.next();
String IMGID = itemEle.elementTextTrim("IMGID");
System.out.println(IMGID);
list3.add(IMGID);
}
}
}

xml比较大,例中的xml只是部分片段!我怎么用sax解析呢?
展开
 我来答
zhiyinmmm
推荐于2016-08-31 · TA获得超过2603个赞
知道小有建树答主
回答量:646
采纳率:75%
帮助的人:89.8万
展开全部
package com.atu.xmltest;
import java.io.InputStream;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.ElementHandler;
import org.dom4j.ElementPath;
import org.dom4j.io.SAXReader;
public class MySaxHandler implements ElementHandler {

SAXReader reader;
public MySaxHandler() {
// test.xml文件跟类放在同一目录下
try {
InputStream is = MySaxHandler.class.getResourceAsStream("test.xml");
reader = new SAXReader();
reader.setDefaultHandler(this);
reader.read(is);
} catch (DocumentException e) {
e.printStackTrace();
}
}
@Override
public void onEnd(ElementPath ep) {
Element e = ep.getCurrent(); //获得当前节点
if(e.getName().equals("PARAM_TIMESTAMP"))
System.out.println("解析到时间:"+e.getText());
else if(e.getName().equals("CITYINFO")){
System.out.printf("解析到CITYINFO,属性值为:%s,%s,%s/n", e
.attributeValue("City"), e.attributeValue("Name"), e
.attributeValue("No"));
}
e.detach(); //记得从内存中移去
}
@Override
public void onStart(ElementPath ep) {
}

public static void main(String[] args){
new MySaxHandler();
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
as3_0
2012-07-05 · TA获得超过241个赞
知道答主
回答量:229
采纳率:0%
帮助的人:111万
展开全部
对于深层次寻找节点值可以使用xpath
./BODY/RECORD//IMGID

XPath path = doc.createXPath(xpath);
path.excute(doc);

代码可能有错误。只是一个思路。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2012-07-05
展开全部
sax 是一行一行读取的
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
林默ZRE
2015-10-19 · TA获得超过898个赞
知道小有建树答主
回答量:701
采纳率:63%
帮助的人:114万
展开全部
import java.io.File;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

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

public class SaxXML {
public static void main(String[] args) throws Exception {
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
SAXParser saxParse = saxFactory.newSAXParser();
saxParse.parse(new File("test.xml"), new SaxHandler());
}
}
class SaxHandler extends DefaultHandler{
private boolean isIMG = false;
public void endElement(String uri, String localName, String qName)
throws SAXException {
if("IMGID".equals(qName))
isIMG = true;
else
isIMG = false;
}
public void characters(char[] ch, int start, int length)
throws SAXException {
if(isIMG)
System.out.println(new String(ch,start,length));
}
}
test.xml里面是你的xml内容;
输出:
046800012012052525000030111001
046800012012052525014030111369001
046800012012052525014030111369002
046800012012052525014030111370001
046800012012052525014030111370002
046800012012052525014030111371001
046800012012052525014030111371002

来源:知道 haowujj | 来自:芝麻团 | 七级
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
haowujj
推荐于2016-12-01 · TA获得超过1207个赞
知道小有建树答主
回答量:177
采纳率:100%
帮助的人:264万
展开全部
import java.io.File;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

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

public class SaxXML {
public static void main(String[] args) throws Exception {
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
SAXParser saxParse = saxFactory.newSAXParser();
saxParse.parse(new File("test.xml"), new SaxHandler());
}
}
class SaxHandler extends DefaultHandler{
private boolean isIMG = false;
public void endElement(String uri, String localName, String qName)
throws SAXException {
if("IMGID".equals(qName))
isIMG = true;
else
isIMG = false;
}
public void characters(char[] ch, int start, int length)
throws SAXException {
if(isIMG)
System.out.println(new String(ch,start,length));
}
}
test.xml里面是你的xml内容;
输出:
046800012012052525000030111001
046800012012052525014030111369001
046800012012052525014030111369002
046800012012052525014030111370001
046800012012052525014030111370002
046800012012052525014030111371001
046800012012052525014030111371002
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式