1

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);
}

2 Answers 2

1

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)

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

1 Comment

Thanx Mate for your help i'll look into your your links
0

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...

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.