Android:从字符串问题解析XML

前端之家收集整理的这篇文章主要介绍了Android:从字符串问题解析XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个自定义的contentHandler(称为 XMLHandler),我已经通过Google和StackOverflow访问了很多网站,详细介绍了如何进行设置.

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

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

如何访问解析的XML数据?

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

请帮忙.

  1. String result = fetchData(doesntmatter);
  2. Xml.parse(result,new XMLHandler());

解决方法

这是一个例子,我希望它能有助于理解“SAXParser”
  1. package test.example;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.net.URL;
  7.  
  8. import javax.xml.parsers.SAXParser;
  9. import javax.xml.parsers.SAXParserFactory;
  10.  
  11. import org.xml.sax.InputSource;
  12. import org.xml.sax.SAXException;
  13. import org.xml.sax.XMLReader;
  14. import org.xml.sax.helpers.DefaultHandler;
  15.  
  16. import android.app.Activity;
  17. import android.os.Bundle;
  18. import android.util.Log;
  19. import android.widget.TextView;
  20.  
  21. public class XMLParsingDemo extends Activity {
  22.  
  23. private final String MY_DEBUG_TAG = "WeatherForcaster";
  24.  
  25. /** Called when the activity is first created. */
  26. @Override
  27. public void onCreate(Bundle icicle) {
  28. super.onCreate(icicle);
  29.  
  30. /* Create a new TextView to display the parsingresult later. */
  31. TextView tv = new TextView(this);
  32.  
  33. try {
  34. /* Create a URL we want to load some xml-data from. */
  35.  
  36. DefaultHttpClient hc = new DefaultHttpClient();
  37. ResponseHandler <String> res = new BasicResponseHandler();
  38. HttpPost postMethod = new HttpPost("http://www.anddev.org/images/tut/basic/parsingxml/example.xml");
  39. String response=hc.execute(postMethod,res);
  40.  
  41. /* Get a SAXParser from the SAXPArserFactory. */
  42. SAXParserFactory spf = SAXParserFactory.newInstance();
  43. SAXParser sp = spf.newSAXParser();
  44.  
  45. /* Get the XMLReader of the SAXParser we created. */
  46. XMLReader xr = sp.getXMLReader();
  47. /* Create a new ContentHandler and apply it to the XML-Reader*/
  48. ExampleHandler myExampleHandler = new ExampleHandler();
  49. xr.setContentHandler(myExampleHandler);
  50.  
  51. /* Parse the xml-data from our URL. */
  52. InputSource inputSource = new InputSource();
  53. inputSource.setEncoding("UTF-8");
  54. inputSource.setCharacterStream(new StringReader(response));
  55.  
  56. /* Parse the xml-data from our URL. */
  57. xr.parse(inputSource);
  58. /* Parsing has finished. */
  59.  
  60. /* Our ExampleHandler now provides the parsed data to us. */
  61. ParsedExampleDataSet parsedExampleDataSet = myExampleHandler.getParsedData();
  62.  
  63.  
  64. /* Set the result to be displayed in our GUI. */
  65. tv.setText(response + "\n\n\n***************************************" + parsedExampleDataSet.toString());
  66.  
  67.  
  68.  
  69. } catch (Exception e) {
  70. /* Display any Error to the GUI. */
  71. tv.setText("Error: " + e.getMessage());
  72. Log.e(MY_DEBUG_TAG,"WeatherQueryError",e);
  73. }
  74. /* Display the TextView. */
  75. this.setContentView(tv);
  76. }
  77.  
  78. public class ExampleHandler extends DefaultHandler {
  79.  
  80. // ===========================================================
  81. // Fields
  82. // ===========================================================
  83.  
  84. private boolean in_outertag = false;
  85. private boolean in_innertag = false;
  86. private boolean in_mytag = false;
  87.  
  88. private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();
  89.  
  90. // ===========================================================
  91. // Getter & Setter
  92. // ===========================================================
  93.  
  94. public ParsedExampleDataSet getParsedData() {
  95. return this.myParsedExampleDataSet;
  96. }
  97.  
  98. // ===========================================================
  99. // Methods
  100. // ===========================================================
  101. @Override
  102. public void startDocument() throws SAXException {
  103. this.myParsedExampleDataSet = new ParsedExampleDataSet();
  104. }
  105.  
  106. @Override
  107. public void endDocument() throws SAXException {
  108. // Nothing to do
  109. }
  110.  
  111. /** Gets be called on opening tags like:
  112. * <tag>
  113. * Can provide attribute(s),when xml was like:
  114. * <tag attribute="attributeValue">*/
  115. @Override
  116. public void startElement(String uri,String localName,String qName,org.xml.sax.Attributes atts) throws SAXException {
  117. super.startElement(uri,localName,qName,atts);
  118. if (localName.equals("outertag")) {
  119. this.in_outertag = true;
  120. }
  121. else if (localName.equals("innertag")) {
  122. String attrValue = atts.getValue("sampleattribute");
  123. myParsedExampleDataSet.setExtractedString(attrValue);
  124. this.in_innertag = true;
  125. }
  126. else if (localName.equals("mytag")) {
  127. this.in_mytag = true;
  128. }
  129. else if (localName.equals("tagwithnumber")) {
  130. // Extract an Attribute
  131. String attrValue = atts.getValue("thenumber");
  132. int i = Integer.parseInt(attrValue);
  133. myParsedExampleDataSet.setExtractedInt(i);
  134. }
  135.  
  136. }
  137.  
  138.  
  139. /** Gets be called on closing tags like:
  140. * </tag> */
  141. @Override
  142. public void endElement(String namespaceURI,String qName)
  143. throws SAXException {
  144. if (localName.equals("outertag")) {
  145. this.in_outertag = false;
  146. }else if (localName.equals("innertag")) {
  147. this.in_innertag = false;
  148. }else if (localName.equals("mytag")) {
  149. this.in_mytag = false;
  150. }else if (localName.equals("tagwithnumber")) {
  151. // Nothing to do here
  152. }
  153. }
  154.  
  155.  
  156. /** Gets be called on the following structure:
  157. * <tag>characters</tag> */
  158. @Override
  159. public void characters(char ch[],int start,int length) {
  160. if(this.in_mytag){
  161. myParsedExampleDataSet.setExtractedString(new String(ch));
  162. }
  163. }
  164. }
  165.  
  166. public class ParsedExampleDataSet {
  167. private String extractedString = null;
  168. private int extractedInt = 0;
  169.  
  170. public String getExtractedString() {
  171. return extractedString;
  172. }
  173. public void setExtractedString(String extractedString) {
  174. this.extractedString = extractedString;
  175. }
  176.  
  177. public int getExtractedInt() {
  178. return extractedInt;
  179. }
  180. public void setExtractedInt(int extractedInt) {
  181. this.extractedInt = extractedInt;
  182. }
  183.  
  184. public String toString(){
  185. return "\n\n\nExtractedString = " + this.extractedString
  186. + "\n\n\nExtractedInt = " + this.extractedInt;
  187. }
  188.  
  189. }
  190.  
  191. }

猜你在找的Android相关文章