一、pull解析方式
plante.xml
<CATALOG>
<PLANT>
<COMMON>Marsh Marigold</COMMON>
<BOTANICAL>Caltha palustris</BOTANICAL>
<ZONE>4</ZONE>
<LIGHT>Mostly Sunny</LIGHT>
<PRICE>$6.81</PRICE>
<AVAILABILITY>051799</AVAILABILITY>
</PLANT>
<PLANT>
<COMMON>Cowslip</COMMON>
<BOTANICAL>Caltha palustris</BOTANICAL>
<ZONE>4</ZONE>
<LIGHT>Mostly Shady</LIGHT>
<PRICE>$9.90</PRICE>
<AVAILABILITY>030699</AVAILABILITY>
</PLANT>
<PLANT>
<COMMON>Dutchman's-Breeches</COMMON>
<BOTANICAL>Dicentra cucullaria</BOTANICAL>
<ZONE>3</ZONE>
<LIGHT>Mostly Shady</LIGHT>
<PRICE>$6.44</PRICE>
<AVAILABILITY>012099</AVAILABILITY>
</PLANT>
<PLANT>
<COMMON>Ginger, Wild</COMMON>
<BOTANICAL>Asarum canadense</BOTANICAL>
<ZONE>3</ZONE>
<LIGHT>Mostly Shady</LIGHT>
<PRICE>$9.03</PRICE>
<AVAILABILITY>041899</AVAILABILITY>
</PLANT>
<PLANT>
<COMMON>Hepatica</COMMON>
<BOTANICAL>Hepatica americana</BOTANICAL>
<ZONE>4</ZONE>
<LIGHT>Mostly Shady</LIGHT>
<PRICE>$4.45</PRICE>
<AVAILABILITY>012699</AVAILABILITY>
</PLANT>
<PLANT>
<COMMON>Liverleaf</COMMON>
<BOTANICAL>Hepatica americana</BOTANICAL>
<ZONE>4</ZONE>
<LIGHT>Mostly Shady</LIGHT>
<PRICE>$3.99</PRICE>
<AVAILABILITY>010299</AVAILABILITY>
</PLANT>
<PLANT>
<COMMON>Trillium</COMMON>
<BOTANICAL>Trillium grandiflorum</BOTANICAL>
<ZONE>5</ZONE>
<LIGHT>Sun or Shade</LIGHT>
<PRICE>$3.90</PRICE>
<AVAILABILITY>042999</AVAILABILITY>
</PLANT>
<PLANT>
<COMMON>Wake Robin</COMMON>
<BOTANICAL>Trillium grandiflorum</BOTANICAL>
<ZONE>5</ZONE>
<LIGHT>Sun or Shade</LIGHT>
<PRICE>$3.20</PRICE>
<AVAILABILITY>022199</AVAILABILITY>
</PLANT>
</CATALOG>
TestPull.java
public class Test {
public static void main(String[] args) {
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(new FileInputStream("res/plant.xml"),"UTF-8");
int type;
Catalog catalog = new Catalog();
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT){
switch (type){
case XmlPullParser.START_DOCUMENT:
System.out.println("开始一个文档");
break;
case XmlPullParser.START_TAG:
switch (parser.getName()){
case "PLANT":
catalog.getPlants().add(new Plant());
break;
case "COMMON":
catalog.getPlants().getLast().setCommon(parser.nextText().trim());
break;
case "BOTANICAL":
catalog.getPlants().getLast().setBotanical(parser.nextText().trim());
break;
case "ZONE":
catalog.getPlants().getLast().setZone(parser.nextText().trim());
break;
case "LIGHT":
catalog.getPlants().getLast().setLight(parser.nextText().trim());
break;
case "PRICE":
catalog.getPlants().getLast().setPrice(parser.nextText().trim());
break;
case "AVAILABILITY":
catalog.getPlants().getLast().setAvailability(parser.nextText().trim());
break;
}
break;
case XmlPullParser.END_TAG:
System.out.println("标签结束");
break;
case XmlPullParser.END_DOCUMENT:
System.out.println("文档结束");
break;
}
}
for (int i = 0; i < catalog.getPlants().size(); i++) {
System.out.println(catalog.getPlants().get(i));
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Catlog.java
public class Catalog {
private LinkedList<Plant> plants;
public LinkedList<Plant> getPlants() {
if (plants == null)
plants = new LinkedList<>();
return plants;
}
public void setPlants(LinkedList<Plant> plants) {
this.plants = plants;
}
}
Plant.java
public class Plant {
private String Common;
private String Botanical;
private String Zone;
private String Light;
private String Price;
private String Availability;
public String getCommon() {
return Common;
}
public void setCommon(String common) {
Common = common;
}
public String getBotanical() {
return Botanical;
}
public void setBotanical(String botanical) {
Botanical = botanical;
}
public String getZone() {
return Zone;
}
public void setZone(String zone) {
Zone = zone;
}
public String getLight() {
return Light;
}
public void setLight(String light) {
Light = light;
}
public String getPrice() {
return Price;
}
public void setPrice(String price) {
Price = price;
}
public String getAvailability() {
return Availability;
}
public void setAvailability(String availability) {
Availability = availability;
}
@Override
public String toString() {
return "Plant{" +
"Common='" + Common + '\'' +
", Botanical='" + Botanical + '\'' +
", Zone=" + Zone +
", Light='" + Light + '\'' +
", Price='" + Price + '\'' +
", Availability='" + Availability + '\'' +
'}';
}
}