扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:朱先忠 来源:天极开发 2007年10月21日
关键字: Linux
<?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> |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者