raghav chopra
raghav chopra

Reputation: 837

XML parsing of the attributes

Hi Mates i am working on xml parsing of attribute my xml is

<CallLists> <CallList ID="1" Name="Name1" Desc="Ignore" CreatedOn="2/15/2011 1:48:30 PM" CreatedBy="def"> <CallList ID="2" Name="Name2" Desc="Agree" CreatedOn="3/8/2011 5:18:52 PM" CreatedBy="abc" > </CallLists>

I want to parse this xml so that i can get the attribute values Homework i have done is

After getting response from SERVER using soap //viewing xml and the traversing

viewXML(Responsedata.toString());



public void viewXML(String xmlStr) throws IOException {
    try{
        byte[] xmlByteArray=xmlStr.getBytes();
        ByteArrayInputStream xmlStream=new ByteArrayInputStream(xmlByteArray);
        InputStreamReader xmlReader=new InputStreamReader(xmlStream);
        XmlParser parser=new XmlParser(xmlReader);
        try{
            traverse(parser,"");
        }
        catch(Exception exc){
            exc.printStackTrace();
        }
        return;
    }
    catch(IOException e){
        return;
    }
}



    public void traverse(XmlParser parser,String indent) throws Exception{
    boolean leave=false;
    String sValue="";
    do{
        ParseEvent event=parser.read();
        ParseEvent pe;
        switch(event.getType()){
            case Xml.START_TAG:
                if (event.equals("calllists")){

                }
                if("ID".equals(event.getAttribute(0))){
                    pe=parser.read();
                    sValue=pe.getText()+"~~";

            }
                if("Name".equals(event.getAttribute(1))){
                    pe=parser.read();
                    sValue=sValue+pe.getText()+"~~";
                }

                if("Desc".equals(event.getAttributes())){
                    pe=parser.read();
                    sValue=sValue+pe.getText()+"~~";
                }

                if("CreatedOn".equals(event.getAttributes())){
                    pe=parser.read();
                    sValue=sValue+pe.getText()+"~~";
                }


                if("CreatedBy".equals(event.getName())){
                    pe=parser.read();
                    sValue=sValue+pe.getText()+"~~";
                }
                traverse(parser,"");
                break;
            case Xml.END_TAG:
                leave=true;
                break;
            case Xml.END_DOCUMENT:
                leave=true;
                break;
            case Xml.TEXT:
                break;
            case Xml.WHITESPACE:
                break;
            default:
        }
    }while(!leave);
}

Upvotes: 1

Views: 122

Answers (2)

hackp0int
hackp0int

Reputation: 4161

I would suggest you to use a SimpleXml, it's very quick and easy to install.

I used it over 3 applications and easy as 1.2.3

Here Enjoy...

Upvotes: 0

Michal
Michal

Reputation: 15669

You are making things way to complicated.

Use SAX instead, it is part of Android SDK here is a nice tutorial.

You also should focus on Default Handler(here). Take a closer look at these methods:

startElement(String uri, String localName, String qName, Attributes attributes)

endElement(String uri, String localName, String qName)

characters(char[] ch, int start, int length)

Upvotes: 1

Related Questions