Using SAXparser to get info to more than one element (Android)

与世无争的帅哥 提交于 2019-12-01 12:33:56
amp

Problem solved!

Bellow are the sites with useful information:

My initial problem was how I should define the class for the data which I wanted to extract from XML. After I figured out how I should do this (reviewing the basic concepts of JAVA programming), I changed the type of data returned by the ExampleHandler to an ArrayList<"class of the data you want return">.

I give below an example:

  • Example of a XML you want to parse:

    <outertag>
    <cartag type="Audi">
        <itemtag name="model">A4</itemtag>
        <itemtag name="color">Black</itemtag>
        <itemtag name="year">2005</itemtag>
    </cartag>
    <cartag type="Honda">
        <itemtag name="model">Civic</itemtag>
        <itemtag name="color">Red</itemtag>
        <itemtag name="year">2001</itemtag>
     </cartag>
     <cartag type="Seat">
        <itemtag name="model">Leon</itemtag>
        <itemtag name="color">White</itemtag>
        <itemtag name="year">2009</itemtag>
     </cartag>
     </outertag>
    

So here you should define a class "car" with proper attributes (String type, model, color, year;), setters and getters...

  • My suggestion of ExampleHandler for this XML is:

public class ExampleHandler extends DefaultHandler{

// ===========================================================
// Fields
// ===========================================================

private int numberOfItems=3;    
private boolean in_outertag = false;
private boolean in_cartag = false;
private boolean[] in_itemtag = new boolean[numberOfItems];

Car newCar = new Car(); 

private ArrayList<Car> list = new ArrayList<Car>(); 

// ===========================================================
// Getter & Setter
// ===========================================================

public ArrayList<Car> getParsedData() {
    return this.list;
}

// ===========================================================
// Methods
// ===========================================================
@Override
public void startDocument() throws SAXException {
    this.list = new ArrayList<Car>();
}

@Override
public void endDocument() throws SAXException {
    // Nothing to do
}

/** Gets be called on opening tags like: 
 * <tag> 
 * Can provide attribute(s), when xml was like:
 * <tag attribute="attributeValue">*/
@Override
public void startElement(String namespaceURI, String localName,
        String qName, Attributes atts) throws SAXException {
    if (localName.equals("outertag")) {
        this.in_outertag = true;
    }else if (localName.equals("cartag")) {
        this.in_cartag = true;
        newCar.setType(atts.getValue("type"));  //setType(...) is the setter defined in car class
    }else if (localName.equals("itemtag")) {
        if((atts.getValue("name")).equals("model")){
            this.in_itemtag[0] = true;
        }else if((atts.getValue("name")).equals("color")){
            this.in_itemtag[1] = true;
        }else if((atts.getValue("name")).equals("year")){
            this.in_itemtag[2] = true;
        }
    }
}

/** Gets be called on closing tags like: 
 * </tag> */
@Override
public void endElement(String namespaceURI, String localName, String qName)
        throws SAXException {
    if (localName.equals("outertag")) {
        this.in_outertag = false;
    }else if (localName.equals("cartag")) {
        this.in_cartag = false;
        Car carTemp = new Car();
        carTemp.copy(newCar, carTemp);  //this method is defined on car class, and is used to copy the
                                        //properties of the car to another Object car to be added to the list
        list.add(carTemp);
    }else if (localName.equals("itemtag")){
        if(in_itemtag[0]){
            this.in_itemtag[0] = false;
        }else if(in_itemtag[1]){
            this.in_itemtag[1] = false;
        }else if(in_itemtag[2]){
            this.in_itemtag[2] = false;
        }
    }
}

/** Gets be called on the following structure: 
 * <tag>characters</tag> */
@Override
public void characters(char ch[], int start, int length) {

    if(in_itemtag[0]){
        newCar.setModel(new String(ch, start, length));
    }else if(in_itemtag[1]){
        newCar.setColor(new String(ch, start, length));
    }else if(in_itemtag[2]){
        newCar.setYear(new String(ch, start, length));
    }
}

}

After this, you can get the parsed data in the Activity using:

...
ArrayList<Car> ParsedData = myExampleHandler.getParsedData();
...

I hope this helps someone.

Attention: I don't have tested exactly like this, but is almost the same of my solution so it should work...

And sorry for my bad English...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!