使用标记解析xml

时间:2012-08-28 19:09:01

标签: java android xml

我按照这个例子来解析xml example,但是当我尝试解析xml时,我得到了错误。 Xml是Reg Variacion =" 20" />

我需要显示TextView []; 20,20,20

这是我的代码:

MainActivity.java

public class MainActivity extends Activity {

    SitesList sitesList = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


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


        /** Create a new textview array to display the results */

        TextView registro[];
        TextView variacion[];

        try {

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

            /** Send URL to parse XML Tags */
            URL sourceUrl = new URL("http://www.bovalpo.com/cgi-local/xml_bcv.pl?URL=75");


            /** 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 */
        sitesList = MyXMLHandler.sitesList;


        /** Assign textview array lenght by arraylist size */
        registro = new TextView[sitesList.getRegistro().size()];
        variacion = new TextView[sitesList.getVariacion().size()];


        /** Set the result text in textview and add it to layout */
        for (int i = 0; i < sitesList.getRegistro().size(); i++) {

            registro[i] = new TextView(this);
            registro[i].setText(sitesList.getRegistro().get(i));
            variacion[i] = new TextView(this);
            variacion[i].setText(sitesList.getVariacion().get(i));


            layout.addView(registro[i]);
            //layout.addView(category[i]);
        }

        /** Set the layout view to display */
        setContentView(layout);
    }
}

MyXMLHandler.java

DefaultHandler {
    @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

    currentElement = true;
    if (localName.equals("root"))
    {
        /** Start */
        sitesList = new SitesList();

    } else if (localName.equals("registro")) {

        String attr = attributes.getValue("Variacion");
        String var = String.valueOf(attr);
        sitesList.setVariacion(var);
    }

    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {

    currentElement = false;

    /** set value */
        if (localName.equalsIgnoreCase("registro"))
            sitesList.setRegistro(currentValue);

    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {

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

    } 
}

1 个答案:

答案 0 :(得分:0)

尝试使用 DOM PARSER (文档对象模型)

请参阅此链接:

http://tutorials.jenkov.com/java-xml/dom.html