写在最开始:这个XML系列博客是我本学期学习Web Database这门课的课程笔记,主要参考课件以及w3schools系列教程:https://www.w3schools.com/xml 。

------------------------------------------------------------------------------分割线

1. XML Syntax

1)XML Docments Must Have a Root

<root></root>,而这个root并不一定叫“root”,而是第一个tag。

2)Prolog,注意由于prolog没有closing tag,所以它不算是XML document的一部分。

<?xml version="1.0" encoding="UTF-8"?>

prolog行可以有也可以没有,但是有的话必须在第一行。它指出了所用的字符编码方法,如UTF-8(XML的默认)。

3)所有element一定要有closing tag。请注意第二行的self-closing tag。

<p>This is a paragraph.</p>
<br />

4)Tags are Case Sensitive

大写的Letter和小写的letter不是一个tag,而作为一对的opening tag和closing tag要一致。

5)所有element必须正确地nested(2个tags里外对应),不能出现在HTML中调换位置的情况。

6)Attribute value要被quoted----" "

<note date="12/11/2007"><to>Tove</to><from>Jani</from>
</note>

7)Entity Reference

<小于号>大于号这种写在语法里就会出问题,引入Entity Reference,但是要注意一下,只有<和and是严格不允许的,其他用ER代替是一种好习惯。

&lt; < less than
&gt; > greater than
&amp; & ampersand 
&apos; ' apostrophe
&quot; " quotation mark

8)注释comment:

<!-- This is a comment -->

中间有两个“--”不允许

9)空格问题

XML不会把许多空格像HTML一样截短,而是保留空格。

10)XML使用line feed(LR)存储新行

2.XML Elements

1)XML Element 是指从start tag开始到end tag结束的所有内容,包括2个tag。

2)一个element可以包括,text,attributes,其他elements,或者三者的混合体。bookstore(element content)有2个element,book(element content)有4个element1个attribute,book里的每个element(text content)又有text。

<bookstore><book category="children"><title>Harry Potter</title><author>J K. Rowling</author><year>2005</year><price>29.99</price></book><book category="web"><title>Learning XML</title><author>Erik T. Ray</author><year>2003</year><price>39.95</price></book>
</bookstore>

3)Empty element,注意Empty elements can have attributes.:

<element></element>

self-closing

<element />

4)命名element的一些注意事项:来自w3school:https://www.w3schools.com/xml/xml_elements.asp:

•XML elements must follow these naming rules:

•Element names are case-sensitive

•Element names must start with a letter or underscore

•Element names cannot start with the letters “xml” (or “XML”, or “Xml”, etc)

•Element names can contain letters, digits, hyphens (-), underscores (_), and periods (.)

•Element names cannot contain spaces

•Any name can be used, no words are reserved (except xml).

•Create descriptive names, like this: <person>, <firstname>, <lastname>.

•Create short and simple names, like this: <book_title> not like this: <the_title_of_the_book>.

•Avoid "-"

•If you name something "first-name", some software may think you want to subtract "name" from "first".

•Avoid ".”

•If you name something "first.name", some software may think that "name" is a property of the object "first".

•Avoid ":”

•Colons are reserved for namespaces (more later).

•Non-English letters like éòá are perfectly legal in XML, but watch out for problems if your software doesn't support them.

Naming Styles

There are no naming styles defined for XML elements. But here are some commonly used:

Style Example Description
Lower case <firstname> All letters lower case
Upper case <FIRSTNAME> All letters upper case
Underscore <first_name> Underscore separates words
Pascal case <FirstName> Uppercase first letter in each word
Camel case <firstName> Uppercase first letter in each word except the first

If you choose a naming style, it is good to be consistent!

XML documents often have a corresponding database. A common practice is to use the naming rules of the database for the XML elements.

Camel case is a common naming rule in JavaScripts.

5)XML element是extensisble的

3.XML Attributes

1)XML Attributes Must be Quoted,一定要加“”/‘’引号,如果attribute本身有双引,外层可以用单引,或者:

<gangster name="George &quot;Shotgun&quot; Ziegler">

2)attributes vs. elements:以下二者same

