前端之家收集整理的这篇文章主要介绍了
xml转bean,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package com.llkj.xmltojson;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// XMLSerializer xmlSerializer = new XMLSerializer();
// JSON json = xmlSerializer.read(xml);
String content = "<data> <databean dataone=\"属性11\" datatwo=\"属性12\"><datathree>属性13</datathree><datafour>属性14</datafour></databean><databean dataone=\"属性21\" datatwo=\"属性22\"><datathree>属性23</datathree><datafour>属性24</datafour></databean><databean dataone=\"属性31\" datatwo=\"属性32\"><datathree>属性33</datathree><datafour>属性34</datafour></databean></data>";
// 获取XmlPullParser的实例
XmlPullParser xpp = getXmlParser(content,0);
List<DataBean> xmlParser = xmlParser(xpp);
for (int i = 0; i < xmlParser.size(); i++) {
Log.e("==",xmlParser.get(i).toString());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
/**
*
* @param data
* 传入的内容 或者路径
* @param type
* 0为内容即网络获取的数据 1为本地文件,路径
*/
public XmlPullParser getXmlParser(String data,int type) {
XmlPullParser xmlPullParser = null;
try {
XmlPullParserFactory pullParserFactory = XmlPullParserFactory
.newInstance();
// 获取XmlPullParser的实例
xmlPullParser = pullParserFactory.newPullParser();
// 设置输入流 xml文件
InputStream inputStream = null;
switch (type) {
case 0:
inputStream = new ByteArrayInputStream(data.getBytes());
break;
case 1:
inputStream = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(data);
break;
default:
break;
}
xmlPullParser.setInput(inputStream,"UTF-8");
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return xmlPullParser;
}
public static List<DataBean> xmlParser(XmlPullParser xmlPullParser) {
// 开始
List<DataBean> dbs = null;
DataBean db = null;
try {
int eventType = xmlPullParser.getEventType();
// 如果等于结束标签就退出循环 否则继续
while (eventType != XmlPullParser.END_DOCUMENT) {
String nodeName = xmlPullParser.getName();
switch (eventType) {
// 文档开始标签
case XmlPullParser.START_DOCUMENT:
dbs = new ArrayList<DataBean>();
break;
// 开始节点处理
case XmlPullParser.START_TAG:
// 判断如果其实节点为student 标签名
if ("databean".equals(nodeName)) {
// 实例化student对象
db = new DataBean();
// 设置Id属性 获取该标签下面的属性
String dataone = xmlPullParser.getAttributeValue(0);
String datatwo = xmlPullParser.getAttributeValue(1);
db.dataone = dataone;
db.datatwo = datatwo;
} else if ("datathree".equals(nodeName)) {
// 设置name
db.datathree = xmlPullParser.nextText();
} else if ("datafour".equals(nodeName)) {
// 设置sex
db.datafour = xmlPullParser.nextText();
}
break;
// 结束节点 处理
case XmlPullParser.END_TAG:
if ("databean".equals(nodeName)) {
dbs.add(db);
db = null;
}
break;
default:
break;
}
// 继续下一个节点
eventType = xmlPullParser.next();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dbs;
}
}
public class DataBean {
public String dataone;// 作为标签属性
public String datatwo;
public String datathree;
public String datafour;
@Override
public String toString() {
return "DataBean [dataone=" + dataone + ",datatwo=" + datatwo
+ ",datathree=" + datathree + ",datafour=" + datafour + "]";
}
}
<data>
<databean dataone="属性11" datatwo="属性12">
<datathree>属性13</datathree>
<datafour>属性14</datafour>
</databean>
<databean dataone="属性21" datatwo="属性22">
<datathree>属性23</datathree>
<datafour>属性24</datafour>
</databean>
<databean dataone="属性31" datatwo="属性32">
<datathree>属性33</datathree>
<datafour>属性34</datafour>
</databean>
</data>