search
HomeBackend DevelopmentXML/RSS TutorialSample code that details three methods of Xml data parsing

1 Overview

Xml, as a data interaction format, involves the generation and parsing of xml data. Here we will describe three methods of Xml parsing.

2. Dom parsing

1. Create a parser factory object (DocumentBuilderFactory object)

2. Create a parser object (DocumentBuilder)

3. Create a Document object

For example, parse the following file

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

        <student id = "1001">
             <id>1</id>
             <name>杨威</name>
             <address>大连</address>
             <age>21</age>
        </student>

        <student id = "1002">
             <id>2</id>
             <name>劉海洋</name>
             <address>深圳</address>
             <age>23</age>
        </student>

        <student id = "1003">
             <id>3</id>
             <name>王小波</name>
             <address>廣州</address>
             <age>22</age>
        </student>

</students>

The parsing code is as follows

[code]package com.kuxiao.train.xml;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class XmlParseTest {

    public static void main(String[] args) throws Exception {

        //xml doc解析步骤
        //1、获取解析工厂对象
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //2、构建解析器对象
        DocumentBuilder   db = dbf.newDocumentBuilder();
        //3、构建docment对象
        Document  doc = db.parse(new File("person.xml"));

        Element ele = doc.getDocumentElement();

        //实现解析逻辑
        NodeList list = doc.getElementsByTagName("student");

        for(int i = 0; i < list.getLength();i++)
        {
              Element element = (Element) list.item(i);
              String attrid = element.getAttribute("id");
              System.out.println("attrid = " + attrid);
              Element  element1 = (Element) element.getElementsByTagName("id").item(0);
              String id = element1.getFirstChild().getNodeValue();
              System.out.println(id);
              element1 = (Element) element.getElementsByTagName("name").item(0);
              String name = element1.getFirstChild().getNodeValue();
              System.out.println(name);
              element1 = (Element) element.getElementsByTagName("address").item(0);
              String address = element1.getFirstChild().getNodeValue();
              System.out.println(address);

        }       

    }

}

3. Notes

1. Element ele = doc.getDocumentElement( ); Get the root element

2. When the element is obtained, the value of the element is also a node, and the value must be obtained by the element.getFirstChild().getNodeValue() method.

3. The blank spaces in xml are also of Node and text type.

4. SAX parsing

1. Create a SAXParserFactory object

2. Create a SAXparser object

3. Create a MyHandler that inherits the DefaultHandler class and overrides the method.

4. sp.parse(new File("student.xml"), new MyHandler(list));

[code]package com.kuxiao.train.xml.sax;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class TestSax {

    public static void main(String[] args) throws Exception {

        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        List<Student> list = new ArrayList<>();
        sp.parse(new File("student.xml"), new MyHandler(list));
        System.out.println(list);

    }
}

class MyHandler extends DefaultHandler {

    private Stack<String> stack = new Stack<>();
    private Student student;
    private List<Student> mList = null;

    public MyHandler(List<Student> list)
    {
          this.mList = list;
    }
    @Override
    public void startDocument() throws SAXException {

        System.out.println("解析文档开始了...");
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
            if(qName.equals("学生"))
            {
                student = new Student();
                if(attributes.getLength() != 0)
                {
                     for(int i = 0; i < attributes.getLength();i++)
                     {
                            String id = attributes.getValue(i);
                            student.setId(Integer.parseInt(id));
                     }
                }
            }
            /*if(qName.equals("姓名"))
            {
                 stack.push(qName);
            }
            if(qName.equals("年龄"))
            {
                 stack.push(qName);
            }
            if(qName.equals("性别"))
            {
                 stack.push(qName);
            }*/
            stack.push(qName);
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
             String qName = stack.peek();
            if(qName.equals("性别")){
                student.setGender(new String(ch,start,length));
            }
            if(qName.equals("姓名")){
                student.setName(new String(ch,start,length));
            }
            if(qName.equals("年龄")){
                student.setAge(new String(ch,start,length));
            }
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
            stack.pop();
            if(qName.equals("学生"))
            {
                mList.add(student);
                student = null;
            }
    }

    @Override
    public void endDocument() throws SAXException {
         System.out.println("解析文档结束了.....");
    }

}

