package sax;
import java.util.List;
import test.TestMain;
public class ListingModel {
private String id;
private String name;
private List<TextModel> textModelList;
private List<CouponModel> couponModelList;
...
}
------------
package sax;
public class TextModel {
private String link;
private String title;
private String type;
...
}
-------------
package sax;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class InstallHandler extends DefaultHandler {
int point = 0;
ListingModel listingModel = new ListingModel();
List<TextModel> textModelList = new ArrayList<TextModel>();
TextModel textModel = new TextModel();
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Complete method stub for startElement
point = 0;
if (qName.equals("id")) {
point = SaxConstants.pointID;
}
if (qName.equals("name")) {
point = SaxConstants.pointName;
}
content point
if (qName.equals("text")) {
point = SaxConstants.pointText;
if (attributes != null && attributes.getLength() != 0) {
for (int i = 0; i < attributes.getLength(); i++) {
textModel.setType(attributes.getValue(i));
}
}
}
if (qName.equals("link")) {
point = SaxConstants.pointTextLink;
}
if (qName.equals("title")) {
point = SaxConstants.pointTextTitle;
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equals("text")) {
textModelList.add(textModel);
listingModel.setTextModelList(textModelList);
textModel = new TextModel();
}
point = 0;
}
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Complete method stub for characters
String pointName = new String(ch, start, length);
switch (point) {
case SaxConstants.pointID:
listingModel.setId(pointName);
break;
case SaxConstants.pointName:
listingModel.setName(pointName);
break;
case SaxConstants.pointTextLink:
textModel.setLink(pointName);
break;
case SaxConstants.pointTextTitle:
textModel.setTitle(pointName);
break;
}
}
public ListingModel getListingModel() {
return listingModel;
}
public void setListingModel(ListingModel listingModel) {
this.listingModel = listingModel;
}
}
------------
注意:
1.sax可以解决dom解析大型文件的困难
2.sax顺序解析,dom一次性放入内存
import java.util.List;
import test.TestMain;
public class ListingModel {
private String id;
private String name;
private List<TextModel> textModelList;
private List<CouponModel> couponModelList;
...
}
------------
package sax;
public class TextModel {
private String link;
private String title;
private String type;
...
}
-------------
package sax;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class InstallHandler extends DefaultHandler {
int point = 0;
ListingModel listingModel = new ListingModel();
List<TextModel> textModelList = new ArrayList<TextModel>();
TextModel textModel = new TextModel();
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Complete method stub for startElement
point = 0;
if (qName.equals("id")) {
point = SaxConstants.pointID;
}
if (qName.equals("name")) {
point = SaxConstants.pointName;
}
content point
if (qName.equals("text")) {
point = SaxConstants.pointText;
if (attributes != null && attributes.getLength() != 0) {
for (int i = 0; i < attributes.getLength(); i++) {
textModel.setType(attributes.getValue(i));
}
}
}
if (qName.equals("link")) {
point = SaxConstants.pointTextLink;
}
if (qName.equals("title")) {
point = SaxConstants.pointTextTitle;
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equals("text")) {
textModelList.add(textModel);
listingModel.setTextModelList(textModelList);
textModel = new TextModel();
}
point = 0;
}
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Complete method stub for characters
String pointName = new String(ch, start, length);
switch (point) {
case SaxConstants.pointID:
listingModel.setId(pointName);
break;
case SaxConstants.pointName:
listingModel.setName(pointName);
break;
case SaxConstants.pointTextLink:
textModel.setLink(pointName);
break;
case SaxConstants.pointTextTitle:
textModel.setTitle(pointName);
break;
}
}
public ListingModel getListingModel() {
return listingModel;
}
public void setListingModel(ListingModel listingModel) {
this.listingModel = listingModel;
}
}
------------
注意:
1.sax可以解决dom解析大型文件的困难
2.sax顺序解析,dom一次性放入内存