0

I'm getting an SOAP response like this:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
           <GetAllCodesAndLevels
              xmlns="http://www.myCodesWeb.com/">
              <GetAllCodesAndLevelsResult>[['1/12/2014 10:50:00 PM',1.5,2.2,55.9],['1/16/2014 11:52:00 AM',88.88,88.12,88.99],['1/18/2014 10:12:00 PM',12.3,12.4,12.2]]</GetAllCodesAndLevelsResult>
           </GetAllCodesAndLevels>
    </soap:Body>
</soap:Envelope>

I'm looking for a way to extract this array and build ArrayList<Code> from Code Class:

Each SOAP array element in the response - ['1/12/2014 10:50:00 PM',1.5,2.2,55.9] suppose to be a Code object.

 public class Code {

        public double code1;
        public double code2;
        public double code3;
        public int date;

public Code (double code1, double code2, double code3, int date) {
        super();
        this.code1= code1;
        this.code2= code2;
        this.code3= code3;
        this.date = date;
    }

    }

How can I do it? do I need to use ksoap2 library or maybe extract it to Json, or maybe use Volley library?

1 Answer 1

0

I would use SaxParser to extract the codes http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/. And the handler would look as follows:

private class Parser extends DefaultHandler
{
    String codes;
    String tag;

    public String getCodes()
    {
        return codes;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
    {
        super.startElement(uri, localName, qName, attributes);
        tag = qName;
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException
    {
        if (tag.equals("GetAllCodesAndLevelsResult"))
        {
            codes = new String(ch, start, length);
        }
        else
        {
            super.characters(ch, start, length);
        }
    }
}

Then use something similar to this solution to extract the separate codes. Java: Is there any simpler way to parse array elements from string?.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.