Java, little knowledge --XML I do not understand

XML Overview

Extensible Markup Language: scalable Markup Language

use:

1. Data storage, small databases, there is a certain CRUD operations feasibility

2. The data transmission network

3.JavaWEB Framework Project Profiles

The basic syntax:

1.XML file extension is .xml format

2.XML first line is the defining declaration for the current file

3.XML file has one and only one root tag

4. quoted attribute values ​​must contain recommended double quotes

The label must be properly matched, correct and proper closing start

6.XML label within strict case-sensitive

Writing the code:

<?xml version="1.0" ?>
<users>
    <user id="1">
        <name>James</name>
        <age>16</age>
    </user>
    <user id="2">
        <name>JK</name>
        <age>16</age>
    </user>
</users>

Part of the XML file

1 document declaration:

. A format:

<? Xml list of attributes? >:? <Xml version = "1.0" encoding = "utf-8">

version: The current xml file version number

encoding: coding, coding suggested here to save xml file parsing and consistent with the corresponding code set

standalone: ​​whether to rely on other documents (understand); yes does not depend on, no dependence

2. Directive (understand)

Here you can import some css styles

3. Custom label content

rule:

a. custom tag allows the use of letters, numbers, and other punctuation

b. are not allowed to use numbers and punctuation marks the beginning, only allows the use of the English alphabet

c. xml tags are not allowed in custom labels, caps XML does not work

d. name spaces are not allowed

4. Properties

Can give the label a property, sometimes required ID attribute is unique

5. Text (Learn)

CDATA area, WYSIWYG, CDATA content area is full of show

format:

<! [CDATA [Data]]>

XML file data constraints

1.DTD

A simple way of constraint, but there are some constraints

2.Schema

A complex XML document constraint way, very strict

DTD constraints

Schema Constraint

XML parsing

XML parsing ideas:

DOM parsing : Document Object Model document object model; the entire XML document as a Document object, each node as a Element, the node has Attribute, Text or text content exists in the current node.

Dom resolution is to read the entire XML file into the computer's memory, you can make CRUD operations

Disadvantages:

It takes up a lot of memory space

For the environment:

Suitable for parsing an XML file

SAX parsing:

Read line by line, to give a certain event actions; to read single line, the line is released content, can effectively save memory space.

Disadvantages:

XML documents can not, additions or deletions to change search operation

For the environment:

Phone reading methods used when parsing an XML file

XML file parsing tools

1.JAXP:

A basic parser SUN company provides support for DOM and SAX two analytical way, but the operation is very complicated and not easy programmers to develop

2kdom4j:

DOM For Java;

A very good parser

Spring, SpringMVC ... in the framework of an integrated XML parser

3.Jsoup:

Java tool for giving complete HTML parsing, because HTML and XML files are markup language; to Jsoup a URL, page address, Java small reptiles

4.PULL

The integrated XML parsing tools on Android phones, SAX parse

Dom4j Getting Started

1. Package guide

Currently use a third-party tool, not a native JDK; to import third-party jar package

2. Set IDEA

3.Dom4j related to the method:

SAXReader (); core classes used to parse XML file

read (); XML Document object file

Document object can be used getRootElement (); method to get the current root object is an XML file

Element object methods can be used:

List elements (); all child nodes of the current node

List elements (String name); all child nodes specify the name of the current node

Element element (); obtaining a first child node of the current node under

Element element (String name); get the first child node specifies the name of the current node

Attribute getAttribute (String name); obtaining a corresponding attribute object according to the attribute Attribute name; Attribute node can use String getValue () to get the corresponding node data

String getName (); Gets the name of the current node

String getText (); Gets the current node

Dom4j Xpath parse XML file to read

XML files are saved

Process:

1. Create a Document object

2. Document object to add elements by:

         addElement();

         addAttribute ();

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Published 14 original articles · won praise 27 · views 9501

Guess you like

Origin blog.csdn.net/weixin_42597414/article/details/104690267