Web notes - XML files

XML introduction

Full name of XML: EXtensible Markup language

XML function: used for data storage and transmission

XML writing

Create a file with the suffix xml

Write header information in the first line: <? xml  version="1.0" encoding="UTF-8" >

XML rules

Structural integrity: label correspondence -- > < XXX > < / xxx >

Structural effectiveness: follow many rules on the basis of good structure. If you want the written xml to follow many rules, you need to write a separate xml file -- > to describe the rules (similar to the role of meta annotation)

The file used to define rules is also in the form of xml {XXX DTD (rule label) XXX XSD (rule label) XXX TLD (definition label)

DTD rules

DTD is an effective method to ensure the correct format of XML documents. You can compare XML documents and DTD files to see whether the documents comply with the specifications and whether the elements and labels are used correctly. A DTD document contains: the definition rules of elements, the definition rules of relationships between elements, the attributes that elements can use, and the entity or symbol rules that can be used. DTD is an effective method to ensure the correct format of XML documents. You can compare XML documents and DTD files to see whether the documents comply with the specifications and whether the elements and labels are used correctly. A DTD document contains: the definition rules of elements, the definition rules of relationships between elements, the attributes that elements can use, and the entity or symbol rules that can be used.

DTD usage

1. Create a DTD rule file named myxml Notes in DTD # element and description reference code

    W3C(https://www.w3school.com.cn/dtd/dtd_elements.asp)

<?xml version="1.0" encoding="UTF-8" ?>
<!--Header label and must be the first line to indicate that its file is a dtd Normative documents-->

<!-- Element description format:
  <!ELEMENT Element name category>  -->
<!-- describe school label class Mark school Sub tags within Tags *Indicates the number of sub tags that can have-->
        <!ELEMENT school (class*)>
        <!ELEMENT class (teacher+,student*)>
        <!ELEMENT teacher (sex,name)>
        <!ELEMENT student (sex,name)>
        <!ELEMENT sex (#PCDATA)>
        <!ELEMENT name (#PCDATA)>


<!-- Attribute description format:
    <!ATTLIST Element name
        Attribute name attribute type default
        Attribute name attribute type default
        Attribute name attribute type default
>  -->

        <!-- Attribute name:id  Attribute type:Character data default: attribute is not required  -->
        <!ATTLIST school
                id CDATA #REQUIRED
                name CDATA #IMPLIED
                loc CDATA #IMPLIED
         >
        <!ATTLIST class
                id CDATA #IMPLIED
                name CDATA #IMPLIED
                loc CDATA #IMPLIED
         >
        <!ATTLIST teacher
                id CDATA #IMPLIED
                name CDATA #IMPLIED
                age CDATA #IMPLIED
        >
        <!ATTLIST student
                id CDATA #IMPLIED
                name CDATA #IMPLIED
                age CDATA #IMPLIED
        >


<!--Define entity
 <!ENTITY Entity name "Value of entity">  -->

        <!ENTITY school "School name">
        <!ENTITY class "Class name">
        <!ENTITY id "Student number">
        <!ENTITY name "name">

2. Create an XML file to implement the above rules. Import method: <! DOCTYPE school SYSTEM "myxml.dtd">

<?xml version="1.0" encoding="UTF-8"?>
<!--Header label and must be the first line to indicate that its file is a XML Type of file-->


<!-- Import just created dtd rule -->
<!DOCTYPE school SYSTEM "myxml.dtd">

<school id="xxx" name="" loc="">

    <class id="&class;" name="" loc="">
        <teacher id="" name="" age="">
            <sex></sex>
            <name>&name;</name>
        </teacher>

        <student id="" name="" age="">
            <sex></sex>
            <name></name>
        </student>

        <student id="" name="" age="">
            <sex></sex>
            <name></name>
        </student>
    </class>


    <class id="" name="" loc="">
        <teacher id="" name="" age="">
            <sex></sex>
            <name></name>
        </teacher>
        <student id="" name="" age="">
            <sex></sex>
            <name></name>
        </student>
    </class>

</school>

Note: the order of labels should be consistent with the order when they are created.

For example: <! Element class (teacher +, student *) > when creating a tag: the teacher tag should be in front of (above) the student tag

XML parsing

The essence is to read the contents of the file

There are two ways to parse XML:

DOM parsing (Document Object Model)
Tree structure processing method
Parse all documents to form a tree structure and parse them down in turn
Advantages: easy programming
Disadvantages: the whole document must be processed completely (slow)

SAX parsing (Simple Api for Xml) -- extension
Similar to streaming media
A series of events are triggered during event based pattern {parsing
When a tag tag is parsed, the subsequent method is} activated
Advantages: fast (no need to process all documents at one time)
Disadvantages: writing is very troublesome (reusability is not strong)

DOM parsing usage

Via org w3c. dom.* Document, Element and NodeList under the package are used to parse XML files.

Method called:

       document.getDocumentElement();// Get root label object (school)

       school.getAttribute("id");// Get the properties in the school tag

       NodeList classList  =  school.getElementsByTagName("class");// Child tags in the school tag

       classList.getLength();// Get classlist length

       classList.item(num);// Get a value in classlist

package testDtd;

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

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;

@SuppressWarnings("all")
public class TestDom {
    public static void main(String[] args) {
        try {
            //Document Object Model in DOM mode
            //1. Need a factory
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            //2. Use the factory to create workers
            DocumentBuilder builder = factory.newDocumentBuilder();
            //3. The worker creates a document object (a drawing xml file is required)
            File file = new File("src/testDtd/school.xml");
            //InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("testxml/school.xml");
            Document document = builder.parse(file);

            //4. Parsing xml files
            //Get root label object (school)
            //Mode 1
            Element school = document.getDocumentElement();
            //Mode 2
//            Element school = document.getElementById("duyi");// dtd rules are required
            //Mode 3
//            NodeList RootList = document.getElementsByTagName("school");
//            Element school = RootList.item(0);//   list.get(0);
            //Get the properties in the school tag
            String schoolID = school.getAttribute("id");
            String schoolName = school.getAttribute("name");
            String schoolLOC = school.getAttribute("loc");
            System.out.println(schoolID+"--"+schoolName+"--"+schoolLOC);
            //Child tags in the school tag
            //Mode 1
//            NodeList classList = document.getElementsByTagName("class");
            //Mode 2
            NodeList classList = school.getElementsByTagName("class");
            //Mode 3
//            NodeList classList = school.getChildNodes();// dtd rules are required

            for(int i=0;i<classList.getLength();i++){
                //A class tag
                Element classEle = (Element)classList.item(i);
                String classID = classEle.getAttribute("id");
                String className = classEle.getAttribute("name");
                String classLOC = classEle.getAttribute("loc");
                System.out.println("\t"+classID+"--"+className+"--"+classLOC);
                //The child tag of the class tag is teacher
                //Mode 1
                Element teacher = (Element)classEle.getElementsByTagName("teacher").item(0);
                //Mode 2
//                Element teacher = (Element)classEle.getFirstChild();// There are rules for dtd
                String teacherID = teacher.getAttribute("id");
                String teacherName = teacher.getAttribute("name");
                String teacherAge = teacher.getAttribute("age");
                Element teacherSexEle = (Element)teacher.getElementsByTagName("sex").item(0);
                String teacherSex = teacherSexEle.getTextContent();
                System.out.println("\t\t"+teacherID+"--"+teacherName+"--"+teacherAge+"--"+teacherSex);

                //Sub label student in class label
                NodeList studentList = classEle.getElementsByTagName("student");
                for(int j=0;j<studentList.getLength();j++){
                    Element student = (Element)studentList.item(j);
                    String studentID = student.getAttribute("id");
                    String studentName = student.getAttribute("name");
                    String studentAge = student.getAttribute("age");
                    Element sex = (Element)student.getElementsByTagName("sex").item(0);
                    String studentSex = sex.getTextContent();//Get the text content in the label < > XXX < / >
                    System.out.println("\t\t"+studentID+"--"+studentName+"--"+studentAge+"--"+studentSex);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

Keywords: Java

Added by Roscoe on Mon, 07 Mar 2022 20:18:44 +0200