SoFunction
Updated on 2025-04-20

Commonly used data structures and XML parsing in JAVA

1 Common data structures

1.1 Array

  • In programming, for the convenience of processing, several variables with the same type are organized in an orderly manner. These sets of similar data elements arranged in order are called arrays.
  • In C language, arrays belong to constructed data types. An array can be broken down into multiple array elements, which can be a basic data type or a constructor type.
  • Therefore, according to the type of array elements, arrays can be divided intoNumerical arrayCharacter arrayPointer arrayStructure arrayVarious categories

1.2 Stack

  • A stack is a special linear table that can only be inserted and deleted at a certain end.
  • It stores data according to the principle of first in and then out. The first incoming data is pushed into the bottom of the stack, and the last data is at the top of the stack. When it is necessary to read the data, the data will pop up from the top of the stack (the last data is read out by the first one)

1.3 Queue

  • A special linear table that is only allowed at the front end of the table (front) performs a delete operation, while on the backend of the table (rear) perform the insert operation.
  • The end that performs the insertion operation is called the tail of the queue, and the end that performs the deletion operation is called the counter.
  • When there are no elements in the queue, it is calledEmpty queue

1.4 Linked List

  • A non-continuous and non-sequential storage structure on a physical storage unit, and the logical order of data elements is passed through the linked list.Pointer linkImplemented in sequence
  • A linked list consists of a series of nodes (each element in the linked list is called a node), and nodes can be generated dynamically at runtime. Each node consists of two parts:
  • One is to store data elementsData Domain, the other one is to store the next node addressPointer field

1.5 Tree

