Android uses Xmlpull method to parse XML example

Source: Internet
Author: User

XML content that needs to be parsed:

<app><dynamic><uploadPhoto>true</uploadPhoto><!--support for uploading images  false is not supported, true means support-- ><specifyFriends>true</specifyFriends><!--support @ friend  false is not supported, true means support  -->< sharwx>true</sharwx><!--  support sharing to  false is not supported, true means support  --><emoji>true</emoji ><!--  Support  for false representation is not supported, true for support  --><score>true</score><!--  false is not supported, true means support for  --></dynamic><doubt><photo>true</photo><!--whether a picture is supported   false is not supported, true represents support for--><supplementaryquestion>true</supplementaryquestion><!--support for issue replenishment   false is not supported, true represents support  for--><collect>true</collect><!--  Whether the collection is supported   by false represents not supported, True to support  --></doubt><courses><elective>true</elective><!--If there is an elective, false means no, True means Allow--></courses></app>


HTTP Encapsulation class:

Package Com.tudou.activity.work3;import Java.io.ioexception;import Java.io.inputstream;import Java.net.httpurlconnection;import Java.net.url;import Android.util.log;public class HttpUtil {public static InputStream getInputStream (String path) {try {URL url = new URL (path); HttpURLConnection conn = (httpurlconnection) url.openconnection (); Conn.setdoinput (true); Conn.setreadtimeout (3000); Conn.setconnecttimeout, Conn.setrequestmethod ("POST"), if (conn.getresponsecode () = =) {LOG.D ("MyLog", " Getresponsecode: "+ +); return Conn.getinputstream ();}} catch (IOException e) {e.printstacktrace ();} return null;}}

Parse the XML using Xmlpull, which is encapsulated directly in JSON:

