Ding! Your xml package has arrived!

XML

Introduction

Extensible Markup Language (eXtensible Markup Language)
features:

  1. XML is platform-independent and is an independent markup language.
  2. xml is self-descriptive

Why learn XML?

  1. Network data transmission.
  2. data storage
  3. Configuration file

XML file

.XML files are a way to save XML data.
XML data can also exist in other ways (such as building XML data in memory).
Don't narrowly understand XML language as XML files.

XML syntax format

1. XML document declaration

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

2. Tags (element/tags/nodes)
XML documents are composed of individual tags.
Syntax:

Start tag (open tag): <tag name>
End tag (closed tag): </tag name>

Tag name: custom name, must follow the following naming rules:

1. The name can contain letters, numbers and other characters
2. The name cannot start with numbers or punctuation
3. The name cannot start with the character "xml" (or XML, Xml)
4. The name cannot contain spaces or colons (: )
5. The name is case sensitive

Marked content: Between the start tag and the end tag, is the marked content.
For example, we use a tag to describe a person's name:

  1. In an XML document, there must be and only one root tag is allowed.

  2. Markers can be nested, but they are not allowed to cross.

  3. The hierarchical name of the tag (child tag, parent tag, sibling tag, descendant tag, ancestor tag)
    advanced syntax CDATA (understand)
    name is the sibling tag of length.
    person is the parent tag
    of name . persons is the ancestor tag of name.

  4. Tag name is allowed to repeat

  5. In addition to the beginning and end of the tag, there are attributes.
    The attributes in the tag are described at the beginning of the tag and consist of the attribute name and the attribute value.
    Format:
    In the beginning tag, the attribute is described. It
    can contain 0-n attributes, and each attribute is A key-value pair!
    Attribute names are not allowed to be repeated. Use an equal sign to connect keys and values, and use spaces to separate multiple attributes.
    Attribute values ​​must be enclosed in quotation marks.

  6. Comments
    Comments cannot be written before the document declaration.
    Comments cannot be nested comments

Give a chestnut

Insert picture description here

Java parses XML (must ask for interview)

There are several XML parsing methods in Java? What are the differences? What are the advantages and disadvantages?
Answer: Four.

  1. SAX parsing The
    parsing method is an event-driven mechanism! The
    SAX parser reads the XML file line by line and parses it. Whenever the start/end/content/attribute of a tag is parsed, an
    event is triggered.
    We can write a program when these events occur , And deal with it accordingly.

Advantages: The analysis can start immediately, instead of waiting for all the data to be processed and loaded line by line, saving memory. It is helpful to parse documents larger than the system memory. Sometimes it is not necessary to parse the entire document. It can stop parsing when a certain condition is met.
Disadvantages : One-way parsing, unable to locate the document level, unable to access different parts of the same document at the same time (because line-by-line parsing, when the nth line is parsed, the n-1th line has been released and cannot be operated). Knowing the level of the element when the event occurs, you can only maintain the parent/child relationship of the node yourself. Read-only parsing mode, unable to modify the content of the XML document.

  1. DOM parsing
    is the official W3C standard for expressing XML documents in a platform and language-independent way. Analysis of this structure usually requires loading the entire
    document and building a document tree model in memory. Programmers can complete data acquisition, modification and deletion by manipulating the document tree. etc.
    advantages:
    document loaded in memory, allowed to make changes to the data and structure.
    access is two-way, two-way can parse the data in the tree at any time.
    Disadvantages:
    All documents are loaded in memory, which consumes a lot of resources.
  2. The
    purpose of JDOM parsing is to become a Java-specific document model, which simplifies the interaction with XML and is faster than using DOM. Since it is the first
    Java-specific model, JDOM has been vigorously promoted and promoted.
    The JDOM document states that its purpose is to "use 20% (or less) of effort to solve 80% (or more) Java/XML problems"
    (assumed to be 20% based on the learning curve)
    Advantages:
    Use concrete classes instead of interfaces, which simplifies DOM API.
    Extensive use of Java collection classes is convenient for Java developers.
    Drawback:
    DOM4J parse XML master
    document object Document
    element object Element
    no better flexibility.
    The performance is not so excellent.
  3. DOM4J analysis
    It is an intelligent branch of JDOM. It incorporates many functions beyond basic XML document representation, including integrated XPath
    support, XML Schema support, and event-based processing for large or streaming documents. It also provides options for constructing document representations.
    DOM4J is an excellent Java XML API with excellent performance, powerful functions and extremely easy-to-use features. At the same time, it is also an
    open source software. Now you can see that more and more Java software is using DOM4J to read and write XML.
    At present, DOM4J is widely used in many open source projects, such as Hibernate

DOM4J parses XML

step:

  1. Introduce the jar file dom4j.jar
  2. Create an input stream pointing to the XML file
    FileInputStream fis = new FileInputStream("xml file address");
  3. Create an XML reading tool object
    SAXReader sr = new SAXReader();
  4. Use the reading tool object to read the input stream of the XML document, and get the document object
    Document doc = sr.read(fis);
  5. Through the document object, get the root element object in the XML document
    Element root = doc.getRootElement();

Document object Document

Refers to the entire XML document loaded into memory.
Common methods:

  1. Through the document object, get the root element object in the XML document
    Element root = doc.getRootElement();
  2. Add root node
    Element root = doc.addElement("root node name");

Element Object

Refers to a single node in an XML document.
Common methods:

  1. Get the node name
    String getName();
  2. Get the content of the node
    String getText();
  3. Set node content
    String setText();
  4. According to the name of the child node, get the first child node object matching the name.
    Element element(String child node name);
  5. Get all child node objects
    List elements();
  6. Get the attribute value of the node
    String attributeValue (String attribute name);
  7. Get the content of the child node
    String elementText (String child node name);
  8. Add child node
    Element addElement(String child node name);
    Parsing local file case:
    Parsing network file case:
  9. Add attribute
    void addAttribute(String attribute name, String attribute value)

HTTP protocol

HTTP protocol

Guess you like

Origin blog.csdn.net/AzirBoDa/article/details/113755012