一、Bean类
public class BubbleEntry {
private float size = 0f;
private String description = "";//描述
private String id = "";//数据的ID
private float x;
private float y;
public BubbleEntry() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public float getSize() {
return mSize;
}
public void setSize(float size) {
this.mSize = size;
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
}
二、Xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<data id="1">
<x>1</x>
<y>10</y>
<size>10</size>
<description>描述1</description>
</data>
<data id="2">
<x>2</x>
<y>20</y>
<size>20</size>
<description>描述2</description>
</data>
<data id="3">
<x>3</x>
<y>30</y>
<size>30</size>
<description>描述3</description>
</data>
<data id="4">
<x>4</x>
<y>40</y>
<size>40</size>
<description>描述4</description>
</data>
<data id="5">
<x>5</x>
<y>50</y>
<size>50</size>
<description>描述5</description>
</data>
<data id="6">
<x>6</x>
<y>60</y>
<size>60</size>
<description>描述6</description>
</data>
<data id="7">
<x>7</x>
<y>70</y>
<size>70</size>
<description>描述7</description>
</data>
<data id="8">
<x>8</x>
<y>80</y>
<size>80</size>
<description>描述8</description>
</data>
<data id="9">
<x>9</x>
<y>90</y>
<size>90</size>
<description>描述9</description>
</data>
<data id="10">
<x>10</x>
<y>100</y>
<size>100</size>
<description>描述10</description>
</data>
</resources>
三、Xml解析类
public class BubbleData {
public static List<BubbleEntry> parserXml(InputStream in) throws Exception {
//0.声明集合对象
List<BubbleEntry> bubbleEntryList = null;
BubbleEntry bubbleEntry = null;
//1.获取xmlpullparser解析实例
XmlPullParser parser = Xml.newPullParser();
//2.设置XmlPullParser的参数
parser.setInput(in, "utf-8");
//3.获取事件类型
int type = parser.getEventType();
while (type != XmlPullParser.END_DOCUMENT) {
switch (type) {
//4.具体判断下解析到了哪个开始标签
case XmlPullParser.START_TAG://开始解析标签
if ("resources".equals(parser.getName())) {
//5.创建一个集合对象
bubbleEntryList = new ArrayList<BubbleEntry>();
} else if ("data".equals(parser.getName())) {
//6.创建Channel对象
bubbleEntry = new BubbleEntry();
//7.获取id值
String id = parser.getAttributeName(0);
bubbleEntry.setId(id);
} else if ("x".equals(parser.getName())) {
//9.获取x值
String x = parser.nextText();
bubbleEntry.setX(Float.parseFloat(x));
} else if ("y".equals(parser.getName())) {
//10.获取y值
String y = parser.nextText();
bubbleEntry.setY(Float.parseFloat(y));
} else if ("size".equals(parser.getName())) {
//11.获取size值
String size = parser.nextText();
bubbleEntry.setSize(Float.parseFloat(size));
} else if ("description".equals(parser.getName())) {
//12.获取description值
String description = parser.nextText();
bubbleEntry.setDescription(description);
}
break;
case XmlPullParser.END_TAG://解析结束标志
//判断要解析的结束标签
if ("data".equals(parser.getName())) {
//把javabean对象存到集合中
bubbleEntryList.add(bubbleEntry);
}
}
//不停的向下解析
type = parser.next();
}
return bubbleEntryList;
}
}
四、解析
public class XmlActivity extends AppCompatActivity {
private TextView content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xml);
content = findViewById(R.id.content);
try {
//获取main文件夹下assets资源文件夹下的文件转成InputStream
//InputStream inputStream = getResources().getAssets().open("bubbleData.xml");
//获取手机或者平板内部存储“tjtb”文件夹下“bubbleData.xml”文件
File file = new File("/mnt/sdcard/tjtb/bubbleData.xml");
InputStream inputStream = new FileInputStream(file);
//2.调用我们定义的解析xml业务方法
List<BubbleEntry> bubbleEntryList = BubbleData.parserXml(inputStream);
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < bubbleEntryList.size(); i++) {
BubbleEntry bubbleEntry = bubbleEntryList.get(i);
stringBuffer.append("x="+bubbleEntry.getX()+",y="+bubbleEntry.getY()+",size="+bubbleEntry.getSize()+",des="+bubbleEntry.getDescription()+"\n\n");
}
content.setText(stringBuffer);
} catch (Exception e) {
e.printStackTrace();
}
}
}