簡體   English   中英

用標簽解析xml

[英]parse xml with tag

我正在按照這個示例來解析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;
        }

    } 
}

嘗試使用DOM PARSER (文檔對象模型)

看到這個鏈接:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM