Directory structure:
1 What is XML
XML (eXtensible markup language) is an extensible markup language, even The language of labels can be customized.
2 Parsing XML
2.1 Two ways of parsing
XML parsing is divided into two ways, namely SAX and DOM.
DOM: (Document Object Model) is a way of processing XML recommended by the W3C organization. Using this method to parse XML documents will construct a tree structure in memory according to the hierarchical relationship between all elements in the document. Therefore, it puts a lot of pressure on memory and is slow to parse and read. The advantage is that it can traverse and modify the contents of nodes.
SAX: (Simple API for XML) is an alternative to XML parsing. Compared with DOM, the parsing speed is faster and the memory pressure is less; the disadvantage is that the content of the node cannot be modified.
2.2 Use dom4j to parse XML
Before using dom4j to parse XML, you need to import relevant tool packages, such as the author's: dom4j-1.6.1.jar Package
2.2.1 dom4j API
//创建SAXReader,是dom4j包提供的解析器SAXReader reader=new SAXReader();//读取指定的文件Document doc=reader.read(new File(filename)); Document Document getRootElement() 用于获取根元素 Element Element element(String name) 获取元素下指定名称的子元素 List<element> elements() 获取元素下所有的子元素 String getName() 获取元素名 String getText() 获取元素文本内容 String elementText(String name) 获取子元素文本内容 Attribute attribute(String) 获取元素的属性 String attributeValue(String name) 获取元素的属性值 Attribute String getName() 获取属性的名字 String getValue() 获取属性的值</element>
2.2.2 Print the entire contents of an XML file
The pricties.xml file is located directly under the project


<?xml version="1.0" encoding="utf-8" ?><books> <book><name>三国演绎</name><author>罗贯中</author><price>58.8</price> </book> <book><name>水浒传</name><author>施耐庵</author><price>49.8</price> </book> <book><name>西游记</name><author>吴承恩</author><price>100.1</price><order>1</order> </book></books>