SAX is based on the event model and is parsed sequentially. The internal implementation is The advantage of the observer mode is that it takes up less memory and is highly efficient. The disadvantage is that the encoding is relatively complicated.

5. Pull parsing

1. This parsing method does not come with JDK and needs to import a third-party library

2. Create an XmlPullParserFactory object

3 , Create an XmlPullParser object

4. Call xpp.setInput(is,”utf-8”)

5. Process the next event type of xpp.next() corresponding to the event type

[code]package com.kuxiao.train.xml.pull;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

public class PullTest {

    public static void main(String[] args) throws Exception {

        FileInputStream is = new FileInputStream(new File("person.xml"));
        long time = System.currentTimeMillis();
        List<Student> list = new ArrayList<>();
        XmlPullParserFactory xppf = XmlPullParserFactory.newInstance();
        XmlPullParser xpp = xppf.newPullParser();

        xpp.setInput(is, "utf-8");
        Student student = null;
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {

            switch (eventType) {
            case XmlPullParser.START_TAG:

                if (xpp.getName().equals("student")) {
                    student = new Student();
                    String id = xpp.getAttributeValue(0);
                    student.setId(id);
                } else if (xpp.getName().equals("name")) {
                    student.setName(xpp.nextText());

                } else if (xpp.getName().equals("address")) {
                    student.setAddress(xpp.nextText());
                } else if (xpp.getName().equals("age")) {
                    student.setAge(xpp.nextText());
                }
                break;
            case XmlPullParser.START_DOCUMENT:
                System.out.println("开始了....");
                break;
            case XmlPullParser.END_TAG:
                if (xpp.getName().equals("student")) {
                    list.add(student);
                    student = null;
                }
                break;
            }
            eventType = xpp.next();
        }

        is.close();
        long time1 = System.currentTimeMillis();
        System.out.println(time1 - time);
        for (Student student2 : list) {
            System.out.println(student2);
        }

        FileInputStream fis = new FileInputStream(new File("person.xml"));
        List<Student> list1 = getListBean(fis, new String[] { "id", "name",
                "address", "age", "gender" }, Student.class, 0);
        for (Student student2 : list1) {
            System.out.println(student2);
        }

    }
   //封装的全能解析xml文件的方法 
   //参数说明
   //attrs是文件里bean对象的元素与属性名
   //clazz是Bean对象的class对象
   //j代表属性的个数
    public static <T> List<T> getListBean(InputStream is, String[] attrs,
            Class<T> clazz, int j) throws Exception {
        long time = System.currentTimeMillis();
        T c = null;
        XmlPullParserFactory xppf = XmlPullParserFactory.newInstance();
        XmlPullParser xpp = xppf.newPullParser();
        xpp.setInput(is, "utf-8");
        List<T> list = null;
        int eventType = xpp.getEventType();
        String classname = "";

        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
            case XmlPullParser.START_TAG:
                int bigen = clazz.getName().lastIndexOf(".") + 1;
                classname = clazz.getName().substring(bigen);
                classname = classname.substring(0, 1).toLowerCase()
                        + classname.substring(1);
                String elementName = xpp.getName();

                if (classname.equals(elementName)) {

                    c = clazz.newInstance();
                    if (xpp.getAttributeCount() != 0) {
                        for (int i = 0; i < j; i++) {
                            String attrName = xpp.getAttributeName(i);
                            for (String field : attrs) {

                                if (field.equals(attrName)) {
                                    String frist = field.substring(0, 1)
                                            .toUpperCase();
                                    Method method = clazz.getDeclaredMethod(
                                            "set" + frist + field.substring(1),
                                            new Class[] { String.class });
                                    method.setAccessible(true);
                                    method.invoke(c, xpp.getAttributeValue(i));
                                }

                            }
                        }
                    }

                } else {
                    for (String field : attrs) {

                        if (field.equals(elementName)) {

                            String frist = field.substring(0, 1).toUpperCase();
                            Method method = clazz.getDeclaredMethod("set"
                                    + frist + field.substring(1),
                                    new Class[] { String.class });
                            method.setAccessible(true);
                            method.invoke(c, xpp.nextText());
                        }

                    }
                }

                break;
            case XmlPullParser.START_DOCUMENT:
                list = new ArrayList<T>();
                break;

            case XmlPullParser.END_TAG:
                if (!classname.equals("") && classname.equals(xpp.getName())) {
                    list.add(c);
                    c = null;
                }
                break;
            }
            eventType = xpp.next();
        }
        is.close();
        long time1 = System.currentTimeMillis();
        System.out.println(time1 - time);
        return list;
    }

}