The tree containsn(n>0)A finite set of nodesK, and inKA relationship is defined inNNThe following conditions are met:

  • There is only one node K0, it is for relationshipsNThere is no front-wheel drive, it is called K0is the root node of the tree, referred to as the root (root
  • Excluding K0outside,Kfor each node in the relationshipNThere is only one front drive
  • KThe relationship between the nodesNIt can bemA successor (m>=0

1.6 Heap

  • In computer science, heaps are a special tree-shaped data structure, with each node having a value.
  • Usually the right data structure we call refers to a binary heap.
  • The characteristic of a heap is that the value of the root node is the smallest (or largest), and the two subtrees of the root node are also a heap.

1.7 Graph

  • The graph is a finite set of nodesVand collection of edgesEcomposition.
  • Among them, in order to distinguish it from the tree structure, nodes are often called vertices in graph structures. Edges are ordered pairs of vertices. If there is an edge between two vertices, it means that the two vertices have an adjacent relationship.

1.8 Hash list

  • If there are keywords andKThe records of equality must bef(K)on the storage location.
  • Therefore, the query record can be obtained directly without comparison, which is called this correspondence.fis a hash function (Hash function), the table established according to this idea is a scatter list

2 XML parsing

2.1 Introduction

XML, an extensible markup language, usually used by developers to transmit and store data, and its definition is relatively simple. It usually starts with the following method to express some information in the document.

After sorting out, parsing XML files through Java programs, there are currently four more mainstream methods:

  • DOM Analysis
  • SAX Analysis
  • JDOM Analysis
  • DOM4J Analysis

Let’s take the following XML file as an example to introduce the parsing implementation of each method.

<?xml version="1.0" encoding="utf-8" ?>
<class>
    <student >
        <name>Zhang San</name>
        <gender>male</gender>
        <age>26</age>
    </student>
    <student >
        <name>Reese</name>
        <gender>male</gender>
        <age>36</age>
    </student>
 <student >
        <name>Wang Wu</name>
        <gender>female</gender>
        <age>24</age>
    </student>
</class>

2.2 DOM analysis

DOMThe full name is:Document Object Model, is the earliest supported type in JavaXMLThe parsing method can be done without relying on any third-party packages,JDKThe API in the w3c package provided can achieve fast parsing and simple code programming.

forXMLA parsed version of the document defines a set of interfaces. The parser reads the entire document, then builds a memory-resident tree structure, and the code can then use itDOMInterface to operate this tree structure

  • Advantages: The entire document tree is in memory, which is easy to operate, supports various functions such as deletion, modification, and rearrangement.
  • Disadvantages: Calling the entire document into memory (including useless nodes), wasting time and space
  • Use occasions: Once the document is parsed, it is necessary to access these data multiple times, and the hardware resources are sufficient (memory,CPU

The implementation process is as follows:

import org..*;
import ;
import ;
import ;

public class DomDemo {
    
    public static void main(String[] args) {
        // 1. Get the xml file stream        InputStream inputStream = ().getResourceAsStream("");
        // 2. Create DocumentBuilderFactory object        DocumentBuilderFactory factory = ();
        // 3. Create DocumentBuilder object        try {
            DocumentBuilder builder = ();
            Document d = (inputStream);
            NodeList stdList = ("student");
            for (int i = 0; i <() ; i++) {
                Node std = (i);
                // traverse tag properties                NamedNodeMap attrs = ();
                for(int j=0; j< (); j++){
                    Node attr = (j);
                    (()+":"+());
                }
                // traverse label child nodes                NodeList childNodes = ();
                for (int k = 0; k <() ; k++) {
                    if ((k).getNodeType()== Node.ELEMENT_NODE) {
                        ((k).getNodeName() + ":" + (k).getTextContent());
                    }
                }
                ("==============");
            }
        } catch (Exception e) {
            ();
        }
    }
}


The operation results are as follows:
id:1
name:Zhang San
gender:male
age:26
==============
id:2
name:Reese
gender:male
age:36
==============
id:3
name:Wang Wu
gender:female
age:24
==============

2.3 SAX analysis

SAXThe full name is:Simple API for XML,TooJDKAnother one providedXMLAnalysis method.

Compared withDOMSAXEach parsing is loaded only in memoryXMLA small part of the file, even for larger XML files, does not require too much memory and there will be no memory overflow problem.

The advantages are as follows:

  • Use event-driven mode to parse data segment by segment, occupying small memory
  • Check the data only when reading it, and does not need to be saved in memory
  • High efficiency and performance, able to parse documents larger than system memory
  • SAXParser code comparisonDOMSmall parser code

Of course there are also disadvantages:

  • Compared with DOM parsers, parsing logic is more complex when reading XML files using SAX parsers.
  • It is impossible to locate the document hierarchy at the same time, and it is difficult to access different parts of the data of the same document at the same time. It is not supportedXPath
  • It is not persistent. If the data is not saved after the event, the data will be lost. Statelessness, only text can be obtained from events, but I don't know which element the text belongs to.

Use occasions:Applet, justXMLA small amount of content in a document, rarely visited back, and less memory in the machine

The implementation process is as follows:

import ;
import ;
import ;

import ;
import ;
import ;
import ;

public class SAXDemoHandel extends DefaultHandler {

    private String value;

    private Map<String, String> student;

    private List<Map<String, String>> students = new ArrayList<>();

    public List<Map<String, String>> getStudents() {
        return students;
    }

    /**
      * xml parsing begins
      * @throws SAXException
      */
    @Override
    public void startDocument() throws SAXException {
        ();
        ("xml parsing begins");
    }

    /**
      * xml parsing ends
      * @throws SAXException
      */
    @Override
    public void endDocument() throws SAXException {
        ();
        ("xml parsing ends");
    }

    /**
      * Start parsing XML elements
      * @param uri
      * @param localName
      * @param qName
      * @param attributes
      * @throws SAXException
      */
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        (uri, localName, qName, attributes);
        ("Start traversal of nodes:" +  qName);

        if (("student")){
            student = new HashMap<>();
            for(int i=0; i<();i++){
                ((i), (i));
            }
        }
    }

    /**
      * End of parsing XML elements
      * @param uri
      * @param localName
      * @param qName
      * @throws SAXException
      */
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        (uri, localName, qName);
        ("Node traversal ends:" +  qName);

        if(("student")){
            (student);
            student = null;
        } else if(("name") || ("gender") || ("age")){
            (qName, value);
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        (ch, start, length);

        // Get the node value array        value = new String(ch,start,length).trim();
        if (!("")) {
            (value);
        }
    }
}
import ;
import ;
import ;
import ;
import ;

public class SAXDemo {

    public static void main(String[] args) throws Exception {
        // 1. Get the xml file stream        InputStream inputStream = ().getResourceAsStream("");
        // 2. Get the SAXParserFactory instance        SAXParserFactory factory = ();
        // 3. Get the SAXparser instance        SAXParser saxParser = ();
        // 4. Create Handel object        SAXDemoHandel handel = new SAXDemoHandel();
        // 5. Parsing XML files        (inputStream, handel);
        // 6. Get the read result        List<Map<String, String>> students = ();
        for (Map<String, String> student : students) {
            (());
        }
    }
}
The operation results are as follows:

{gender=male, name=Zhang San, id=1}
{gender=male, name=Reese, id=2}
{gender=female, name=Wang Wu, id=3}

2.4 JDOM analysis

JDOMyesJavaA very excellent XML open source document parsing library in the ecosystem, you can think of it asDOMandSAXThe combination version of the DOM and SAX are also designed to make up for the shortcomings of DOM and SAX in practical applications.

The advantages are as follows:

  • Tree-based model processes XML files, and data is loaded in memory
  • There is no backward compatibility limitation, so it is simpler than DOM
  • Fast speed, few defects
  • Analytical features with SAX
  • APIs are easier to understand than DOM
  • 20-80Principle, greatly reducing the amount of code

Of course there are also disadvantages:

  • Can handle XML documents larger than memory
  • It does not support traversal packages corresponding to DOM

Use occasions: The functions to be implemented are simple, such as parsing, creating, etc., but at the bottom,JDOMOr useSAX,DOM,Xanandocument

pom dependency

<!--jdom -->
<dependency>
    <groupId></groupId>
    <artifactId>jdom</artifactId>
    <version>1.1.3</version>
</dependency>

The implementation process is as follows:

import ;
import ;
import ;
import ;
import ;
import ;


public class JdomDemo {

    public static void main(String[] args) throws Exception {
        // 1. Get the xml file stream        InputStream inputStream = ().getResourceAsStream("");
        // 2. Create SAXBuilder object        SAXBuilder saxBuilder = new SAXBuilder();
        // 3. Load the input stream into the build        Document document = (inputStream);
        // 4. Get the root node        Element rootElement = ();
        // 5. Get child nodes        List&lt;Element&gt; children = ();
        for (Element child : children) {
            List&lt;Attribute&gt; attributes = ();
            // traverse tag properties            for (Attribute attr : attributes) {
                (()+":"+());
            }
            // traverse label child nodes            List&lt;Element&gt; childrenList = ();
            for (Element  o: childrenList) {
                (() + ":" + ());
            }
            ("==============");
        }
    }
}
The operation results are as follows:

