XML Tutorial

7. Describing Data with XML

<BusinessCard>
    <name>Joe Marini</name>
    <phone type = "home">+1 (415) 555-1234</phone>
    <phone type = "work">+1 (415) 555-2222</phone>
    <email>joe@gmail.com</email>
</BusinessCard>

8. Advantages and drawabacks of XML

Advantages

  • XML keeps content separate from presentation
  • XML is an open format that can be read by many applications
  • XML can be used on both the client and the server
  • XML has widespread support in multiple languages, runtimes
  • XML makes it possible for disparate systems to exchange data

Drawbacks

  • XML is not suitable for a very large data sets (like 1GB)
  • Some formats like JSON might be better in some cases
  • Some types like images aren’t represented well
  • XML can quickly get hard to read when complex

9. Real world examples of XML

  • RSS pages
  • Microsoft Office can aslo support XML

10. Types of XML content

  • XML document declaration (optional but recommended)
    • standalone: whether the document is complete by itself
<?xml version="1.0" encoding="UTF-8" standalone="yes">
  • Elements (Tags) and Attributes

    • attribute appears only once in the opening element
<element attribute="value">
  • Comments
<!-- This is a comment -->
  • Character Data

    • It’s part of document but not parsed by XML parser. Typically used to contain unescaped textual data
<![CDATA[This is text & data]]>

<!-- Example -->
<script>
<![CDATA[
    function (a,b) {
        return a < b;
    }
]]>
</script>
  • Processing Instructions
    • They are special instructions to the XML parser.
<?SpellCheckMode mode="us-english"?>
  • Entity References
    • Help shorten and modularize XML documents
    • Provide markup for otherwise illegal characters
    • General entities:
      • Replaced by parser with a full string
      • Example: &copyright; or &author
    • Character entities if the character is not on the keyboard
      • &#060;
Character (&#60;) and General (&copyright)

11. Proper XML syntax

  • XML documents must have a single root tag
  • XML documents must be “well-formed”
  • Empty tags must be closed with />
    • Instead of <elem> </elem>, use <elem/>
  • Attriute values can’t be minimized
    • <option selected> is illegal, use <option selected="selected">
  • Attribute values must be inside of quotes, single or double
    • <elem attr=val> is illegal, use <elem attr="val">

12. Valid XML documents

INstead of only checking whether the the XML document is “well-formed” or not, we can also apply some rules to the parser like “tag A must be inside the tag B” and so on. If the document passes the rules and that’s called valid.

There are two ways to do this:

  1. Document type definition (DTD)
    1. simple to use but not powerful
    2. written using a syntax that’s different that XML
  2. XML Schema
    1. more powerful and flexible that DTD

13. XML namespaces

Form

<html xmlns="http://www.w3.org/1999/xhtml">

<!-- Example -->
<table>
    <tr>
        <td>Cell Content</td>
    </tr>
    <tr>
        <td>Cell Content</td>
    </tr>
</table>

<table>
    <type>Coffee</type>
    <price>199.99</price>
    <material>Wood</material>
    <stock>25</stock>
</table>

<!-- Solution -->
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:furn="http://www.funitur.org/items">
<table>
    <tr><td>
        <furn:table>
            <furn:type>Coffee</furn:type>
            <furn:price>199.99</furn:price>
            <furn:material>Wood</furn:material>
            <furn:stock>25</furn:stock>
        </furn:talbe>        
    </td></tr>
</table>
  • It’s usually a URL but not necessary, and it needs to be unique
  • Prevent tags from different languages from conflicting
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值