时间:2021-07-01 10:21:17 帮助过:17人阅读
<?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> http://www.bkjia.com/PHPjc/508481.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/508481.htmlTechArticle一、 XML简介 XML(可扩展的标注语言)是一种W3C标准,主要用于Web应用程序和服务器之间实现容易的交互、数据的存储与使用。 使用XML标准编... 人气教程排行
|