Android and .NET Webservice - parsing the returned xml

末鹿安然 提交于 2019-11-29 08:57:37

So after much research I was able to get this parsed correctly so I figured I would answer my own question. I also had to change the structure of my program to get what I needed...or at least understand what I was doing before I started to implement it in the code above. So the 3 classes that are below are the ones I used to get what I needed...for now.

public class MyXMLHandler extends DefaultHandler {

    Boolean currentElement = false;
    String currentValue = null;
    public static ProgramList programList = null;

    public static ProgramList getProgramList() {
        return programList;
    }

    public static void setProgramList(ProgramList programList) {
        MyXMLHandler.programList = programList;
    }

    //called when tag starts ( ex:- <tblPrograms>diffgr:id="tblPrograms1" msdata:rowOrder="0"</tblPrograms>  -- <tblPrograms> )
    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {

        currentElement = true;

        //set up for hierarchy scan to place data within category
        if (localName.equals("DataSet"))
        {
            /** Start */ 
            programList = new ProgramList();
        } else if (localName.equals("tblPrograms")){//"NewDataSet" - DOES NOT WORK - F.C.
            /** Get attribute value */
            String attr = attributes.getValue(0);
            String attr2 = attributes.getValue(1);
            programList.setTable(attr);
            programList.setRowOrder(attr2);
        }

    }

    //called when tag closing ( ex:- <Program>Ancillary</Program>  -- </Program> )
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {

        currentElement = false;

        /** set value */ 
        if (localName.equalsIgnoreCase("Program"))
            programList.setProgram(currentValue);
        //else if (localName.equalsIgnoreCase("tblPrograms"))
            //programList.setWebsite(currentValue);

    }

    //called to get tag characters ( ex:- <Program>Ancillary</Program> -- to get Ancillary character )
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {

        if (currentElement) {
            currentValue = new String(ch, start, length);
            currentElement = false;
        }

    }

}

Then I have my getter and setter methods for the variables within my program list.

//contains getter and setter method for variables 
public class ProgramList {

    //variables
    private ArrayList<String> program = new ArrayList<String>();
    private ArrayList<String> rowOrder = new ArrayList<String>();
    private ArrayList<String> table = new ArrayList<String>();


    //in Setter method default it will return arraylist change that to add

    public ArrayList<String> getProgram() {
        return program;
    }

    public void setProgram(String program) {
        this.program.add(program);
    }

    public ArrayList<String> getRowOrder() {
        return rowOrder;
    }

    public void setRowOrder(String rowOrder) {
        this.rowOrder.add(rowOrder);
    }

    public ArrayList<String> getTable() {
        return table;
    }

    public void setTable(String table) {
        this.table.add(table);
    }

}

Finally main

public class XMLParsingExample extends Activity {

    //create object For SiteList class
    ProgramList programList = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //create a new layout to display the view 
        //LinearLayout layout = new LinearLayout(this);
        //layout.setOrientation(1);

        LinearLayout ll = (LinearLayout)findViewById(R.id.LL01);

        //create a new textview array to display the results 
        //TextView name[];
        //TextView website[];
        //TextView category[];


        try {

            //handle XML
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();

            //URL to parse XML Tags 
            URL sourceUrl = new URL(
                    "http://www.ces.org/android/android.asmx/SelectPrograms");

            //Create handler to handle XML Tags ( extends DefaultHandler )
            MyXMLHandler myXMLHandler = new MyXMLHandler();
            xr.setContentHandler(myXMLHandler);
            xr.parse(new InputSource(sourceUrl.openStream()));

        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }

        //get result from MyXMLHandler SitlesList Object 
        programList = MyXMLHandler.programList;

        //assign a textview array length by arraylist size 
        TextView name[] = new TextView[programList.getProgram().size()];
        TextView website[] = new TextView[programList.getProgram().size()];
        TextView category[] = new TextView[programList.getProgram().size()];




        //set the result text in textview and add it to layout 
        for (int i = 0; i < programList.getProgram().size(); i++) {
            name[i] = new TextView(this);
            name[i].setText("Program = "+programList.getProgram().get(i));
            name[i].setBackgroundColor(Color.BLUE);

            website[i] = new TextView(this);
            website[i].setText("Row Order = "+programList.getRowOrder().get(i));

            category[i] = new TextView(this);
            category[i].setText("Program Table = "+programList.getTable().get(i));

            //layout.addView(name[i]);
            //layout.addView(website[i]);
            //layout.addView(category[i]);
            ll.addView(name[i]);
            ll.addView(website[i]);
            ll.addView(category[i]);


        }

        //set the layout view to display 
        //setContentView(layout);
        setContentView(ll);

    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!