解析一个XML文件: XML解析器

前端之家收集整理的这篇文章主要介绍了解析一个XML文件: XML解析器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

解析一个XML文件:XML解析器

public class MainActivity extends Activity implements OnClickListener {

private Button bt ;
private SmsBean sms ;
private List<SmsBean> smsList ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout. activity_main );
bt =(Button) findViewById(R.id. bt );
bt .setOnClickListener( this );
}

/**
* 解析xml文件方法
* @param v
*/
@Override
public void onClick(View v) {
try {
//创建pull解析器
XmlPullParser parser = Xml.newPullParser();
//pull解析器配置
FileInputStream fis= new FileInputStream(Environment.getExternalStorageDirectory().getPath()+ "/sms.xml" );
parser.setInput(fis, "UTF-8" );
//开始解析,获事件的类型
int eventType = parser.getEventType();
while (eventType!=XmlPullParser. END_DOCUMENT ){ //当事件类型不等于END_DOCUMENT时,继续解析
String tageName=parser.getName(); //得到解析节点的名称
switch (eventType) {
case XmlPullParser. START_TAG : //当解析的事件是开始标签
if ( "Sms" .equals(tageName)){
smsList = new ArrayList<SmsBean>();
}
else if ( "sms" .equals(tageName)){
sms = new SmsBean();
sms .setName(parser.getAttributeValue(0));
}
else if ( "address" .equals(tageName)){
sms .setStress(parser.nextText());
}
else if ( "content" .equals(tageName)){
sms .setContent(parser.nextText());
}
else if ( "tate" .equals(tageName)){
sms .setDate(Integer.parseInt(parser.nextText()));
}
break ;

case XmlPullParser. END_TAG :
if ( "Sms" .equals(tageName)){
smsList .add( sms );
sms = null ;
}
break ;
}
}
//继续往下解析
eventType=parser.next();
} catch (Exception e) {
e.printStackTrace();
}
sms .toString();
}
}




--------------------------------------------------------------------------------------------------

解析网络上xml文件和json文件:

public class PersonService {

public List<Person> getPersonsByXML() throws Exception {
URL url = new URL( "http://192.168.1.100:8080/05.Web/persons.xml" );
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3000);
int code = conn.getResponseCode();
if (code == 200) {
InputStream in = conn.getInputStream();
return parseXML(in);
}
throw new RuntimeException( "网络异常: " + code);
}

/**
* 解析网络上的xml文件
* @param in
* @return
* @throws Exception
*/
private List<Person> parseXML(InputStream in) throws Exception {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(in, "UTF-8" );
List<Person> list = new ArrayList<Person>();
Person p = null ;
for ( int type = parser.getEventType(); type != XmlPullParser. END_DOCUMENT ; type = parser.next()) {
if (type == XmlPullParser. START_TAG ) {
if ( "person" .equals(parser.getName())) {
p = new Person();
String id = parser.getAttributeValue(0);
p.setId(Integer.parseInt(id));
list.add(p);
} else if ( "name" .equals(parser.getName())) {
String name = parser.nextText();
p.setName(name);
} else if ( "age" .equals(parser.getName())) {
String age = parser.nextText();
p.setAge(Integer.parseInt(age));
}
}
}
return list;
}

/**
* 解析网络上的json文件
* @return
* @throws Exception
*/
public List<Person> getPersonsByJSON() throws Exception {
URL url = new URL( "http://192.168.1.100:8080/05.Web/persons.js" );
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3000);
int code = conn.getResponseCode();
if (code == 200) {
InputStream in = conn.getInputStream();
return parseJSON(in);
}
throw new RuntimeException( "网络异常: " + code);
}

private List<Person> parseJSON(InputStream in) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte [] buffer = new byte [8192];
int length;
while ((length = in.read(buffer)) != -1)
out.write(buffer,length);
in.close();
out.close();
String json = new String(out.toByteArray());
List<Person> list = new ArrayList<Person>();
JSONArray arr = new JSONArray(json); // 把String封装成JSON数组
for ( int i = 0; i < arr.length(); i++) { // 遍历JSON数组
JSONObject obj = arr.getJSONObject(i); // 获取每个JSON对象
Person p = new Person();
p.setId(obj.getInt( "id" )); // 从JSON对象中获取"id"字段的值
p.setName(obj.getString( "name" ));
p.setAge(obj.getInt( "age" ));
list.add(p);
}
return list;
}

}

猜你在找的XML相关文章