id:1
name:Zhang San
gender:male
age:26
==============
id:2
name:Reese
gender:male
age:36
==============
id:3
name:Wang Wu
gender:female
age:24
==============

2.5 DOM4J Analysis

DOM4JIt is also a very, very excellent XML open source document analysis library in the Java ecosystem, and it is an upgraded product of JDOM.

Initially, it wasJDOMA branch of , which later merged many functions beyond the basic XML document representation, and was finally released as a tool separately.

The advantages are as follows:

  • Excellent performance, powerful function, extremely easy to use
  • Simple development, and also provides some alternatives to improve performance
  • Support XPath

The only disadvantage:

  • The API is too complex

pom dependency

 <!-- dom4j -->
 <dependency>
     <groupId>dom4j</groupId>
     <artifactId>dom4j</artifactId>
     <version>1.6.1</version>
</dependency>

The implementation process is as follows:

public class Dom4jDemo {

    public static void main(String[] args) throws Exception {
        // 1. Get the xml file stream        InputStream inputStream = ().getResourceAsStream("");
        // 2. Create Reader object        SAXReader reader = new SAXReader();
        // 3. Load xml        Document document = (inputStream);
        // 4. Get the root node        Element rootElement = ();
        // 5.Transfer elements        Iterator iterator = ();
        while (()){
            Element stu = (Element) ();
            // traverse tag properties            List&lt;Attribute&gt; attributes = ();
            for (Attribute attribute : attributes) {
                (() + ":" + ());
            }

            // traverse label child nodes            Iterator iterator1 = ();
            while (()){
                Element stuChild = (Element) ();
                (()+":"+());
            }
            ("==============");
        }
    }
}
The operation results are as follows:

id:1
name:Zhang San
gender:male
age:26
==============
id:2
name:Reese
gender:male
age:36
==============
id:3
name:Wang Wu
gender:female
age:24
==============

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.