0

我正在按照此示例解析 xml示例,但是当我尝试解析 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;
        }

    } 
}
4

1 回答 1

0

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

请参阅此链接:

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

于 2012-08-28T19:11:30.257 回答