XML basics and reading data with dom4j

Source: Internet
Author: User
Tags xsl

As we all know, HTML is designed to display data, and XML is designed to store and transmit data. What we usually use is to save and read data. So here we will mainly introduce the basic content of XML and use dom4j to access XML data.

The following describes the basic content of XML-namespace, XSL, DTD, and schema.

 

1. Basic Structure

 

   

-------------------------

? <> Paired, case sensitive

? The top-level element can only have one

? Element cannot start with XML and cannot contain spaces

 

2. namespace

 

It is similar to the namespace in our programming language. For example, if you want to create two classes with the same name, you must place the two classes under different namespaces. XML is similar, that is, to distinguish two elements with the same name with different prefixes before the element. For example, the second sentence in the following xls file: <XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/TR/WD-xsl">

Xmlns in stands for XML namespace, that is, XML namespace. This namespace is represented by the XSL prefix. The specific address below does not seem to have any practical significance, just to give the namespace a unique name.

 

3. XSL

 

XSL ---- Extensible Stylesheet Language (Extended style sheet language) is actuallyXML style sheet,Just like CSS is an HTML style table, it is used to modify the XML display. Make the display more standard and beautiful. Example:

 

When XML is not modified with XSL (style sheet), it is displayed in the browser:

 

  

 

XML is added to the XSL (style sheet) and displayed in the browser:

 

  

 

The content of XSL is as follows:

<? XML version = "1.0" encoding = "gb2312"?> <XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/TR/WD-xsl"> <XSL: template match = "/"> <HTML> 

4. DTD and schema

 

  ?DTD-Document Type Definition, used to define the syntax and structure that XML documents must follow. It specifies that the Tag Name and sequence can only be in a certain format. It has been replaced by schema.

  ?Schema-a more accurate definition of XML syntax and structure than DTD. The labels and types of XML can be agreed.

They can verify whether the XML meets the current standards, for example:

XML document: (doctype indicates the compliance with the syntax and structure Standards)

<Span style = "color: #000000;"> <? XML version = "1.0" encoding = "gb2312"?> <! Doctype books System "book. DTD "> <books> <Name> Ping Yi Xia Ying </Name> <person> Liang Yusheng </person> <price unit =" RMB "> 100.60 </price> </ books> <book> <Name> Yueyang Tower </Name> <person> Fan Zhongyan </person> <price unit = "USD"> 76.8 </price> </book> </books> </span>

 

Corresponding DTD:

<Span style = "color: #000000;"> <? XML version = "1.0" encoding = "gb2312"?> <! Element books (books *)> <! Element book (name, person +, price *)> <! Element name (# pcdata)> <! Element (# pcdata)> <! ATTLIST by gender CDATA 'male'> <! Element price (# pcdata)> <! ATTLIST price unit (RMB | USD | yen) 'RMB'> </span>

? , The attribute of the interval must be strictly in order; the space interval can not be in order

? + Number indicates that the code appears at least once, and * indicates that the code appears 0 or multiple times.

? No + and * must appear once.

 

Corresponding Schema:

<Span style = "color: #000000;"> <? XML version = "1.0" encoding = "gb2312"?> <Xs: schema xmlns: xs = "http://www.w3.org/2001/XMLSchema"> <Xs: element name = "books"> <Xs: complextype> <Xs: sequence> <Xs: element name = "book"> <Xs: element name = "name" minoccurs = "1"> </Xs: Element> <Xs: element name = ""> </Xs: Element> <Xs: element name = ""> <Xs: attribute name = "unit"> <Xs: enumeration value = "RMB"/> <Xs: enumeration value = ""/> <Xs: enumeration value = ""/> </Xs: attribute> </Xs: element> </Xs: sequence> </Xs: complextype> </Xs: Element> </Xs: schema> </span>

 

5. dom4j reads XML

 

It can be seen from the name that it uses Dom to parse XML files. The basic usage is as follows:

First, introduce two jar packages:

? Dom4j-1.6.1.jar

? Jaxen-1.1-beta-6.jar (located under the Lib folder of dom4j,XPath required)

Instance-read the following configuration file: (sys-config.xml ):

<?xml version="1.0" encoding="UTF-8"?><config><db-info><driver-name>oracle.jdbc.driver.OracleDriver</driver-name><url>jdbc:oracle:thin:@localhost:1521:bjpowern</url><user-name uid="zhipeng"/><password pwd="zhipeng"/></db-info></config>

Read files:

<span style="font-size:14px;">SAXReader reader = new SAXReader();InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("sys-config.xml");Document doc = reader.read(in);</span>

Read Node object: (read using XPath)

<span style="font-size:14px;">Element driverNameElt = (Element)doc.selectObject("/config/db-info/driver-name");Element urlElt = (Element)doc.selectObject("/config/db-info/url");Element userNameElt = (Element)doc.selectObject("/config/db-info/user-name");Element passwordElt = (Element)doc.selectObject("/config/db-info/password");</span>

Read the value or attribute of a Node object:

<Span style = "font-size: 14px;"> // obtain the node value string drivername = drivernameelt. getstringvalue (); string url = urlelt. getstringvalue (); // get the node attribute string username = usernameelt. attribute ("uid"); string Password = passwordelt. attribute ("PWD"); </span>

6. Summary

XML is the most common tool for data transmission between various applications. It should be used to separate data from HTML and can be used to exchange, share, and store data.

 

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.