Android:从字符串问题解析XML

Android:从字符串问题解析XML,第1张

概述我有一个自定义的contentHandler(称为 XMLHandler),我已经通过Google和StackOverflow访问了很多网站,详细介绍了如何进行设置. 我不明白的是如何使用它. Xml.parse(…,…)什么都不返回,因为它是一个void方法. 如何访问解析的XML数据? 我意识到这个问题可能是微不足道的,但我一直在寻找(字面上)小时,但没有找到解决方案. 请帮忙. String 我有一个自定义的contentHandler(称为 XMLHandler),我已经通过Google和StackOverflow访问了很多网站,详细介绍了如何进行设置.

我不明白的是如何使用它.

Xml.parse(…,…)什么都不返回,因为它是一个voID方法.

如何访问解析的XML数据?

我意识到这个问题可能是微不足道的,但我一直在寻找(字面上)小时,但没有找到解决方案.

请帮忙.

String result = fetchData(doesntmatter);Xml.parse(result,new XMLHandler());
解决方法 这是一个例子,我希望它能有助于理解“SAXParser”
package test.example;import java.io.BufferedReader;import java.io.inputStream;import java.io.inputStreamReader;import java.net.URL;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.inputSource;import org.xml.sax.SAXException;import org.xml.sax.XMLReader;import org.xml.sax.helpers.DefaultHandler;import androID.app.Activity;import androID.os.Bundle;import androID.util.Log;import androID.Widget.TextVIEw;public class XMLParsingDemo extends Activity {    private final String MY_DEBUG_TAG = "WeatherForcaster";    /** Called when the activity is first created. */    @OverrIDe    public voID onCreate(Bundle icicle) {        super.onCreate(icicle);        /* Create a new TextVIEw to display the parsingresult later. */        TextVIEw tv = new TextVIEw(this);        try {            /* Create a URL we want to load some xml-data from. */        DefaulthttpClIEnt hc = new DefaulthttpClIEnt();          ResponseHandler <String> res = new BasicResponseHandler();          httpPost postMethod = new httpPost("http://www.anddev.org/images/tut/basic/parsingxml/example.xml");        String response=hc.execute(postMethod,res);         /* Get a SAXParser from the SAXPArserFactory. */        SAXParserFactory spf = SAXParserFactory.newInstance();        SAXParser sp = spf.newSAXParser();        /* Get the XMLReader of the SAXParser we created. */        XMLReader xr = sp.getXMLReader();        /* Create a new ContentHandler and apply it to the XML-Reader*/         ExampleHandler myExampleHandler = new ExampleHandler();        xr.setContentHandler(myExampleHandler);        /* Parse the xml-data from our URL. */        inputSource inputSource = new inputSource();        inputSource.setEnCoding("UTF-8");        inputSource.setCharacterStream(new StringReader(response));        /* Parse the xml-data from our URL. */        xr.parse(inputSource);        /* Parsing has finished. */        /* Our ExampleHandler Now provIDes the parsed data to us. */        ParsedExampleDataSet parsedExampleDataSet = myExampleHandler.getParsedData();        /* Set the result to be displayed in our GUI. */        tv.setText(response + "\n\n\n***************************************" + parsedExampleDataSet.toString());        } catch (Exception e) {            /* display any Error to the GUI. */            tv.setText("Error: " + e.getMessage());            Log.e(MY_DEBUG_TAG,"WeatherqueryError",e);        }        /* display the TextVIEw. */        this.setContentVIEw(tv);    }    public class ExampleHandler extends DefaultHandler {        // ===========================================================        // FIElds        // ===========================================================        private boolean in_outertag = false;        private boolean in_innertag = false;        private boolean in_mytag = false;        private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();        // ===========================================================        // Getter & Setter        // ===========================================================        public ParsedExampleDataSet getParsedData() {            return this.myParsedExampleDataSet;        }        // ===========================================================        // Methods        // ===========================================================        @OverrIDe        public voID startdocument() throws SAXException {            this.myParsedExampleDataSet = new ParsedExampleDataSet();        }        @OverrIDe        public voID enddocument() throws SAXException {            // nothing to do        }        /** Gets be called on opening Tags like:          * <tag>          * Can provIDe attribute(s),when xml was like:         * <tag attribute="attributeValue">*/        @OverrIDe        public voID startElement(String uri,String localname,String qname,org.xml.sax.Attributes atts) throws SAXException {            super.startElement(uri,localname,qname,atts);            if (localname.equals("outertag")) {                this.in_outertag = true;            }            else if (localname.equals("innertag")) {                String attrValue = atts.getValue("sampleattribute");                myParsedExampleDataSet.setExtractedString(attrValue);                this.in_innertag = true;            }            else if (localname.equals("mytag")) {                this.in_mytag = true;            }            else if (localname.equals("tagwithnumber")) {                // Extract an Attribute                String attrValue = atts.getValue("thenumber");                int i = Integer.parseInt(attrValue);                myParsedExampleDataSet.setExtractedInt(i);            }        }        /** Gets be called on closing Tags like:          * </tag> */        @OverrIDe        public voID endElement(String namespaceURI,String qname)                throws SAXException {            if (localname.equals("outertag")) {                this.in_outertag = false;            }else if (localname.equals("innertag")) {                this.in_innertag = false;            }else if (localname.equals("mytag")) {                this.in_mytag = false;            }else if (localname.equals("tagwithnumber")) {                // nothing to do here            }        }               /** Gets be called on the following structure:          * <tag>characters</tag> */        @OverrIDe        public voID characters(char ch[],int start,int length) {            if(this.in_mytag){                myParsedExampleDataSet.setExtractedString(new String(ch));            }        }    }    public class ParsedExampleDataSet {        private String extractedString = null;        private int extractedInt = 0;        public String getExtractedString() {            return extractedString;        }        public voID setExtractedString(String extractedString) {            this.extractedString = extractedString;        }        public int getExtractedInt() {            return extractedInt;        }        public voID setExtractedInt(int extractedInt) {            this.extractedInt = extractedInt;        }        public String toString(){            return "\n\n\nExtractedString = " + this.extractedString                    + "\n\n\nExtractedInt = " + this.extractedInt;        }    }}
总结

以上是内存溢出为你收集整理的Android:从字符串问题解析XML全部内容,希望文章能够帮你解决Android:从字符串问题解析XML所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1134426.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存