Table of Contents
Development History
##XMLComparison with HTML Extensible
XML Syntax details compared to HTML
XML validation DTD
XMLSyntax structure
XMLNamespace
DOM4JRead and write configuration file
About SLT
XML verification DTD
As mentioned in the previous tutorial, the biggest role of XML is to store, transmit and exchange data. During this period, the correctness of XML was crucial, and corresponding measures were taken to ensure the correctness of XML. The correctness of
XML is divided into two aspects: one is the syntax of XML, and the other is the content of XML. 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 the document conforms to the XML specification. In other words, we must ensure that the XML has no grammatical errors, but The relationship between elements and whether the values of attributes are correct cannot be known. For a well-formed document, if it is only used in limited applications, such as as a configuration file in a self-developed system, or as a storage and transmission of data, it may be able to meet our application well. But if you want other users to understand or the system to use your XML document, or to exchange data, then you must ensure that the XML is "legal". In this way, it is necessary to provide an XML verification mechanism. The purpose is to ensure that the structure of the XML document we write and the XML document written by others are the same, the relationship between elements is correct, and the values of attributes are correct. is in compliance with the requirements.
This mechanism has been provided for us in the XML standard, which is the DTD (Document Type Definition) we mentioned earlier. In other words, you can verify whether your own XML is "legal" XML through DTD.
We can define DTD directly in the XML document, or introduce external DTD files through URI. Although the internal DTD is convenient, it will increase the transmission burden due to the length of the document itself, and if multiple XML documents want to share a DTD, we need to add the DTD to each document, which is quite cumbersome. Therefore, the recommended approach is to define the DTD in a separate file and reference the external DTD file through the URI in the XML document.
The following demonstrates how to use a DTD file to verify the legitimacy of an XML file
test.xml file code
<?xml version="1.0" encoding="gb2312" standalone="no"?> <!DOCTYPE student SYSTEM "test.dtd"> <!--这是XML文档--> <student> <name>张三</name> <age>24</age> </student>
代码解析:在第二行中将外部的DTD文档引入,用于判断XML是否合法。其中用的路径为相对路径,网上很多XML中引入的DTD是一个URI,无论是相对还是绝对的路径,总之只要XML能找到其对应的DTD就是可行的。
test.dtd文件代码
<!ELEMENT student (name,age)> <!ELEMENT name (#PCDATA)> <!ELEMENT age (#PCDATA)> <!ATTLIST student sex (man | woman) 'man'>
代码解析:第一行至第三行定义了XML文件中的元素,以及元素之间的关系。在第四行定义了student属性中对sex的限制内容,其默认为man而且只能选取两个值man或者woman。
下面开始验证XML的合法性:
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 static 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 正确!"); } 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 正确!"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
代码解析:上面的代码验证XML是否为合法,需要注意的是不要直接将XML读入到输入流中,那样的话会找不到相对路径下的DTD,调用test2XML会报错如下,如果调用test1XML则会正确验证XML。
直接用输入流读入的话XML寻找相对路径会在eclipse的环境下进行寻找DTD,如果用setSystemId进行设置的话会根据XML自己存在的目录中寻找DTD(参看具体解释),很显然后一种方式才是我们想要的。
通过DTD我们可以很容易的判断要验证的XML是否符合我们所定义的规范(元素之间的关系,属性的取值是否正确)但是如果要验证元素的内容DTD就无能为力了,于是人们研究了新的验证方法——Schema。就像人们远行一样,当对时间要求不苛刻的时候,火车便宜而且安全;当对时间有严格要求的时候,飞机也是不错的选择。根据实际需要改进技术,根据实际需要选择技术。量体裁衣,明智之举。
以上就是XML简明教程(4) 的内容,更多相关内容请关注PHP中文网(www.php.cn)!

The implementation of RSS in XML is to organize content through a structured XML format. 1) RSS uses XML as the data exchange format, including elements such as channel information and project list. 2) When generating RSS files, content must be organized according to specifications and published to the server for subscription. 3) RSS files can be subscribed through a reader or plug-in to automatically update the content.

Advanced features of RSS include content namespaces, extension modules, and conditional subscriptions. 1) Content namespace extends RSS functionality, 2) Extended modules such as DublinCore or iTunes to add metadata, 3) Conditional subscription filters entries based on specific conditions. These functions are implemented by adding XML elements and attributes to improve information acquisition efficiency.

RSSfeedsuseXMLtostructurecontentupdates.1)XMLprovidesahierarchicalstructurefordata.2)Theelementdefinesthefeed'sidentityandcontainselements.3)elementsrepresentindividualcontentpieces.4)RSSisextensible,allowingcustomelements.5)Bestpracticesincludeusing

RSS and XML are tools for web content management. RSS is used to publish and subscribe to content, and XML is used to store and transfer data. They work with content publishing, subscriptions, and update push. Examples of usage include RSS publishing blog posts and XML storing book information.

RSS documents are XML-based structured files used to publish and subscribe to frequently updated content. Its main functions include: 1) automated content updates, 2) content aggregation, and 3) improving browsing efficiency. Through RSSfeed, users can subscribe and get the latest information from different sources in a timely manner.

The XML structure of RSS includes: 1. XML declaration and RSS version, 2. Channel (Channel), 3. Item. These parts form the basis of RSS files, allowing users to obtain and process content information by parsing XML data.

RSSfeedsuseXMLtosyndicatecontent;parsingtheminvolvesloadingXML,navigatingitsstructure,andextractingdata.Applicationsincludebuildingnewsaggregatorsandtrackingpodcastepisodes.

RSS documents work by publishing content updates through XML files, and users subscribe and receive notifications through RSS readers. 1. Content publisher creates and updates RSS documents. 2. The RSS reader regularly accesses and parses XML files. 3. Users browse and read updated content. Example of usage: Subscribe to TechCrunch's RSS feed, just copy the link to the RSS reader.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools