ONE Goal ,ONE Passion !
AndroidThe most developed ones are json. And parsing xml With Xstream, I have almost forgotten the manual parsing of xml. Generally, the most common ones are demo4j based on dom, and pull parsing based on sax. There is another kind that I have forgotten
What is XML?
eXtensible Markup Language (extensible markup language);
1 , used asconfiguration file
2, data format during data transmission
3, resource file in Android
1. The statement must be written on the first line
version : xml的版本号.目前只有1,0版本 encoding: 编码格式
2, there can only be one root tag
3, each tag must be closed
4, cannot Cross-nesting
is in the form of:
#3F51B5
5, CDATA area
should be used when special symbols such as "<", ">" are used in the document CDATA area, otherwise it cannot be compiled normally.
public class WriteXml { public static void main(String[] args) throws Exception { // 创建xml解析工厂 XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); // 通过工厂创建序列化器(xml生成器) XmlSerializer ser = factory.newSerializer(); // 为序列化器指定输出流(将xml文件写到指定的某个文件中) ser.setOutput(new FileOutputStream("src/b.xml"), "utf-8"); // 开始写xml文件 // 1.xml的声明---------- ser.startDocument("utf-8", true); // 2. 开始标签 ---------- <书库> ser.startTag(null, "书库"); for ( int i = 0; i < 2; i++) { //3.开始标签 ------------- <书> ser.startTag(null, "书"); // 4. 开始标签------------ <书名> ser.startTag(null, "书名"); //4.1为书名标签设置 属性--------- <书名 id = "1001"> ser.attribute(null, "id", "1001"); //4.2标签设置 文本------------ <书名 id = "1001" >百年孤独 ser.text("百年孤独"); // 5,结束标签 ------------ 书名> ser.endTag(null, "书名"); // 6.结束标签------------书> ser.endTag(null, "书"); } // 7.根标签 结束-----------书库> ser.endTag(null, "书库"); ser.endDocument(); } }
The generated b.xml file is:
<书库> <书> <书名 id="1001">百年孤独书名> 书> <书> <书名 id="1002">百年孤独书名> 书> 书库>
public class XmlParserDemo { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // 创建pull解析器工厂对象 XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); // 创建解析器对象 ArrayListlist = null; Book book = null; // 获得解析器 XmlPullParser parser = factory.newPullParser(); // 从指定文件中解析 出xml parser.setInput(new FileInputStream("src/b.xml"), "utf-8"); // 1 . 判断是否是根标签的结束标签 while (parser.getEventType() != XmlPullParser.END_DOCUMENT) { switch (parser.getEventType()) { case XmlPullParser.START_TAG: // 开始标签 if (parser.getName().equalsIgnoreCase("书库")) { // ------------<书库> // 如果是 跟标签时,创建集合存储 对象 list = new ArrayList (); } else if (parser.getName().equalsIgnoreCase("书")) { // ----------<书> // 当标签是对象时. 创建 对象 book = new Book(); } else if (parser.getName().equalsIgnoreCase("书名")) { // ----------<书名> // 获得当前标签中的 属性以及text.并存储到对象中 String id = parser.getAttributeValue(null, "id"); book.setId(id); String name = parser.nextText(); book.setName(name); } break; case XmlPullParser.END_TAG: // 结束标签 // 标签为对象的结束标签时,将对象存储到集合中 书> if (parser.getName().equalsIgnoreCase("书")) { // -------书> // 注意:当时书名>结束标签时不用做处理 list.add(book); } break; } parser.next(); } for (int i = 0; i < list.size(); i++) { System.out.println("---" + list.get(i)); } } }
org.xmlpull.v1.XmlPullParserException: caused by: org.xmlpull.v1.XmlPullParserException: resource not found: /META-INF/services/org.xmlpull.v1.XmlPullParserFactory make sure that parser implementing XmlPull API is available
The above is the detailed content of XML overview and sample code for Pull parsing. For more information, please follow other related articles on the PHP Chinese website!