<person gender="female"><firstname>Anna</firstname><lastname>Smith</lastname>
</person>
<person><gender>female</gender><firstname>Anna</firstname><lastname>Smith</lastname>
</person>

3) Attributes的注意事项

Some things to consider when using attributes are:

  • attributes cannot contain multiple values (elements can)
  • attributes cannot contain tree structures (elements can)
  • attributes are not easily expandable (for future changes)

4. XML Namespaces

1)使用前缀prefix来解决来解决命名冲突问题,在每个element的tag前加上xxx:来以示区别:

<h:table><h:tr><h:td>Apples</h:td><h:td>Bananas</h:td></h:tr>
</h:table><f:table><f:name>African Coffee Table</f:name><f:width>80</f:width><f:length>120</f:length>
</f:table>

2)xmlns Attribute

当使用prefix的时候,prefix前缀的namespace要被定义

可以在一个element开始的时候去定义一个xmlns的attribute

namespace的声明,xmlns:prefix="URI".

<root><h:table xmlns:h="http://www.w3.org/TR/html4/"><h:tr><h:td>Apples</h:td><h:td>Bananas</h:td></h:tr>
</h:table><f:table xmlns:f="https://www.w3schools.com/furniture"><f:name>African Coffee Table</f:name><f:width>80</f:width><f:length>120</f:length>
</f:table></root>

还可以在root element中被定义:

<root xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="https://www.w3schools.com/furniture"><h:table><h:tr><h:td>Apples</h:td><h:td>Bananas</h:td></h:tr>
</h:table><f:table><f:name>African Coffee Table</f:name><f:width>80</f:width><f:length>120</f:length>
</f:table></root>

The purpose of using an URI is to give the namespace a unique name.

However, companies often use the namespace as a pointer to a web page containing namespace information.

3)URI: Uniform Resource Identifier

The most common URI is the Uniform Resource Locator (URL) which identifies an Internet domain address. Another, not so common type of URI is the Uniform Resource Name (URN).

Default Namespace:

xmlns="namespaceURI"注意没有prefix

This XML carries information about a piece of furniture:

<table xmlns="https://www.w3schools.com/furniture"><name>African Coffee Table</name><width>80</width><length>120</length>
</table>

5.DTD:经过DTD以后不光是没有语法错误的well-formed,还是valid

1)DTD可以定义一个XML document的格式,里面有一个legal的清单:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "Note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<!DOCTYPE note
[
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
  • !DOCTYPE note defines that the root element of the document is note
  • !ELEMENT note defines that the note element must contain the elements: "to, from, heading, body"
  • !ELEMENT to defines the to element to be of type "#PCDATA"
  • !ELEMENT from defines the from element to be of type "#PCDATA"
  • !ELEMENT heading defines the heading element to be of type "#PCDATA"
  • !ELEMENT body defines the body element to be of type "#PCDATA"

PCDATA means parsed character data,•PCDATA is text that WILL be parsed by a parserThe text will be examined by the parser for entities and markup.;•CDATA means character data.CDATA is text that will NOT be parsed by a parser.

2)使用DTD做实体(entity)的声明:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE note [
<!ENTITY nbsp " ">
<!ENTITY writer "Writer: Donald Duck.">
<!ENTITY copyright "Copyright: W3Schools.">
]><note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
<footer>&writer;&nbsp;&copyright;</footer>
</note>

3)使用或者不适用DTD的原因:

When to Use a DTD/Schema?

With a DTD, independent groups of people can agree to use a standard DTD for interchanging data.

With a DTD, you can verify that the data you receive from the outside world is valid.

You can also use a DTD to verify your own data.

If you want to study DTD, please read our DTD Tutorial.


When NOT to Use a DTD/Schema?

XML does not require a DTD/Schema.

When you are experimenting with XML, or when you are working with small XML files, creating DTDs may be a waste of time.

If you develop applications, wait until the specification is stable before you add a document definition. Otherwise, your software might stop working because of validation errors.

4)

  • Elements
  • Attributes
  • Entities
  • PCDATA
  • CDATA

Elements are the main building blocks of both XML and HTML documents.

Attributes provide extra information about elements.

Some characters have a special meaning in XML, like the less than sign (<) that defines the start of an XML tag:

