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解析呢? 展开
<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解析呢? 展开
5个回答
展开全部
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();
}
}
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();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
对于深层次寻找节点值可以使用xpath
./BODY/RECORD//IMGID
XPath path = doc.createXPath(xpath);
path.excute(doc);
代码可能有错误。只是一个思路。
./BODY/RECORD//IMGID
XPath path = doc.createXPath(xpath);
path.excute(doc);
代码可能有错误。只是一个思路。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-07-05
展开全部
sax 是一行一行读取的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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 | 来自:芝麻团 | 七级
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 | 来自:芝麻团 | 七级
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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
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
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询