网络下载
package com.jj.day14_pull_sax_dom;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Created by jj on 2018/1/10.
*/
public class HttpUtils {
public static String getFromJson(String path){
try {
URL url=new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
if (conn.getResponseCode()==200){
InputStream is=conn.getInputStream();
ByteArrayOutputStream os=new ByteArrayOutputStream();
int len=0;
byte[] b=new byte[1024];
while ((len=is.read(b))!=-1){
os.write(b,0,len);
}
return os.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Activitypackage com.animee.day13.xml_test;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.animee.day13.R;
import java.io.IOException;
public class TestActivity extends AppCompatActivity {
private TextView tv;
public String url = "http://www.w3school.com.cn/example/xmle/note.xml";
public String TAG = "TestActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
tv = (TextView) findViewById(R.id.tv);
loadData(url);
}
private void loadData(final String url) {
new AsyncTask<Void,Void,String>(){
@Override
protected String doInBackground(Void... params) {
try {
return HttpUtils.getStringContent(url);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
if (s!=null&&!s.isEmpty()) {
Log.i(TAG, "onPostExecute: s==="+s);
// 解析xml文件
Note note = XMLTest.parseXML(s);
Log.i(TAG, "onPostExecute: note==="+note.toString());
tv.setText(note.toString());
}
}
}.execute();
}
}
实体类
package com.animee.day13.xml_test;
/**
* Created by Administrator on 2018/1/10.
* <to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
*/
public class Note {
private String to;
private String from;
private String heading;
private String body;
@Override
public String toString() {
return "Note{" +
"to='" + to + '\'' +
", from='" + from + '\'' +
", heading='" + heading + '\'' +
", body='" + body + '\'' +
'}';
}
public Note() {
}
public Note(String to, String from, String heading, String body) {
this.to = to;
this.from = from;
this.heading = heading;
this.body = body;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getHeading() {
return heading;
}
public void setHeading(String heading) {
this.heading = heading;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
解析XML
package com.animee.day13.xml_test;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.ByteArrayInputStream;
/**
* Created by Administrator on 2018/1/10.
*/
public class XMLTest {
public static Note parseXML(String s){
try {
// 1获取Pull解析工厂类对象
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
// 2.获取pull解析类对象
XmlPullParser pullParser = factory.newPullParser();
byte[]buf = s.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
// 3.设置要被解析的数据源
pullParser.setInput(bais,"utf-8");
// 4.获取标签的响应码
int code = pullParser.getEventType();
Note note = new Note();
// 5.循环判断,并解析每一个节点
while (code != XmlPullParser.END_DOCUMENT) {
//获取正在被解析的标签的名称
String name = pullParser.getName();
//判断正在被解析的是开始标签还是结束标签
switch (code) {
case XmlPullParser.START_TAG:
if (name.equals("to")) {
note.setTo(pullParser.nextText());
}else if (name.equals("from")) {
note.setFrom(pullParser.nextText());
}else if (name.equals("heading")) {
note.setHeading(pullParser.nextText());
}else if (name.equals("body")) {
note.setBody(pullParser.nextText());
}
break;
}
//移动指针到下一个标签
code = pullParser.next();
}
return note;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context="com.jj.day14_pull_sax_dom.MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"/>
</RelativeLayout>