[java] [view plain](() [copy](() [print](() [?](()
-
/** Pull方式,创建 XML */
-
public String pullXMLCreate(){
-
StringWriter xmlWriter = new StringWriter();
-
Person []persons = new Person[3]; // 创建节点Person对象
-
persons[0] = new Pers 《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》无偿开源 徽信搜索公众号【编程进阶路】 on(1, “sunboy_2050”, “http://blog.csdn.net/sunboy_2050”);
-
persons[1] = new Person(2, “baidu”, “http://www.baidu.com”);
-
persons[2] = new Person(3, “google”, “http://www.google.com”);
-
try {
-
// // 方式一:使用Android提供的实用工具类android.util.Xml
-
// XmlSerializer xmlSerializer = Xml.newSerializer();
-
// 方式二:使用工厂类XmlPullParserFactory的方式
-
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
-
XmlSerializer xmlSerializer = factory.newSerializer();
-
xmlSerializer.setOutput(xmlWriter); // 保存创建的xml
-
xmlSerializer.setFeature(“http://xmlpull.org/v1/doc/features.html#indent-output”, true);
-
// xmlSerializer.setProperty(“http://xmlpull.org/v1/doc/properties.html#serializer-indentation”, " "); // 设置属性
-
// xmlSerializer.setProperty(“http://xmlpull.org/v1/doc/properties.html#serializer-line-separator”, “\n”);
-
xmlSerializer.startDocument(“utf-8”, null); // <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
-
xmlSerializer.startTag(“”, “root”);
-
xmlSerializer.attribute(“”, “author”, “homer”);
-
xmlSerializer.attribute(“”, “date”, “2012-04-28”);
-
int personsLen = persons.length;
-
for(int i=0; i<personsLen; i++) {
-
xmlSerializer.startTag(“”, “person”); // 创建person节点
-
xmlSerializer.startTag(“”, “id”);
-
xmlSerializer.text(persons[i].getId()+“”);
-
xmlSerializer.endTag(“”, “id”);
-
xmlSerializer.startTag(“”, “name”);
-
xmlSerializer.text(persons[i].getName());
-
xmlSerializer.endTag(“”, “name”);
-
xmlSerializer.startTag(“”, “blog”);
-
xmlSerializer.text(persons[i].getBlog());
-
xmlSerializer.endTag(“”, “blog”);
-
xmlSerializer.endTag(“”, “person”);
-
}
-
xmlSerializer.endTag(“”, “root”);
-
xmlSerializer.endDocument();
-
} catch (XmlPullParserException e) { // XmlPullParserFactory.newInstance
-
e.printStackTrace();
-
} catch (IllegalArgumentException e) { // xmlSerializer.setOutput
-
e.printStackTrace();
-
} catch (IllegalStateException e) { // xmlSerializer.setOutput
-
e.printStackTrace();
-
} catch (IOException e) { // xmlSerializer.setOutput
-
e.printStackTrace();
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
savedXML(fileName, xmlWriter.toString());
-
return xmlWriter.toString();
-
}
运行结果:
3、Pull 解析 XML
pull方式,解析xml是通过 XmlPullParser 类实现
首先,通过XmlPullParser得到解析xml的实例 xpp
接着,通过 xpp设置输入 xpp.setInput(is, “utf-8”),声明定义保存xml信息的数据结构(如:Person数组)
然后,通过 xpp 解析 START_DOCUMENT、START_TAG、TEXT、END_TAG、END_DOCUMENT等
Code
[java] [view plain](() [copy](() [print](() [?](()
-
/** Pull方式,解析 XML */
-
public String pullXMLResolve(){
-
StringWriter xmlWriter = new StringWriter();
-
InputStream is = readXML(fileName);
-
try {
-
// // 方式一:使用Android提供的实用工具类android.util.Xml
-
// XmlPullParser xpp = Xml.newPullParser();
-
// 方式二:使用工厂类XmlPullParserFactory的方式
-
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
-
XmlPullParser xpp = factory.newPullParser();
-
xpp.setInput(is, “utf-8”);
-
List personsList = null; // 保存xml的person节点
-
Person person = null;
-
StringBuffer xmlHeader = null; // 保存xml头部
-
String ele = null; // Element flag
-
int eventType = xpp.getEventType();
-
while(XmlPullParser.END_DOCUMENT != eventType) {
-
switch (eventType) {
-
case XmlPullParser.START_DOCUMENT:
-
personsList = new ArrayList(); // 初始化persons
-
xmlHeader = new StringBuffer(); // 初始化xmlHeader
-
break;
-
case XmlPullParser.START_TAG:
-
if(“root”.equals(xpp.getName())) {
-
String attrAuthor = xpp.getAttributeValue(0);
-
String attrDate = xpp.getAttributeValue(1);
-
xmlHeader.append(“root”).append(“\t\t”);
-
xmlHeader.append(attrAuthor).append(“\t”);
-
xmlHeader.append(attrDate).append(“\n”);
-
} else if(“person”.equals(xpp.getName())) {
-
person = new Person(); // 创建person实例
-
} else if(“id”.equals(xpp.getName())) {
-
ele = “id”;
-
} else if(“name”.equals(xpp.getName())) {
-
ele = “name”;
-
} else if(“blog”.equals(xpp.getName())) {
-
ele = “blog”;
-
} else {
-
ele = null;
-
}
-
break;
-
case XmlPullParser.TEXT:
-
if(null != ele) {
-
if(“id”.equals(ele)) {
} else {
-
ele = null;
-
}
-
break;
-
case XmlPullParser.TEXT:
-
if(null != ele) {
-
if(“id”.equals(ele)) {