一.使用DOM生成和读取XML文件
实例一:
<BR><?php <BR>//Creates XML string and XML document using the DOM <BR>$dom = new DomDocument('1.0'); <BR>//add root - <BR>$books = $dom->appendChild($dom->createElement_x_x ('books')); <BR>//add element to <BR>$book = $books->appendChild($dom->createElement_x_x ('book')); <BR>//add <title> element to <BR>$title = $book->appendChild($dom->createElement_x_x ('title')); <BR>//add <title> text node element to <title> <BR>$title->appendChild($dom->createTextNode('Great American Novel')); <BR>//generate xml <BR>$dom->formatOutput = true; // set the formatOutput attribute of domDocument to true <BR>//save XML as string or file <BR>$test1 = $dom->saveXML(); // put string in test1 <BR>$dom -> save('test1.xml'); // save as file <BR>?> <BR>
实例二:
<BR>$aa = "111"; <BR>$xmlstr = <<<XML <BR><?xml version='1.0'?> <BR> <BR><title>{$aa}</title> <BR>Joe <BR>Jane <BR><body> <BR>I know that's the answer -- but what's the question? <BR> <BR> <BR>XML; <BR>$dom = new domDocument; <BR>$dom->loadXML($xmlstr); <BR>$test1 = $dom->saveXML(); <BR>$dom->save('test1.xml'); <BR>
实例三:
test1.xml:
<BR><?xml version="1.0"?> <BR> <BR> <BR>Jack Herrington <BR><title>PHP Hacks</title> <BR>O'Reilly <BR> <BR> <BR>Jack Herrington <BR><title>Podcasting Hacks</title> <BR>O'Reilly <BR> <BR> <BR>
example.php:
<BR>$doc = new DOMDocument(); <BR>$doc->load('test1.xml'); <BR>$books = $doc->getElementsByTagName("book"); <BR>foreach($books as $book){ <BR>$authors = $book->getElementsByTagName("author"); <BR>$author = $authors->item(0)->nodeValue; <BR>$publishers =<strong style="color:transparent">本文来源gao@daima#com搞(%代@#码@网&</strong><strong>搞gaodaima代码</strong> $book->getElementsByTagName( "publisher" ); <BR>$publisher = $publishers->item(0)->nodeValue; <BR>$titles = $book->getElementsByTagName( "title" ); <BR>$title = $titles->item(0)->nodeValue; <BR>echo "$title - $author - $publisher\n"; <BR>} <BR>
二.使用simple生成和读取xml文件
实例一:
<BR><? <BR>$xmlstr = <<<XML <BR><?xml version='1.0' standalone='yes'?> <BR> <BR> <BR><title>Great American Novel</title> <BR> <BR> <BR>Cliff <BR>really great guy <BR> <BR> <BR>Lovely Woman <BR>matchless beauty <BR> <BR> <BR>Loyal Dog <BR>sleepy <BR> <BR> <BR> <BR>Cliff meets Lovely Woman. Loyal Dog sleeps, but wakes up to bark <BR>at mailman. <BR> <BR>4 <BR>9 <BR> <BR> <BR>XML; <br><br>//提取节点内容 <BR>$xml = new SimpleXMLElement($xmlstr); <BR>foreach ($xml->book[0]->success as $success) { <BR>switch((string) $success['type']) { // Get attributes as element indices <BR>case 'bestseller': <BR>echo $success. ' months on bestseller list<br>'; <BR>break; <BR>case 'bookclubs': <BR>echo $success. ' bookclub listings'; <BR>break; <BR>} <BR>} <br><br>//修改文本节点内容 <BR>$xml = new SimpleXMLElement($xmlstr); <BR>$xml->book[0]->characters->character[0]->name = 'Big Cliff'; <BR>echo $xml->asXML(); <br><br>//添加子元素的文本节点 <BR>$xml = new SimpleXMLElement($xmlstr); <BR>$character = $xml->book[0]->characters->addChild('character'); <BR>$character->addChild('name', 'Yellow Cat'); <BR>$character->addChild('desc', 'aloof'); <BR>$success = $xml->book[0]->addChild('success', '2'); <BR>$success->addAttribute('type', 'reprints'); <BR>echo $xml->asXML(); <br><br>?> <BR>
实例二:
<BR>if (file_exists('test1.xml')) { //读取xml文件 <BR>$xml = simplexml_load_file('test1.xml'); <BR>var_dump(xml); <BR>} else { <BR>exit('Failed to open test1.xml.'); <BR>} <BR>
三.DOM和simple互操作
DOM导入simpleXML:
<BR><?php <BR>$sxe = simplexml_load_string('<title>Great American <BR>Novel</title>'); <BR>if ($sxe === false) { <BR>echo 'Error while parsing the document'; <BR>exit; <BR>} <BR>$dom_sxe = dom_import_simplexml($sxe); <BR>if (!$dom_sxe) { <BR>echo 'Error while converting XML'; <BR>exit; <BR>} <BR>$dom = new DOMDocument('1.0'); <BR>$dom_sxe = $dom->importNode($dom_sxe, true); <BR>$dom_sxe = $dom->appendChild($dom_sxe); <BR>$test2 = $dom->saveXML(); // put string in test2 <BR>$dom -> save('test2.xml'); // save as file <BR>?> <BR>
simpleXML导入DOM:
<BR><?php <BR>$dom = new domDocument; <BR>$dom->loadXML('<title>Great American <BR>Novel</title>'); <BR>if (!$dom) { <BR>echo 'Error while parsing the document'; <BR>exit; <BR>} <BR>$s = simplexml_import_dom($dom); <BR>echo $s->book[0]->title; // Great American Novel <BR>?> <BR>