分类:
2008-04-14 16:29:30
一、 简介
XML(可扩展的标注语言)是一种W3C标准,主要用于Web应用程序和服务器之间实现容易的交互、数据的存储与使用。
使用XML标准编码的数据具有能容易被人和计算机解释的意义和结构。XML数据是平台和应用程序独立的。不用多说,这本身就使XML成为适合于的一个理想的数据交换格式(事实上,它正是因这一用途而被开发的)。最近,宽带连接的增长及消费者对于越过任何媒体进行数据共享的应用软件的需求意味着,XML Web服务和应用软件正变得越来越丰富。
XML的发明正是为了解决描述网上丰富的数据的组织问题;而目前为止,这一问题仅能够通过HTML的巧妙使用得到部分地解决。
下面是一XML文档的实例:
<?xml version="1.0"?> <party> <location>My House</location> <time>7pm</time> <guest> <name>John Bloggs</name> <item>Crate of Fosters</item> </guest> <guest> <name>Sara Bloggs</name> <item>Umbrella</item> </guest> <guest> <name>David Fig</name> <item>Bombay Mix</item> </guest> </party> |
<xml version="1.0"?> <library> <categories> <category cid="1">Web Development</category> <category cid="2">Database Programming</category> <category cid="3">PHP</category> <category cid="4">Java</category> </categories> <books> <book> <title>Apache 2</title> <author>Peter Wainwright</author> <publisher>Wrox</publisher> <category>1</category> </book> <book> <title>Advanced PHP Programming</title> <author>George Schlossnagle</author> <publisher>Developer Library</publisher> <category>1</category> <category>3</category> </book> <book> <title>Visual FoxPro 6 - Programmers Guide</title> <author>Eric Stroo</author> <publisher>Microsoft Press</publisher> <category>2</category> </book> <book> <title>Mastering Java 2</title> <author>John Zukowski</author> <publisher>Sybex</publisher> <category>4</category> </book> </books> </library> |
<?php /*这里我们必须指定XML版本:也即是1.0 */ $xml = new DomDocument('1.0'); $xml->load('xml/library.xml'); /*首先,创建一个目录列表*/ $categories = array(); $XMLCategories = $xml->getElementsByTagName('categories')->item(0); foreach($XMLCategories->getElementsByTagName('category') as $categoryNode) { /*注意我们是如何得到属性的*/ $cid = $categoryNode->getAttribute('cid'); $categories[$cid] = $categoryNode->firstChild->nodeValue; } ?> <html> <head> <title>XML Library</title> </head> <body> <? php foreach($xml->getElementsBytagName('book') as $book): /*查找标题*/ $title = $book->getElementsByTagName('title')->item(0)->firstChild->nodeValue; /*查找作者-为了简化起见,我们假设仅仅有一个作者*/ $author = $book->getElementsByTagName('author')->item(0)->firstChild->nodeValue; /* 列表目录*/ $bookCategories = $book->getElementsByTagName('category'); $catList = ''; foreach($bookCategories as $category) { $catList .= $categories[$category->firstChild->nodeValue] . ', '; } $catList = substr($catList, 0, -2); ?> <div> <h2><?php echo($title) ?></h2> <p><b>Author:</b>: <?php echo($author) ?></p> <p><b>Categories: </b>: <?php echo($catList) ?></p> </div> <? php endforeach; ?> </html> |
function addCategory(DOMDocument $xml, $catID, $catName) { $catName = $xml->createTextNode($catName); //创建一个结点以存储文本 $category = $xml->createElement('category'); //创建一个目录元素 $category->appendChild($catName); //把文本添加到目录元素上 $category->setAttribute('cid', $catID); //设置目录的ID $XMLCategories = $xml->getElementsByTagName('categories')->item(0); $XMLCategories->appendChild($category); //添加新目录 } |
$xml->save('xml/library.xml'); //保存全部文件 $categories=$xml->saveXML($XMLCategories); //返回一个包含种类的字符串 |
Javascript: function doXML(){ /* 首先创建一个种类列表*/ var categories = Array(); var XMLCategories = xml.getElementsByTagName('categories')[0]; var theCategories = XMLCategories.getElementsByTagName('category'); for (var i = 0; i < theCategories.length; i++) { /* 注意我们是怎样得到属性的*/ var cid = theCategories[i].getAttribute('cid'); categories[cid] = theCategories[i].firstChild.nodeValue; } var theBooks = xml.getElementsByTagName('book'); for(var i = 0; i < theBooks.length; i++) { var book = theBooks[i]; /* 查找标题*/ var title = book.getElementsByTagName('title')[0].firstChild.nodeValue; /* 查找作者-为简单起见,我们假定仅有一个作者*/ var author = book.getElementsByTagName('author')[0].firstChild.nodeValue; /* 列出种类*/ var bookCategories = book.getElementsByTagName('category'); var catList = ''; for(var j = 0; j < bookCategories.length; j++) { catList += categories[bookCategories[j].firstChild.nodeValue] + ', '; } catList = catList.substring(0, catList.length -2); document.open(); document.write("<h2>" + title + "</h2>"); document.write("<p><b>Author:</b>: " + author + "</p>"); document.write("<p><b>Categories: </b>: " + catList + "</p>"); } document.close(); } |
$xml->books;//返回元素"books" $xml->books->book[0];//返回在books元素中的第一本书 |
$category['cid'];//返回cid属性的值 |
echo ($xml->books->book[0]->title);//显示第一本书的标题 |
<?php $xml = simplexml_load_file('xml/library.xml'); /* 把一个列表的目录装载到一个数组中*/ $categories = array(); foreach($xml->categories->category as $category) { $categories[(string) $category['cid']] = (string) $category; } ?> <html> <head> <title>XML Library</title> </head> <body> <?php foreach($xml->books->book as $book): /* 列举目录*/ $catList = ''; foreach($book->category as $category) { $catList .= $categories[((string) $category)] . ', '; } $catList = substr($catList, 0, -2); ?> <div> <h2><?php echo($book->title) ?></h2> <p><b>Author:</b>: <?php echo($book->author) ?></p> <p><b>Categories: </b>: <? php echo($catList) ?></p> </div> <? php endforeach; ?> </html> |
function addCategory(SimpleXMLElement &$sXML, $catID, $catName) { $xml = new DOMDocument; $xml->loadXML($sXML->asXML()); $catName = $xml->createTextNode($catName); //创建一个结点来存放该文本 $category = $xml->createElement('category'); //创建一个目录元素 $category->appendChild($catName); //把文本添加到目录元素 $category->setAttribute('cid', $catID); //设置目录id $XMLCategories = $xml->getElementsByTagName('categories')->item(0); $XMLCategories->appendChild($category); //添加新目录 $sXML = simplexml_import_dom($xml); return $sXML; } |
<?php $xml = simplexml_load_file('xml/library.xml'); ?> <html> <head> <title>XML Library</title> </head> <body> <?php foreach(((array)$xml->xpath("/library/books/book")) as $book): /*列表目录*/ $catList = ''; foreach($book->category as $category) { /*得到具有这个ID的目录*/ $category = $xml->xpath("/library/categories/category[@cid='$category']"); $catList .= (string) $category[0] . ', '; } $catList = substr($catList, 0, -2); ?> <div> <h2><?php echo($book->title) ?></h2> <p><b>Author:</b>: <?php echo($book->author) ?></p> <p><b>Categories: </b>: <?php echo($catList) ?></p> </div> <?php endforeach; ?> </html> |
$xPath = new DOMXPath($xml); $xPath->evaluate("/library/books/book[title='Apache 2']"); |