Simplified XML tutorial (4)

Source: Internet
Author: User
As mentioned in the previous tutorial, XML is used to store, transmit, and exchange data. During this period, the correctness of XML is crucial. in order to ensure the correctness of XML, appropriate measures are taken.

Directory

Development history

Comparison between XML and HTML

Syntax details for comparison between XML and HTML

DTD for XML verification

XML syntax structure

XML namespace

DOM4J read/write configuration file

About SLT

DTD for XML verification


As mentioned in the previous tutorial, XML is used to store, transmit, and exchange data. During this period, the correctness of XML is crucial. in order to ensure the correctness of XML, appropriate measures are taken.

The correctness of XML is divided into two aspects: XML syntax and XML content. People refer to XML with correct syntax as "well-formed" XML. for a well-formed XML document, we can only ensure that the format of this document complies with XML specifications, in other words, it is impossible to know whether the XML syntax is correct, but the relationship between elements and whether the attribute values are correct. For a well-formed document, if it is used only in a limited number of applications, such as a configuration file in a self-developed system or data storage and transmission, it may well satisfy our applications. However, if you want other users to understand or use your XML documents, or exchange data, you must ensure that the XML is "legal. In this way, it is necessary to provide an XML verification mechanism to ensure that the structure of the XML document we write is the same as that of the XML document written by others, the relationship between elements is correct, and the attribute value meets the requirements.

This mechanism has been provided for us in the XML standard, that is, the DTD (Document Type Definition, Document Type Definition) we mentioned earlier ). In other words, whether your XML can be verified by using DTD is "legal" XML.

You can either define a DTD directly in an XML document or introduce an external DTD file through a URI. Although the internal DTD is convenient, it will cause the document length to increase the transmission burden. if multiple XML documents need to share one DTD, we need to add a DTD to each document, this is quite tedious. Therefore, we recommend that you put the DTD in a separate file for definition and reference the external DTD file through URI in the XML document.

The following describes how to use a DTD file to verify the validity of an XML file.

Code of the test. xml file

 
 
 
  
   
Zhang San
  24
 



Code parsing: introduce external DTD documents in the second line to determine whether XML is legal. The path used is relative path. the DTD introduced in many XML files on the Internet is a URI. whether it is relative or absolute path, it is feasible to find the corresponding DTD in XML.

Test. dtd file code

 
 
 
 



Code parsing: Lines 1 to 3 define the elements in the XML file and the relationship between elements. Row 4 defines the content restricted by sex in the student attribute. the default value is man and only two values can be selected: man or woman.

Verify the validity of XML as follows:



Package ValidateXml; import java. io. fileNotFoundException; import java. io. IOException; import javax. xml. parsers. documentBuilder; import javax. xml. parsers. documentBuilderFactory; import javax. xml. parsers. parserConfigurationException; import org. xml. sax. inputSource; import org. xml. sax. SAXException; public class ValidateXMLDTD {public static void main (String [] args) {// test1XML (); test2XML ();} public stati C void test1XML () {try {InputSource ips = new InputSource (); ips. setSystemId ("d: \ test. xml "); DocumentBuilderFactory dbf = DocumentBuilderFactory. newInstance (); dbf. setValidating (true); DocumentBuilder db = dbf. newDocumentBuilder (); db. parse (ips); System. out. println ("xml is correct! ");} Catch (FileNotFoundException e) {e. printStackTrace ();} catch (ParserConfigurationException e) {e. printStackTrace ();} catch (SAXException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}} public static void test2XML () {try {DocumentBuilderFactory dbf = DocumentBuilderFactory. newInstance (); dbf. setValidating (true); DocumentBuilder db = dbf. newDocumentBuilder (); Db. parse (new java. io. FileInputStream ("d: \ test. xml"); System. out. println ("xml is correct! ");} Catch (FileNotFoundException e) {e. printStackTrace ();} catch (ParserConfigurationException e) {e. printStackTrace ();} catch (SAXException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}}



Code parsing: the code above verifies whether the XML is legal. Note that you should not directly read the XML into the input stream. in this case, the DTD in the relative path cannot be found, when test2XML is called, the following error is reported. if test1XML is called, the XML is verified correctly.

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.