Take you through XML

Source: Internet
Author: User
1. XML: extensiblemarkuplanguage extensible markup language version1.0 scalable: all labels are custom functions: Data storage configuration file data transmission html and xml difference HTML syntax loose, xml syntax: Strictly html is used for page display. xml is used for data storage. all Html tags are predefined. xml labels are custom. 2. xml syntax: document declaration required. 1. XML: extensible markup language version = "1.0"

  • Scalable: all labels are customized.

  • Function: Data storage

    • Configuration file

    • Data transmission

  • Differences between html and xml

    • The HTML syntax is loose and the xml syntax is strict.

    • Html for page display, xml for data storage

    • All Html tags are predefined, and all xml tags are custom.

2. xml syntax:

  • Document Declaration

    • Fixed version 1.0

    • Encoding specifies the document's code table default value is ISO-8859-1

    • Standalone specifies whether the document is independent. yes or no

    • Must be written in the first line of the xml document

    • Statement:

    • Attribute

  • Tags in element xml documents

    • Element names are case sensitive

    • The number cannot start

    • The document must have only one root element.

    • Elements must be properly closed

    • Element needs to be correctly nested

    • Element names must comply

  • Text

    • Escape character>;

    • The data in CDATA is displayed as is.

  • Attribute

    • Attribute values must be enclosed by quotation marks.

  • Note

  • Processing Command: Not required now

3. XML constraints

  • Constraints are the writing rules of xml.

  • Classification of constraints:

    • Import xsd constraints

    • Compile the root tag

    • Introduce the instance namespace xmlns: xsi = "www.w3.org/2001/XMLSchema-instance"

    • Introduce namespace xsi: schemaLocation = "www.itcast.cn/xml student. xsd"

    • Introduce the default namespace

    • Student. xsd

    • Student. xml

    •     
              
               
                   
                        
                     
                
               
                   
                        
                         
                         
                     
                    
                
               
                   
                        
                         
                     
                
               
                   
                        
                         
                     
                
               
                   
                        
                     
                
           
          
          
    •         
                   
            
             asfd
                    12        
            
             male
                
           
          
              
                   
            
             asfd
                    
            
             12
                    
            
             male    
            
           
          
    • Internal dtd defines dtd in xml

    • External dtd defines a dtd in an external file

    • Student. dtd

    • Student. xml

    • Local dtd file

    • Network dtd file

    •     
          
          
          
          
          Unique, required
          
              
                   
            
             
      Zs
            Abc        
            
             
      Yao
                
           
          
    • Dtd constraints are not rigorous

    • Schema

4. XML parsing

  • XML parsing can be performed:

    • If xml is used as the configuration file: Read

    • If xml is used as the transmission File: write and read

  • XML parsing ideology:

    • Advantage: no memory occupied, fast

    • Disadvantage: Read-only, not write-back

    • Advantage: because the dom tree is formed in the memory, you can add, delete, modify, and query the dom tree.

    • Disadvantage: dom trees occupy very memory and slow resolution

    • Document Element Text Attribute Comment

    • DOM: loads a document to the memory to form a dom tree (document object). It encapsulates all components of the document into some objects.

    • SAX: row-by-row reading, event-driven

  • Common xml parser

    • Defines a rule

    • Usage

    • Procedure

    • XPath:

    • public classTestXPath2 {      @Test      publicvoidtest()throwsException{            SAXReaderread= new SAXReader();            Documentdocument= read.read("src/Dom4jTest.xml");            Listnodes= document.selectNodes("/bookstore//book/title");            for(inti= 0;i< nodes.size();i++) {                  Nodenode= (Node)nodes.get(i);                  System.out.println(node.getText());            }      }}
    • SelectSingleNode ()

    • SelectNodes ()

    • Note: export the jaxen .. jar package.

    • Create a parser SAXReader reader = new SAXReader ()

    • Parse xml to obtain the document object Document document = reader. read (url)

    • // Nodename select this node.

    • /// Select from the root node.

    • //// Select the nodes in the document from the current node that matches the selected node, regardless of their location.

    • // .. Select the parent node of the current node.

    • // @ Select attributes.

    • // [@ Attribute name] attribute filtering

    • // [Tag name] child element filter

    • @ Test

      // Traverse all element nodes

    • Publicvoidtest2 () throwsException {// Create an xml parsing object SAXReaderreader = new SAXReader (); // load the xml document to the document object Documentdocument = reader. read ("src/Book. xml "); Elementroot = document. getRootElement (); treeWalk (root);} privatevoidtreeWalk (Elementele) {// output the name of the current node System. out. println (ele. getName (); // ele. nodeCount () obtains the number of all subnodes of the current node for (inti = 0; I
          
    • Public classTestDom4j {@ Test publicvoidtest1 () throwsException {// Create an xml parsing object SAXReaderreader = new SAXReader (); // load the xml document to the document object Documentdocument = reader. read ("src/Book. xml "); Elementroot = document. getRootElement (); // Element bookNode = root. element ("book"); // System. out. println (bookNode. getName (); // obtain all the subnodes of the current node Listlist = root. elements (); // Obtain the object ElementsecondBook = (Element) list. get (1); // get the text content of the current node Stringname = secondBook. element ("name "). getText (); System. out. println (name );}
    • Import the jar package dom4j. jar

    • Create a parser

    • Parse xml to obtain the document object

    • SAXReader reader = new SAXReader ()

    • Document document = reader. read (url)

    • Parsing supported by JAXP sun dom and sax

    • JDOM

    • DOM4J dom for java is a folk method, but it is a fact method, very good, supports dom

    • Parse xml

    • XPATH for query

The above section provides you with an in-depth understanding of the XML content. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.