import java.io.File;import java.util.List;import org.dom4j.Attribute;import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.io.SAXReader;public class ParseXML {public static void main(String[] args) {//创建SAXReader对象SAXReader saxr=new SAXReader(); Document docu=null;try{//读取指定的文件,相对于项目路径docu=saxr.read(new File("pricties.xml"));//获得元素的文件的根节点Element e=docu.getRootElement(); searchAllElement(e); }catch(Exception e){ e.printStackTrace(); } } public static void searchAllElement(Element e){//获得当前元素下的所有子元素,并存储到集合中List<element> elements=e.elements(); System.out.print(" atrs=e.attributes();//打印该标记下的所有属性for(Attribute att:atrs){ System.out.print(" "+att.getName()+"=\""+att.getValue()+"\""); } System.out.println(">"); //如果集合的大小为0,表示该集合下没有子元素了if(elements.size()==0){ System.out.println(e.getText());//打印文本信息System.out.println(""+e.getName()+">");//打印结束标记return;//退出当前层方法 } //递归每一个子元素for(Element ele:elements){ searchAllElement(ele); } System.out.println(""+e.getName()+">");//打印结束标记 } }</element>
2.3 Apply XPath to parse XML in dom4j
First, you need to introduce the corresponding jar package based on dom4j, such as the reader's: jaxen-1.1-beta-6. jar
2.3.1 XPath API
Document List<node> selectNodes(String xpath) Node selectSingleNode(String xpath)</node>
2.3.2 XPath的路径表达式
2.3.2.1 XPath的路径表达式规则
2.3.2.2 XPath的路径表达式应用案例
2.3.3 通配符
2.3.3.1 通配符规则
2.3.3.2 通配符应用案例
2.3.4 谓语
2.3.4.1 谓语规则
谓语是用来查找某个特定的节点或是包含某个指定的值的节点
谓语被嵌在方括号中
2.3.4.2 谓语应用案例
3 java写XML文件
3.1 将一个带有书籍信息的List集合解析为XML文件


package com.xdl.xml;public class Book {private String name;private String author;private String price;public Book() {super(); }public Book(String name, String author, String price) {super(); setName(name); setAuthor(author); setPrice(price); }/** * @return the name */public String getName() {return name; }/** * @param name the name to set */public void setName(String name) {this.name = name; }/** * @return the author */public String getAuthor() {return author; }/** * @param author the author to set */public void setAuthor(String author) {this.author = author; }/** * @return the price */public String getPrice() {return price; }/** * @param price the price to set */public void setPrice(String price) {this.price = price; } }


package com.xdl.xml;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.dom4j.Document;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.io.XMLWriter;public class WriteXML {public static void main(String[] args) {//创建一个Book集合用于存储书籍信息List<book> list_books=new ArrayList<book>();//插入书籍信息for(int i=0;i</book></book>
4 Schema和DTD的区别
Schema是对XML文档结构的定义和描述,其主要的作用是用来约束XML文件,并验证XML文件有效性。DTD的作用是定义XML的合法构建模块,它使用一系列的合法元素来定义文档结构。它们之间的区别有下面几点:
1、Schema本身也是XML文档,DTD定义跟XML没有什么关系,Schema在理解和实际应用有很多的好处。
2、DTD文档的结构是“平铺型”的,如果定义复杂的XML文档,很难把握各元素之间的嵌套关系;Schema文档结构性强,各元素之间的嵌套关系非常直观。
3、DTD只能指定元素含有文本,不能定义元素文本的具体类型,如字符型、整型、日期型、自定义类型等。Schema在这方面比DTD强大。
4、Schema支持元素节点顺序的描述,DTD没有提供无序情况的描述,要定义无序必需穷举排列的所有情况。Schema可以利用xs:all来表示无序的情况。
5、对命名空间的支持。DTD无法利用XML的命名空间,Schema很好满足命名空间。并且,Schema还提供了include和import两种引用命名空间的方法。
5 参考文章
Schema和DTD的区别
The above is the detailed content of What is XML? Example explanation of xml. For more information, please follow other related articles on the PHP Chinese website!

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.

The steps to build an RSSfeed using XML are as follows: 1. Create the root element and set the version; 2. Add the channel element and its basic information; 3. Add the entry element, including the title, link and description; 4. Convert the XML structure to a string and output it. With these steps, you can create a valid RSSfeed from scratch and enhance its functionality by adding additional elements such as release date and author information.

The steps to create an RSS document are as follows: 1. Write in XML format, with the root element, including the elements. 2. Add, etc. elements to describe channel information. 3. Add elements, each representing a content entry, including,,,,,,,,,,,. 4. Optionally add and elements to enrich the content. 5. Ensure the XML format is correct, use online tools to verify, optimize performance and keep content updated.

The core role of XML in RSS is to provide a standardized and flexible data format. 1. The structure and markup language characteristics of XML make it suitable for data exchange and storage. 2. RSS uses XML to create a standardized format to facilitate content sharing. 3. The application of XML in RSS includes elements that define feed content, such as title and release date. 4. Advantages include standardization and scalability, and challenges include document verbose and strict syntax requirements. 5. Best practices include validating XML validity, keeping it simple, using CDATA, and regularly updating.

RSSfeedsareXMLdocumentsusedforcontentaggregationanddistribution.Totransformthemintoreadablecontent:1)ParsetheXMLusinglibrarieslikefeedparserinPython.2)HandledifferentRSSversionsandpotentialparsingerrors.3)Transformthedataintouser-friendlyformatsliket

JSONFeed is a JSON-based RSS alternative that has its advantages simplicity and ease of use. 1) JSONFeed uses JSON format, which is easy to generate and parse. 2) It supports dynamic generation and is suitable for modern web development. 3) Using JSONFeed can improve content management efficiency and user experience.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment