XML overview and sample code for Pull parsing

黄舟
Release: 2017-03-18 17:41:29
Original
1861 people have browsed it


XMLOverview and Pull parsing details

 ONE Goal ,ONE Passion !
Copy after login

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

Overview:

What is XML?
eXtensible Markup Language (extensible markup language);

Common functions of XML:

1 , used as configuration file
2, data format during data transmission
3, resource file in Android

Basic syntax of XML:

1. The statement must be written on the first line

<?xml version="1.0" encoding="utf-8"?>

    version : xml的版本号.目前只有1,0版本

    encoding: 编码格式
Copy after login

2, there can only be one root tag

3, each tag must be closed

4, cannot Cross-nesting

is in the form of:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="colorPrimary">#3F51B5</color>
     </resources>
Copy after login

5, CDATA area

    <![CDATA[

    将有特殊符号文本显示
    if( 3< 5 ){
       }

    ]]>
Copy after login

should be used when special symbols such as "<", ">" are used in the document CDATA area, otherwise it cannot be compiled normally.

Pull to write the XML file.

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的声明---------- <?xml version=&#39;1.0&#39; encoding=&#39;utf-8&#39; standalone=&#39;yes&#39; ?>
        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();

    }
    }
Copy after login

The generated b.xml file is:

<?xml version=&#39;1.0&#39; encoding=&#39;utf-8&#39; standalone=&#39;yes&#39; ?>
    <书库>
        <书>
            <书名 id="1001">百年孤独</书名>
        </书>
        <书>
            <书名 id="1002">百年孤独</书名>
        </书>
    </书库>
Copy after login

Pull parsing xmlpull_1_1_3_4c.jar two jars are parsed.

If there is no build.path, the following error will be reported:
 public class XmlParserDemo {

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {        
    // 创建pull解析器工厂对象
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();        
        // 创建解析器对象
        ArrayList<Book> list = 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<Book>();
                } 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));
        }

    }
    }
Copy after login
2. The return value of the parser.getName() method – The tag name

is in the shape of:
 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
Copy after login
When parser parses, parser will also move to the tag title text (text). And text has no tag name, so it is null.

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template