Entity References Character
&lt; <
&gt; >
&amp; &
&quot; "
&apos; '

5)DTD elements在不同情况下的声明:

<!ELEMENT element-name category>
or
<!ELEMENT element-name (element-content)>
<!ELEMENT element-name EMPTY>Example:<!ELEMENT br EMPTY>XML example:<br />
<!ELEMENT element-name (#PCDATA)>Example:<!ELEMENT from (#PCDATA)>
<!ELEMENT element-name ANY>Example:<!ELEMENT note ANY>
<!ELEMENT element-name (child1)>
or
<!ELEMENT element-name (child1,child2,...)>Example:<!ELEMENT note (to,from,heading,body)>

注意;In a full declaration, the children must also be declared, and the children can also have children.

<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

occur once  inside the "note" element

<!ELEMENT element-name (child-name)>Example:<!ELEMENT note (message)>

occur one or more times

<!ELEMENT element-name (child-name+)>Example:<!ELEMENT note (message+)>

can occur zero or more times

<!ELEMENT element-name (child-name*)>Example:<!ELEMENT note (message*)>

zero or one time

<!ELEMENT element-name (child-name?)>Example:<!ELEMENT note (message?)>

must contain a "to" element, a "from" element, a "header" element, and either a "message" or a "body" element.

<!ELEMENT note (to,from,header,(message|body))>

the "note" element can contain zero or more occurrences of parsed character data, "to", "from", "header", or "message" elements.

<!ELEMENT note (#PCDATA|to|from|header|message)*>

6)DTD Attribute的声明:

<!ATTLIST element-name attribute-name attribute-type attribute-value>DTD example:<!ATTLIST payment type CDATA "check">XML example:<payment type="check" />

attribute-type:

Type Description
CDATA The value is character data
(en1|en2|..) The value must be one from an enumerated list
ID The value is a unique id
IDREF The value is the id of another element
IDREFS The value is a list of other ids
NMTOKEN The value is a valid XML name
NMTOKENS The value is a list of valid XML names
ENTITY The value is an entity
ENTITIES The value is a list of entities
NOTATION The value is a name of a notation
xml: The value is a predefined xml value

attribute-value:

Value Explanation
value The default value of the attribute
#REQUIRED The attribute is required
#IMPLIED The attribute is optional
#FIXED value The attribute value is fixed

Enumerative Atttibutes:

<!ATTLIST element-name attribute-name (en1|en2|..) default-value>
DTD:
<!ATTLIST payment type (check|cash) "cash">XML example:
<payment type="check" />
or
<payment type="cash" />

7)避免使用attributes的原因:

  • attributes cannot contain multiple values (child elements can)
  • attributes are not easily expandable (for future changes)
  • attributes cannot describe structures (child elements can)
  • attributes are more difficult to manipulate by program code
  • attribute values are not easy to test against a DTD

6.JSON:JSON stands for JavaScript Object Notation.

1)

JSON is a lightweight data-interaction format.

JSON is “self-describing” and easy to understand.

JSON is text based and language independent.

The JSON format is almost identical to JavaScript objects

2){"surname":"John"},Keys must be strings不能是array,但value可以是array类型可以混合, written with double quotes.

Data is in name/value pairs

Data is separated by commas

Curly bracekets hold objects

Square brackets hold arrays

3)JSON可以XML互换;

Both JSON and XML are "self-describing" (human readable)

Both JSON and XML are hierarchical (values within values)

Both JSON and XML can be parsed and used by lots of programming languages

JSON doesn't use end tag

JSON is shorter

JSON is quicker to read and write

