zoukankan      html  css  js  c++  java
  • xml 学习

    2013-02-01

    http://www.w3school.com.cn/xml/xml_tree.asp


    XML 文档必须包含根元素。该元素是所有其他元素的父元素。

    所有元素均可拥有文本内容和属性

    对应xml描述:

     1 <bookstore>
     2 <book category="COOKING">
     3   <title lang="en">Everyday Italian</title> 
     4   <author>Giada De Laurentiis</author> 
     5   <year>2005</year> 
     6   <price>30.00</price> 
     7 </book>
     8 <book category="CHILDREN">
     9   <title lang="en">Harry Potter</title> 
    10   <author>J K. Rowling</author> 
    11   <year>2005</year> 
    12   <price>29.99</price> 
    13 </book>
    14 <book category="WEB">
    15   <title lang="en">Learning XML</title> 
    16   <author>Erik T. Ray</author> 
    17   <year>2003</year> 
    18   <price>39.95</price> 
    19 </book>
    20 </bookstore>

    所有 XML 元素都须有关闭标签

    XML 的属性值须加引号

    属性值必须被引号包围,不过单引号和双引号均可使用。

    注释:如果属性值本身包含双引号,那么有必要使用单引号包围它,就像这个例子:

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

    或者可以使用实体引用:

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

    实体引用:

    &lt; < 小于
    &gt; > 大于
    &amp; & 和号
    &apos; ' 单引号
    &quot; " 引号

    xml注释:

    <!-- This is a comment -->


     

    XML 元素 vs. 属性

    请看这些例子:

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

    在第一个例子中,sex 是一个属性。在第二个例子中,sex 则是一个子元素。两个例子均可提供相同的信息。

    没有什么规矩可以告诉我们什么时候该使用属性,而什么时候该使用子元素。我的经验是在 HTML 中,属性用起来很便利,但是在 XML 中,您应该尽量避免使用属性。如果信息感觉起来很像数据,那么请使用子元素吧。

    xml推荐使用方式
     1 <note>
     2 <date>
     3   <day>08</day>
     4   <month>08</month>
     5   <year>2008</year>
     6 </date>
     7 <to>George</to>
     8 <from>John</from>
     9 <heading>Reminder</heading>
    10 <body>Don't forget the meeting!</body>
    11 </note>

    请尽量使用元素来描述数据。而仅仅使用属性来提供与数据无关的信息。

    在此我们极力向您传递的理念是:元数据(有关数据的数据)应当存储为属性,而数据本身应当存储为元素。

    XML 以 LF 存储换行

    在 Windows 应用程序中,换行通常以一对字符来存储:回车符 (CR) 和换行符 (LF)。这对字符与打字机设置新行的动作有相似之处。在 Unix 应用程序中,新行以 LF 字符存储。而 Macintosh 应用程序使用 CR 来存储新行。

    “形式良好”或“结构良好”的 XML 文档拥有正确的语法。

    “形式良好”(Well Formed)的 XML 文档会遵守前几章介绍过的 XML 语法规则:

    • XML 文档必须有根元素
    • XML 文档必须有关闭标签
    • XML 标签对大小写敏感
    • XML 元素必须被正确的嵌套
    • XML 属性必须加引号

     

     

    某些文本,比如 JavaScript 代码,包含大量 "<" 或 "&" 字符。为了避免错误,可以将脚本代码定义为 CDATA。

    CDATA 部分中的所有内容都会被解析器忽略。

    CDATA 部分由 "<![CDATA[" 开始,由 "]]>" 结束:

    <script>
    <![CDATA[
    function matchwo(a,b)
    {
    if (a < b && a < 0) then
      {
      return 1;
      }
    else
      {
      return 0;
      }
    }
    ]]>
    </script>

     2013年2月25日 16:26:02 继续

    有点理解了
     1  XmlDocument xmlDoc = new XmlDocument();
     2             XmlElement root = null, theBook=null, theElemet = null;
     3             try
     4             {
     5                 xmlDoc.Load("book.xml");
     6                 root = xmlDoc.DocumentElement;
     7                 //textBox1.Text = root.OuterXml;
     8                 theBook = (XmlElement)root.SelectSingleNode("/books/book[name='哈里波特']");
     9                 textBox1.Text = theBook.OuterXml;
    10 
    11                 theBook = xmlDoc.CreateElement("bok");
    12                 theElemet = xmlDoc.CreateElement("name");
    13                 theElemet.InnerText = "不知道";
    14                 theBook.AppendChild(theElemet);
    15                 root.AppendChild(theBook);

     

     

     

     

  • 相关阅读:
    交友app
    xcode5修改APP名字
    xcode5下取消ARC
    五语言学习系列 C,C++,Objective-C,Java,C# (一)历史
    IT领域的罗马帝国——微软公司
    IBOutlet loadView UIButton的subview数量 UIWebView
    UIColor用自定义颜色,TableView去掉背景色
    ORA-00937: not a single-group group function
    向一张表批量插入值
    lsnrctl start 报错
  • 原文地址:https://www.cnblogs.com/zhiying678/p/2889016.html
Copyright © 2011-2022 走看看