Package Com.tudou.activity.work3;import Java.io.ioexception;import Java.io.inputstream;import java.util.List; Import Org.json.jsonexception;import Org.json.jsonobject;import Org.xmlpull.v1.xmlpullparser;import Org.xmlpull.v1.xmlpullparserexception;import Org.xmlpull.v1.xmlpullparserfactory;import Android.util.Log;public Class Xmlparser {public static string GetXML (InputStream InputStream, String inputencoding) {Jsonobject obj = new Jsonobje CT (); try {//LOG.D ("MyLog", "Xmlparser" + 200); Xmlpullparser parser = Xmlpullparserfactory.newinstance (). Newpullparser ();p arser.setinput (InputStream, inputencoding); int eventtype = Parser.geteventtype (); Jsonobject obj2 = new Jsonobject (); Jsonobject obj3 = new Jsonobject (); Jsonobject obj4 = new Jsonobject (); while (eventtype! = xmlpullparser.end_document) {//LOG.D ("MyLog", "EventType:" + Event Type), switch (eventtype) {case xmlpullparser.start_document:break;case xmlpullparser.start_tag:log.d ("MyLog", "name : "+ parser.getname ()), if (" Uploadphoto ". Equals(Parser.getname ())) {Obj2.put (Parser.getname (), Parser.nexttext ());} else if ("Specifyfriends". Equals (Parser.getname ())) {Obj2.put (Parser.getname (), Parser.nexttext ()),} else if ("sharwx ". Equals (Parser.getname ())) {Obj2.put (Parser.getname (), Parser.nexttext ());} else if ("emoji". Equals (Parser.getname ())) {Obj2.put (Parser.getname (), Parser.nexttext ()),} else if ("Score". Equals ( Parser.getname ())) {Obj2.put (Parser.getname (), Parser.nexttext ()),} else if ("Photo". Equals (Parser.getname ())) { Obj3.put (Parser.getname (), Parser.nexttext ());} else if ("Supplementaryquestion". Equals (Parser.getname ())) {Obj3.put (Parser.getname (), Parser.nexttext ());} else if ( "Collect". Equals (Parser.getname ())) {Obj3.put (Parser.getname (), Parser.nexttext ()),} else if ("Elective". Equals ( Parser.getname ())) {Obj4.put (Parser.getname (), Parser.nexttext ());} Break;case XmlPullParser.END_TAG:if ("app". Equals (Parser.getname ())) {Obj.put ("dynamic", obj2); Obj.put ("Doubt", OBJ3); Obj.put ("Courses", OBJ4);} Break;default:break;}EventType = Parser.next ();}} catch (Xmlpullparserexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} catch ( Jsonexception e) {e.printstacktrace ();} finally {try {inputstream.close ()} catch (IOException e) {e.printstacktrace ();}} return obj.tostring ();}}

Activity, where practiced hand is not considered efficient, is placed directly in the main thread:

Package Com.tudou.activity.work3;import Com.tudou.activity.r;import Android.app.activity;import android.os.Bundle; Import Android.util.log;public class HomeWork3 extends Activity {String path = "http://software.webtrn.cn/cms/res_base/ Software_webtrn_cn/privacy/config/tyxl/appconfiguration.xml "; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.LAYOUT.HOMEWORK3);} @Overrideprotected void OnStart () {Super.onstart (); String result = Xmlparser.getxml (Httputil.getinputstream (path), "Utf-8"); LOG.D ("MyLog", "Result:" + result);}}

By the way, a practiced hand interface is attached.

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:ad= "Http://schemas.android.com/apk/res/android "Ad:layout_width=" match_parent "ad:layout_height=" match_parent "ad:orientation=" vertical "> <linearlayo        UT ad:layout_width= "match_parent" ad:layout_height= "30DP" ad:background= "@drawable/titlebar_bg" ad:orientation= "Horizontal" > <imageview ad:layout_width= "wrap_content" Ad:layout_hei ght= "Wrap_content" ad:src= "@drawable/back_44_44"/> <linearlayout ad:layout_width= "MATC H_parent "ad:layout_height=" 30DP "ad:gravity=" center "> <textview AD : layout_width= "wrap_content" ad:layout_height= "wrap_content" ad:text= "local Course" a D:textsize= "20sp"/> </LinearLayout> </LinearLayout> <linearlayout ad:layout_width= " Match_parent "Ad:layout_height= "Wrap_content" ad:gravity= "center_vertical" ad:orientation= "Horizontal" > <imageview Ad:layout_width= "Wrap_content" ad:layout_height= "wrap_content" ad:src= "@drawable/image_default _195_130 "/> <linearlayout ad:layout_width=" match_parent "ad:layout_height=" Wrap_conten                T "ad:orientation=" vertical "> <textview ad:layout_width=" wrap_content "            ad:layout_height= "Wrap_content" ad:text= "reform and development of public organizations" ad:textsize= "18SP"/>                <linearlayout ad:layout_width= "match_parent" ad:layout_height= "Wrap_content"                    ad:orientation= "Horizontal" > <textview ad:layout_width= "wrap_content" ad:layout_height= "Wrap_content" ad:text= "Learning Progress:" Ad:textcolor= "#b6b6b 6 "Ad: textsize= "12sp"/> <progressbar style= "? Android:attr/progressbarstylehorizontal" Ad:layout_width= "100DP" ad:layout_height= "Wrap_content" Ad:second                    aryprogress= "ad:max=" "ad:progress="/> <textview Ad:layout_width= "Wrap_content" ad:layout_height= "Wrap_content" ad:t ext= "10%" ad:textcolor= "#b6b6b6" ad:textsize= "12sp"/> </linearlayo        ut> </LinearLayout> </LinearLayout> <linearlayout ad:layout_width= "Match_parent" ad:layout_height= "Wrap_content" ad:layout_margintop= "10DP" ad:gravity= "center" ad:orientation= "Horizonta L "> <button ad:layout_width=" 80dp "ad:layout_height=" 30DP "ad:background=" @ Drawable/coursedetail_startstudy_600_68 "ad:text=" offline cache "/> <button ad:layout_width=" 80DP "Ad:layout_ height= "30DP" ad:background= "@drawable/coursedetail_startstudy_600_68" ad:layout_marginleft= "20DP" a d:text= "Start learning"/> </LinearLayout> <textview ad:layout_width= "match_parent" ad:layout_height= "1DP" Ad:bac Kground= "#b6b6b6" ad:layout_margintop= "10DP"/></linearlayout>


Android uses Xmlpull method to parse XML example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.