Sem 2---Web Database---XML学习笔记[2]相关推荐

  1. XML学习笔记01【xml_基础、xml_约束】

    Java后端 学习路线 笔记汇总表[黑马程序员] XML学习笔记01[xml_基础.xml_约束][day01] XML学习笔记02[xml_解析][day01] 目录 01 xml_基础 今日内容 ...

  2. XML学习笔记02【xml_解析】

    Java后端 学习路线 笔记汇总表[黑马程序员] XML学习笔记01[xml_基础.xml_约束][day01] XML学习笔记02[xml_解析][day01] 目录 03 xml_解析 xml_解 ...

  3. XML学习笔记(1)

    XML学习笔记(1) 陈保权 2006-2-27 定义:XML:可扩展标记语言,可扩展表现在我们可以自已定义标记不像HTML那样只能用人家定义好的标记,另外XML对结构化数据由很好的表现,便于软件分析 ...

  4. XML学习笔记之XML的简介

    最近,自学了一段时间xml,希望通过学习笔记的整理能够巩固一下知识点,也希望把知识分享给你们(描红字段为重点): XML(extensible Markup language):可扩展的标记语言,解决 ...

  5. XML学习笔记(二)-- DTD格式规范

    标签(空格分隔): 学习笔记 XML的一个主要目的是允许应用程序之间自由交换结构化的数据,因此要求XML文档具有一致的结构.业务逻辑和规则.可以定义一种模式来定义XML文档的结构,并借此验证XML文档 ...

  6. 【web编程技术学习笔记】因特网与万维网简介

    目录 Client客户端 Server服务器端 TCP/IP五层协议 IP 查看IP地址的的两种方法 TCP URL 样式一 样式二 DNS URL&DNS HTTP 与因特网有关的组织 IE ...

  7. MAVEN配置文件Setting.xml学习笔记

    题记:对于maven一直是"拿来主义",但是遇到问题就有点傻逼了,今天就遇到一个maven打包的缺少插件的问题,于是整理一片学习笔记.2020.3.25 一.Setting.xml ...

  8. html调用js函数_Java Web初学者探索学习笔记10—网络API的js数据接口调用解决方案...

    声明:本学习笔记内容均为小蔡蜀黍亲自整理和原创,如需借引,请注明出处! 1. 时间戳转化为时间 1.1 时间转换函数准备 1.2 时间函数的具体使用 2. 远程js函数调用 调用的js为天天基金网的实 ...

  9. 【web搜索】学习笔记-层次汇合聚类HAC算法

    层次汇合聚类(Hierarchical Agglomerative Clustering,HAC) 算法如下: 相关链接: 层次聚类算法原理及实现 学习笔记: 先附上上一段链接中的代码: // HAC ...

  10. web之html学习笔记

     目录 1.html定义 2.video标签 3.下载资源及audio标签 4.img标签 5.标签.超链接 6:h标签 7:meta标签 8:div标签: 9:span标签 10:pre标签 11: ...

最新文章

  1. nginx配置websocket代理
  2. LeetCode算法题2:求字符串b在字符串a中的起始下标
  3. 邮件系统三功能 建金字塔防护体系
  4. JavaScript高级之ES5 中的新增方法
  5. 奖励超10万!交大超牛本科生:成果达博士毕业水平,如今保研国家重点实验室...
  6. ie浏览器速度提升设置 关闭网页多媒体方法
  7. 关键系统的JVM参数推荐
  8. jquery控制左右箭头滚动图片列表
  9. 用于函数优化的一维 (1D) 测试函数
  10. unity描边发光shader_Unity Shader 边缘高亮、描边
  11. 宋宝华: 文件读写(BIO)波澜壮阔的一生
  12. ABBYY2022PDF个人版
  13. C/C++求职者必备的20道面试题
  14. c++笔试题(带答案)值得我们一看
  15. 做国外Lead,你不懂这些概念就out了
  16. 链家房源数据清洗和预处理(pandas)
  17. python实现微信自动投票_Python——开发一个自动化微信投票器【附代码实例方法】...
  18. Css基本样式————链接
  19. 计算机用户组连接打印机,工作组链接域内共享打印机的正确姿势
  20. SSL数字证书认证的过程

热门文章

  1. 华三交换机配置ntp server
  2. 智能聊天功能——语音聊天篇
  3. Default changeset implementation allows only one operation
  4. 题解 | Birthday Reminders-2019牛客暑期多校训练营第九场F题
  5. Subspace Adversarial Training
  6. Android自定义View_绘制菱形图片
  7. 【Oracle】并行等待之PX Deq Credit: send blkd
  8. Ajax-GET请求
  9. kvm虚拟机上安装kata
  10. 发现把图片拖放到百度翻译(fanyi.baidu.com)的文本框里,就能识别出图片里的文字