XML Parser returns only first item

269 Views Asked by At

This is my xml content on a url over the internet:

<skus>
    <item>
        <id>r_1</id>
        <size>10</size>
    </item>
    <item>
        <id>c_1</id>
        <size>10</size>
    </item>
    <item>
        <id>d_1</id>
        <size>10</size>
    </item>
    <item>
        <id>e_1</id>
        <size>10</size>
    </item>
    <item>
        <id>f_1</id>
        <size>10</size>
    </item>
</skus>

This is SAXXMLParser Class:

public class SAXXMLParser {

    public static List<XMLSetAdd> parse(InputStream is) {
        List<XMLSetAdd> setAdds = null;
        try {
            // create a XMLReader from SAXParser
            XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser()
                    .getXMLReader();
            // create a SAXXMLHandler
            SAXXMLHANDLER saxHandler = new SAXXMLHANDLER();
            // store handler in XMLReader
            xmlReader.setContentHandler(saxHandler);
            // the process starts
            xmlReader.parse(new InputSource(is));
            // getting the list`
            setAdds = saxHandler.getIds();

        } catch (Exception ex) {
            Log.d("XML", "SAXXMLParser: parse() failed");
            ex.printStackTrace();
        }

        // return The list
        return setAdds;
    }

}

This is my XMLSetAdd Class:

public class XMLSetAdd {

    public String getId() {
        return Id;
    }

    public void setId(String Id) {
        this.Id = Id;
    }

    public String getSize(){
        return Size;
    }

    public void setSize(String Size){
        this.Size = Size;
    }

    private String Id;
    private String Size;
}

And This is my SAXXMLHANDLER:

public class SAXXMLHANDLER extends DefaultHandler {

    private List<XMLSetAdd> setAdds;
    private String tempVal;
    // to maintain context
    private XMLSetAdd setAdd;

    public SAXXMLHANDLER() {
        setAdds = new ArrayList<XMLSetAdd>();
    }

    public List<XMLSetAdd> getIds() {
        return setAdds;
    }

    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        tempVal = "";
        if (qName.equals("skus")){

        }else if (qName.equals("item")){
            setAdd = new XMLSetAdd();
        }
    }

    public void characters(char[] ch, int start, int length)
            throws SAXException {
        tempVal = new String(ch, start, length);
    }

    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        if (qName.equals("item")) {
            setAdds.add(setAdd);
        } else if (qName.equals("id")) {
            setAdd.setId(tempVal);
        } else if (qName.equals("size")){
            setAdd.setSize(tempVal);
        } 
    }

}

And finally in terms of using it:

I have an async task with the purpose of reading xml content and filling it within a list. the result I get when I run this code is "ONE ROW" which is r_1. whereas it should return 5 results. ( I Also don't receive the size value)

I can't figure out which part of my code is wrong!

1

There are 1 best solutions below

0
On

This is how you can do it

    package com.teste;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

public class SAXXMLParser {

    public static List<XMLSetAdd> parse(InputStream is) {
        List<XMLSetAdd> setAdds = null;
        try {
            // create a XMLReader from SAXParser
            XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser()
                    .getXMLReader();
            // create a SAXXMLHandler
            SAXXMLHANDLER saxHandler = new SAXXMLHANDLER();
            // store handler in XMLReader
            xmlReader.setContentHandler(saxHandler);
            // the process starts
            xmlReader.parse(new InputSource(is));
            // getting the list`
            setAdds = saxHandler.getIds();
            System.out.println(setAdds.size());
            for(int i=0;i<setAdds.size();i++)
            {
            System.out.println(setAdds.get(i).getId());
            System.out.println(setAdds.get(i).getSize());
            }
        } catch (Exception ex) {
            System.out.println("XML SAXXMLParser: parse() failed");
            ex.printStackTrace();
        }

        // return The list
        return setAdds;
    }

    public static void main(String[] args) throws FileNotFoundException {
        SAXXMLParser parser = new SAXXMLParser();

        InputStream inputStream = new FileInputStream(
                "D:\\StackOverFlow\\JAXBTest\\src\\skus.xml");
        parser.parse(inputStream);
        System.out.println("parsed");

    }
}

And

    package com.teste;

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 SAXXMLHANDLER extends DefaultHandler {

    private List<XMLSetAdd> setAdds;
    private String tempVal;
    // to maintain context
    private XMLSetAdd setAdd;

    public SAXXMLHANDLER() {
        setAdds = new ArrayList<XMLSetAdd>();
    }

    public List<XMLSetAdd> getIds() {
        return setAdds;
    }

    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        tempVal = "";
        if (qName.equals("skus")){

        }else if (qName.equals("item")){
            if(setAdd!=null){
            setAdds.add(setAdd);
            }
            setAdd = new XMLSetAdd();
        }
    }

    public void characters(char[] ch, int start, int length)
            throws SAXException {
        tempVal = new String(ch, start, length);
    }

    public void endElement(String uri, String localName, String qName)
            throws SAXException {
         if (qName.equals("id")) {
            setAdd.setId(tempVal);
        } else if (qName.equals("size")){
            setAdd.setSize(tempVal);
        } 

   }

}

There is no change in another class.. I have done it in Java remove main method and run it will work