版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chennai1101/article/details/82112289
相关文章
Android 文件操作
Android SQLite应用
Android Xml文件操作
Android SharedPreferences应用
Android Json应用
1. Xml文件写操作
数据类
public class People {
public int id;
public String name;
public String addr;
public int age;
}
XmlSerializer写操作,以startDocument开始,endDocument结束。attribute()
保存属性,text()
保存文本。
private void writeXml(OutputStream output, List<People> peopleList) throws IOException {
XmlSerializer xmlSerializer = Xml.newSerializer();
xmlSerializer.setOutput(output, CHARSET);
xmlSerializer.startDocument(CHARSET, true);
xmlSerializer.startTag(null, TAG_ROOT);
for (People people : peopleList) {
xmlSerializer.startTag(null, TAG_PEOPLE);
xmlSerializer.attribute(null, TAG_ID, Integer.toString(people.id));
writeTextTag(xmlSerializer, TAG_NAME, people.name);
writeTextTag(xmlSerializer, TAG_ADDR, people.addr);
writeTextTag(xmlSerializer, TAG_AGE, Integer.toString(people.age));
xmlSerializer.endTag(null, TAG_PEOPLE);
}
xmlSerializer.endTag(null, TAG_ROOT);
xmlSerializer.endDocument();
}
private void writeTextTag(XmlSerializer xmlSerializer, String tag, String text)
throws IOException {
xmlSerializer.startTag(null, tag);
xmlSerializer.text(text);
xmlSerializer.endTag(null, tag);
}
people.xml文件
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<peoples>
<people id="1">
<name>Mike</name>
<addr>ShangHai</addr>
<age>20</age>
</people>
<people id="2">
<name>Tom</name>
<addr>BeiJing</addr>
<age>23</age>
</people>
<people id="3">
<name>Lily</name>
<addr>ShenZhen</addr>
<age>21</age>
</people>
<people id="4">
<name>Jack</name>
<addr>GuangZhou</addr>
<age>22</age>
</people>
</peoples>
2. Xml文件读操作
(1) DOM方式,一次读入全部文档,优点是比较直观
private List<People> readXmlByDom(InputStream input) throws Exception {
List<People> peopleList = new ArrayList<>();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(input);
Element root = document.getDocumentElement();
NodeList nodeList = root.getElementsByTagName(TAG_PEOPLE);
for (int index = 0; index < nodeList.getLength(); index++) {
Element peopleElement = (Element) nodeList.item(index);
String id = peopleElement.getAttribute(TAG_ID);
People people = new People();
people.id = Integer.parseInt(id);
people.name = readText(peopleElement, TAG_NAME);
people.addr = readText(peopleElement, TAG_ADDR);
people.age = Integer.parseInt(readText(peopleElement, TAG_AGE));
peopleList.add(people);
}
return peopleList;
}
private String readText(Element parent, String tag) {
return parent.getElementsByTagName(tag).item(0).getTextContent();
}
(2) SAX方式,逐行读入,需要指定Handler
private List<People> readXmlBySax(InputStream input) throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
PeopleHandler handler = new PeopleHandler();
SAXParser parser = factory.newSAXParser();
XMLReader xmlReader = parser.getXMLReader();
xmlReader.setContentHandler(handler);
xmlReader.parse(new InputSource(input));
return handler.getPeopleList();
}
private static class PeopleHandler extends DefaultHandler {
private List<People> peopleList = new ArrayList<>();
private People people;
private String text;
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (TAG_PEOPLE.equals(localName)) {
people = new People();
people.id = Integer.parseInt(attributes.getValue(TAG_ID));
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (TAG_PEOPLE.equals(localName)) {
peopleList.add(people);
} else if (TAG_NAME.equals(localName)) {
people.name = text;
} else if (TAG_ADDR.equals(localName)) {
people.addr = text;
} else if (TAG_AGE.equals(localName)) {
people.age = Integer.parseInt(text);
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
text = new String(ch, start, length);
}
public List<People> getPeopleList() {
return peopleList;
}
}
(3) PULL方式,与SAX方法一样
private List readXmlByPull(InputStream input) throws Exception {
List peopleList = new ArrayList<>();
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(new InputStreamReader(input));
int eventType = parser.getEventType();
People people = null;
String text = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (TAG_PEOPLE.equals(parser.getName())) {
people = new People();
people.id = Integer.parseInt(parser.getAttributeValue(null, TAG_ID));
}
} else if (eventType == XmlPullParser.END_TAG) {
if (TAG_PEOPLE.equals(parser.getName())) {
peopleList.add(people);
} else if (TAG_NAME.equals(parser.getName())) {
people.name = text;
} else if (TAG_ADDR.equals(parser.getName())) {
people.addr = text;
} else if (TAG_AGE.equals(parser.getName())) {
people.age = Integer.parseInt(text);
}
} else if (eventType == XmlPullParser.TEXT) {
text = parser.getText();
}
eventType = parser.next();
}
return peopleList;
}