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

前端 未结 1 667
被撕碎了的回忆
被撕碎了的回忆 2021-01-16 10:34

I\'m new in Android (and in Java too) but now I\'m starting to work with web services.

So to understand better how to parse an XML, I started to try this tutorial:

1条回答
  •  清酒与你
    2021-01-16 10:59

    Problem solved!

    Bellow are the sites with useful information:

    • https://stackoverflow.com/a/4828765
    • https://stackoverflow.com/a/5709544
    • http://as400samplecode.blogspot.com/2011/11/android-parse-xml-file-example-using.html
    • http://www.anddev.org/novice-tutorials-f8/parsing-xml-from-the-net-using-the-saxparser-t353.html

    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:

      
      
          A4
          Black
          2005
      
      
          Civic
          Red
          2001
       
       
          Leon
          White
          2009
       
       
      

    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 list = new ArrayList(); 
    
    // ===========================================================
    // Getter & Setter
    // ===========================================================
    
    public ArrayList getParsedData() {
        return this.list;
    }
    
    // ===========================================================
    // Methods
    // ===========================================================
    @Override
    public void startDocument() throws SAXException {
        this.list = new ArrayList();
    }
    
    @Override
    public void endDocument() throws SAXException {
        // Nothing to do
    }
    
    /** Gets be called on opening tags like: 
     *  
     * Can provide attribute(s), when xml was like:
     * */
    @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: 
     *  */
    @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: 
     * characters */
    @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 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...

    0 讨论(0)
提交回复
热议问题