需要解析的xml内容:
<app>
<dynamic>
<uploadPhoto>true</uploadPhoto>
<!-- 是不是支持上传图片 false代表不支持,true代表支持 -->
<specifyFriends>true</specifyFriends>
<!-- 是不是支持@好友 false代表不支持,true代表支持 -->
<sharWx>true</sharWx>
<!-- 是不是支持分享到微信 false代表不支持,true代表支持 -->
<emoji>true</emoji>
<!-- 是不是支持表情 false代表不支持,true代表支持 -->
<score>true</score>
<!-- 是不是支持积分 false代表不支持,true代表支持 -->
</dynamic>
<doubt>
<photo>true</photo>
<!-- 是不是支持图片 false代表不支持,true代表支持 -->
<supplementaryQuestion>true</supplementaryQuestion>
<!-- 是不是支持问题补充 false代表不支持,true代表支持 -->
<collect>true</collect>
<!-- 是不是支持收藏 false代表不支持,true代表支持 -->
</doubt>
<courses>
<elective>true</elective>
<!-- 是不是有选修课 false代表没有,true代表允许 -->
</courses>
</app>
http封装类:
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(3000);
conn.setRequestMethod("POST");
if (conn.getResponseCode() == 200) {
Log.d("mylog", "getResponseCode:" + 200);
return conn.getInputStream();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
使用xmlpull解析xml,直接用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 JSONObject();
try {
// Log.d("mylog", "XmlParser" + 200);
XmlPullParser parser = XmlPullParserFactory.newInstance()
.newPullParser();
parser.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:" + eventType);
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,此处练手不斟酌效力,直接放在主线程中了:
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";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homework3);
}
@Override
protected void onStart() {
super.onStart();
String result = XmlParser.getXML(HttpUtil.getInputStream(path), "utf⑻");
Log.d("mylog", "result:" + result);
}
}
顺便贴1个练手的界面。
<?xml version="1.0" encoding="utf⑻"?>
<LinearLayout xmlns:ad="http://schemas.android.com/apk/res/android"
ad:layout_width="match_parent"
ad:layout_height="match_parent"
ad:orientation="vertical" >
<LinearLayout
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_height="wrap_content"
ad:src="@drawable/back_44_44" />
<LinearLayout
ad:layout_width="match_parent"
ad:layout_height="30dp"
ad:gravity="center" >
<TextView
ad:layout_width="wrap_content"
ad:layout_height="wrap_content"
ad:text="本地课程"
ad: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_content"
ad:orientation="vertical" >
<TextView
ad:layout_width="wrap_content"
ad:layout_height="wrap_content"
ad:text="公共组织的变革与发展"
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="学习进度:"
ad:textColor="#b6b6b6"
ad:textSize="12sp" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
ad:layout_width="100dp"
ad:layout_height="wrap_content"
ad:secondaryProgress="50"
ad:max="100"
ad:progress="10" />
<TextView
ad:layout_width="wrap_content"
ad:layout_height="wrap_content"
ad:text="10%"
ad:textColor="#b6b6b6"
ad:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
ad:layout_width="match_parent"
ad:layout_height="wrap_content" ad:layout_marginTop="10dp"
ad:gravity="center"
ad:orientation="horizontal" >
<Button
ad:layout_width="80dp"
ad:layout_height="30dp"
ad:background="@drawable/coursedetail_startstudy_600_68"
ad:text="离线缓存" />
<Button
ad:layout_width="80dp"
ad:layout_height="30dp"
ad:background="@drawable/coursedetail_startstudy_600_68" ad:layout_marginLeft="20dp"
ad:text="开始学习" />
</LinearLayout>
<TextView ad:layout_width="match_parent" ad:layout_height="1dp" ad:background="#b6b6b6" ad:layout_marginTop="10dp"/>
</LinearLayout>