转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
XML Basic-from w3schools.com         ★★★★

XML Basic-from w3schools.com

作者:闵涛 文章来源:闵涛的学习笔记 点击数:4074 更新时间:2009/4/23 15:41:28
es because it is reserved to be used for something called namespaces (more later).



XML Attributes

back next

XML elements can have attributes in the start tag, just like HTML.

Attributes are used to provide additional information about elements.


XML Attributes

XML elements can have attributes.

From HTML you will remember this: <IMG SRC="computer.gif">. The SRC attribute provides additional information about the IMG element.

In HTML (and in XML) attributes provide additional information about elements:

<img src="computer.gif">
<a href="demo.asp">

Attributes often provide information that is not a part of the data. In the example below, the file type is irrelevant to the data, but important to the software that wants to manipulate the element:

<file type="gif">computer.gif</file>


Quote Styles, "female" or ''''female''''?

Attribute values must always be enclosed in quotes, but either single or double quotes can be used. For a person''''s sex, the person tag can be written like this:

<person sex="female">

or like this:

<person sex=''''female''''>

Note: If the attribute value itself contains double quotes it is necessary to use single quotes, like in this example: 

<gangster name=''''George "Shotgun" Ziegler''''>

Note: If the attribute value itself contains single quotes it is necessary to use double quotes, like in this example: 

<gangster name="George ''''Shotgun'''' Ziegler">


Use of Elements vs. Attributes

Data can be stored in child elements or in attributes.

Take a look at these examples:

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

<person>
  <sex>female</sex>
  <firstname>Anna</firstname>
  <lastname>Smith</lastname>
</person>

In the first example sex is an attribute. In the last, sex is a child element. Both examples provide the same information.

There are no rules about when to use attributes, and when to use child elements. My experience is that attributes are handy in HTML, but in XML you should try to avoid them. Use child elements if the information feels like data.


My Favorite Way

I like to store data in child elements.

The following three XML documents contain exactly the same information:

A date attribute is used in the first example:

<note date="12/11/2002">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don''''t forget me this weekend!</body>
</note>

A date element is used in the second example:

<note>
<date>12/11/2002</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don''''t forget me this weekend!</body>
</note>

An expanded date element is used in the third: (THIS IS MY FAVORITE):

<note>
<date>
  <day>12</day>
  <month>11</month>
  <year>2002</year>
</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don''''t forget me this weekend!</body>
</note> 


Avoid using attributes?

Should you avoid using attributes?

Some of the problems with using attributes are:

  • 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 Document Type Definition (DTD) - which is used to define the legal elements of an XML document

If you use attributes as containers for data, you end up with documents that are difficult to read and maintain. Try to use elements to describe data. Use attributes only to provide information that is not relevant to the data.

Don''''t end up like this ( if you think this looks like XML, you have not understood the point):

<note day="12" month="11" year="2002"
to="Tove" from="Jani" heading="Reminder" 
body="Don''''t forget me this weekend!">
</note> 


An Exception to my Attribute rule

Rules always have exceptions.

My rule about attributes has one exception:

Sometimes I assign ID references to elements. These ID references can be used to access XML elements in much the same way as the NAME or ID attributes in HTML. This example demonstrates this:

<messages>
  <note id="p501">
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don''''t forget me this weekend!</body>
  </note>

  <note id="p502">
    <to>Jani</to>
    <from>Tove</from>
    <heading>Re: Reminder</heading>
    <body>I will not!</body>
  </note> 
</messages>

The ID in these examples is just a counter, or a unique identifier, to identify the different notes in the XML file, and not a part of the note data.

What I am trying to say here is that metadata (data about data) should be stored as attributes, and that data itself should be stored as elements.



XML Validation

back next

XML with correct syntax is Well Formed XML.

XML validated against a DTD is Valid XML.


"Well Formed" XML documents

A "Well Formed" XML document has correct XML syntax.

A "Well Formed" XML document is a document that conforms to the XML syntax rules that were described in the previous chapters:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don''''t forget me this weekend!</body>
</note>


"Valid" XML documents

A "Valid" XML document also conforms to a DTD.

A "Valid" XML document is a "Well Formed" XML document, which also conforms to the rules of a Document Type Definition (DTD):

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE note SYSTEM "InternalNote.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don''''t forget me this weekend!</body>
</note> 


XML DTD

A DTD defines the legal elements of an XML document.

The purpose of a DTD is to define the legal building blocks of an XML document. It defines the document structure with a list of legal elements. You can read more about DTD, and how to validate your XML documents in our DTD tutorial.


XML Schema 

XML Schema is an XML based alternative to DTD.

W3C supports an alternative to DTD called XML Schema. You can read more about XML Schema in our Schema tutorial.


Errors will Stop you

Errors in XML documents will stop the XML program.

The W3C XML specification states that a program should not continue to process an XML document if it finds a validation error. The reason is that XML software should be easy to write, and that all XML documents should be compatible.

With HTML it was possible to create documents with lots of errors (like when you forget an end tag). One of the main reasons that HTML browsers are so big and incompatible, is that they have their own ways to figure out what a document should look like when they encounter an HTML error.

With XML this should not be possible.


A general XML Validator

To help you validate your xml files, we have created this link so that you can Validate any XML file.



XML Browser Support

prev next

In our XML tutorial we have focused on Internet Explorer 5.0+.


XML on this Web Site

Many browsers support XML. We are focusing on Internet Explorer 5.0+. 

Some of you have complained about this, but we think this is the most practical way to demonstrate real XML examples for you over the Internet.


XML in Mozilla Firefox 1.0

Mozilla Firefox 1.0 supports the XML 1.0 standard.


XML in Netscape 6

Netscape 6 supports XML.

To look at the XML source in Netscape 6: right-click on the page and select "View Page Source".


XML in Internet Explorer 5.0

Internet Explorer 5.0 supports the XML 1.0 standard.

Internet Explorer 5.0 supports most of the international standards for XML 1.0 and the XML DOM (Document Object Model). These standar

上一页  [1] [2] [3] [4] [5]  下一页


[Web开发]XML与HTML在语法上的主要区别详解  [系统软件]如何让Expat支持中文XML
[VB.NET程序]vb.net 与 XML 的操作  [VB.NET程序]vb.net读写xml(2)--实现datagrid与xml的沟通(原创…
[VB.NET程序]vb.net 读写xml方法(1)  [VB.NET程序][VB.NET+XML]完成简单程序配置
[VB.NET程序]Visual Basic 6 逆向工程与反逆向工程 (2)  [VB.NET程序]Visual Basic 6 逆向工程与反逆向工程 (1)
[VB.NET程序]用Visual Basic创建复杂窗体  [VB.NET程序]Visual Basic的类对于面向对象的支持
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台