The above is the detailed content of Sample code that details three methods of Xml data parsing. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
RSS Documents: The Foundation of Web SyndicationRSS Documents: The Foundation of Web SyndicationApr 18, 2025 am 12:04 AM

RSS documents are XML-based structured files used to publish and subscribe to frequently updated content. Its main functions include: 1) automated content updates, 2) content aggregation, and 3) improving browsing efficiency. Through RSSfeed, users can subscribe and get the latest information from different sources in a timely manner.

Decoding RSS: The XML Structure of Content FeedsDecoding RSS: The XML Structure of Content FeedsApr 17, 2025 am 12:09 AM

The XML structure of RSS includes: 1. XML declaration and RSS version, 2. Channel (Channel), 3. Item. These parts form the basis of RSS files, allowing users to obtain and process content information by parsing XML data.

How to Parse and Utilize XML-Based RSS FeedsHow to Parse and Utilize XML-Based RSS FeedsApr 16, 2025 am 12:05 AM

RSSfeedsuseXMLtosyndicatecontent;parsingtheminvolvesloadingXML,navigatingitsstructure,andextractingdata.Applicationsincludebuildingnewsaggregatorsandtrackingpodcastepisodes.

RSS Documents: How They Deliver Your Favorite ContentRSS Documents: How They Deliver Your Favorite ContentApr 15, 2025 am 12:01 AM

RSS documents work by publishing content updates through XML files, and users subscribe and receive notifications through RSS readers. 1. Content publisher creates and updates RSS documents. 2. The RSS reader regularly accesses and parses XML files. 3. Users browse and read updated content. Example of usage: Subscribe to TechCrunch's RSS feed, just copy the link to the RSS reader.

Building Feeds with XML: A Hands-On Guide to RSSBuilding Feeds with XML: A Hands-On Guide to RSSApr 14, 2025 am 12:17 AM

The steps to build an RSSfeed using XML are as follows: 1. Create the root element and set the version; 2. Add the channel element and its basic information; 3. Add the entry element, including the title, link and description; 4. Convert the XML structure to a string and output it. With these steps, you can create a valid RSSfeed from scratch and enhance its functionality by adding additional elements such as release date and author information.

Creating RSS Documents: A Step-by-Step TutorialCreating RSS Documents: A Step-by-Step TutorialApr 13, 2025 am 12:10 AM

The steps to create an RSS document are as follows: 1. Write in XML format, with the root element, including the elements. 2. Add, etc. elements to describe channel information. 3. Add elements, each representing a content entry, including,,,,,,,,,,,. 4. Optionally add and elements to enrich the content. 5. Ensure the XML format is correct, use online tools to verify, optimize performance and keep content updated.

XML's Role in RSS: The Foundation of Syndicated ContentXML's Role in RSS: The Foundation of Syndicated ContentApr 12, 2025 am 12:17 AM

The core role of XML in RSS is to provide a standardized and flexible data format. 1. The structure and markup language characteristics of XML make it suitable for data exchange and storage. 2. RSS uses XML to create a standardized format to facilitate content sharing. 3. The application of XML in RSS includes elements that define feed content, such as title and release date. 4. Advantages include standardization and scalability, and challenges include document verbose and strict syntax requirements. 5. Best practices include validating XML validity, keeping it simple, using CDATA, and regularly updating.

From XML to Readable Content: Demystifying RSS FeedsFrom XML to Readable Content: Demystifying RSS FeedsApr 11, 2025 am 12:03 AM

RSSfeedsareXMLdocumentsusedforcontentaggregationanddistribution.Totransformthemintoreadablecontent:1)ParsetheXMLusinglibrarieslikefeedparserinPython.2)HandledifferentRSSversionsandpotentialparsingerrors.3)Transformthedataintouser-friendlyformatsliket

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor