用PHP編寫(xiě)和讀取XML的幾種方式
更新時(shí)間:2013年01月12日 16:31:33 作者:
今天我看了IBM的一些官方文檔和一些XML的相關(guān)資料,特把一些關(guān)鍵點(diǎn)以及PHP編寫(xiě)和讀取XML的一些實(shí)例整理出來(lái),方便以后使用
一.使用DOM生成和讀取XML文件
實(shí)例一:
<?php
//Creates XML string and XML document using the DOM
$dom = new DomDocument('1.0');
//add root - <books>
$books = $dom->appendChild($dom->createElement_x_x ('books'));
//add <book> element to <books>
$book = $books->appendChild($dom->createElement_x_x ('book'));
//add <title> element to <book>
$title = $book->appendChild($dom->createElement_x_x ('title'));
//add <title> text node element to <title>
$title->appendChild($dom->createTextNode('Great American Novel'));
//generate xml
$dom->formatOutput = true; // set the formatOutput attribute of domDocument to true
//save XML as string or file
$test1 = $dom->saveXML(); // put string in test1
$dom -> save('test1.xml'); // save as file
?>
實(shí)例二:
$aa = "111";
$xmlstr = <<<XML
<?xml version='1.0'?>
<document>
<title>{$aa}</title>
<from>Joe</from>
<to>Jane</to>
<body>
I know that's the answer -- but what's the question?
</body>
</document>
XML;
$dom = new domDocument;
$dom->loadXML($xmlstr);
$test1 = $dom->saveXML();
$dom->save('test1.xml');
實(shí)例三:
test1.xml:
<?xml version="1.0"?>
<books>
<book>
<author>Jack Herrington</author>
<title>PHP Hacks</title>
<publisher>O'Reilly</publisher>
</book>
<book>
<author>Jack Herrington</author>
<title>Podcasting Hacks</title>
<publisher>O'Reilly</publisher>
</book>
</books>
example.php:
$doc = new DOMDocument();
$doc->load('test1.xml');
$books = $doc->getElementsByTagName("book");
foreach($books as $book){
$authors = $book->getElementsByTagName("author");
$author = $authors->item(0)->nodeValue;
$publishers = $book->getElementsByTagName( "publisher" );
$publisher = $publishers->item(0)->nodeValue;
$titles = $book->getElementsByTagName( "title" );
$title = $titles->item(0)->nodeValue;
echo "$title - $author - $publisher\n";
}
二.使用simple生成和讀取xml文件
實(shí)例一:
<?
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<books>
<book>
<title>Great American Novel</title>
<characters>
<character>
<name>Cliff</name>
<desc>really great guy</desc>
</character>
<character>
<name>Lovely Woman</name>
<desc>matchless beauty</desc>
</character>
<character>
<name>Loyal Dog</name>
<desc>sleepy</desc>
</character>
</characters>
<plot>
Cliff meets Lovely Woman. Loyal Dog sleeps, but wakes up to bark
at mailman.
</plot>
<success type='bestseller'>4</success>
<success type='bookclubs'>9</success>
</book>
</books>
XML;
//提取節(jié)點(diǎn)內(nèi)容
$xml = new SimpleXMLElement($xmlstr);
foreach ($xml->book[0]->success as $success) {
switch((string) $success['type']) { // Get attributes as element indices
case 'bestseller':
echo $success. ' months on bestseller list<br>';
break;
case 'bookclubs':
echo $success. ' bookclub listings';
break;
}
}
//修改文本節(jié)點(diǎn)內(nèi)容
$xml = new SimpleXMLElement($xmlstr);
$xml->book[0]->characters->character[0]->name = 'Big Cliff';
echo $xml->asXML();
//添加子元素的文本節(jié)點(diǎn)
$xml = new SimpleXMLElement($xmlstr);
$character = $xml->book[0]->characters->addChild('character');
$character->addChild('name', 'Yellow Cat');
$character->addChild('desc', 'aloof');
$success = $xml->book[0]->addChild('success', '2');
$success->addAttribute('type', 'reprints');
echo $xml->asXML();
?>
實(shí)例二:
if (file_exists('test1.xml')) { //讀取xml文件
$xml = simplexml_load_file('test1.xml');
var_dump(xml);
} else {
exit('Failed to open test1.xml.');
}
三.DOM和simple互操作
DOM導(dǎo)入simpleXML:
<?php
$sxe = simplexml_load_string('<books><book><title>Great American
Novel</title></book></books>');
if ($sxe === false) {
echo 'Error while parsing the document';
exit;
}
$dom_sxe = dom_import_simplexml($sxe);
if (!$dom_sxe) {
echo 'Error while converting XML';
exit;
}
$dom = new DOMDocument('1.0');
$dom_sxe = $dom->importNode($dom_sxe, true);
$dom_sxe = $dom->appendChild($dom_sxe);
$test2 = $dom->saveXML(); // put string in test2
$dom -> save('test2.xml'); // save as file
?>
simpleXML導(dǎo)入DOM:
<?php
$dom = new domDocument;
$dom->loadXML('<books><book><title>Great American
Novel</title></book></books>');
if (!$dom) {
echo 'Error while parsing the document';
exit;
}
$s = simplexml_import_dom($dom);
echo $s->book[0]->title; // Great American Novel
?>
實(shí)例一:
復(fù)制代碼 代碼如下:
<?php
//Creates XML string and XML document using the DOM
$dom = new DomDocument('1.0');
//add root - <books>
$books = $dom->appendChild($dom->createElement_x_x ('books'));
//add <book> element to <books>
$book = $books->appendChild($dom->createElement_x_x ('book'));
//add <title> element to <book>
$title = $book->appendChild($dom->createElement_x_x ('title'));
//add <title> text node element to <title>
$title->appendChild($dom->createTextNode('Great American Novel'));
//generate xml
$dom->formatOutput = true; // set the formatOutput attribute of domDocument to true
//save XML as string or file
$test1 = $dom->saveXML(); // put string in test1
$dom -> save('test1.xml'); // save as file
?>
實(shí)例二:
復(fù)制代碼 代碼如下:
$aa = "111";
$xmlstr = <<<XML
<?xml version='1.0'?>
<document>
<title>{$aa}</title>
<from>Joe</from>
<to>Jane</to>
<body>
I know that's the answer -- but what's the question?
</body>
</document>
XML;
$dom = new domDocument;
$dom->loadXML($xmlstr);
$test1 = $dom->saveXML();
$dom->save('test1.xml');
實(shí)例三:
test1.xml:
復(fù)制代碼 代碼如下:
<?xml version="1.0"?>
<books>
<book>
<author>Jack Herrington</author>
<title>PHP Hacks</title>
<publisher>O'Reilly</publisher>
</book>
<book>
<author>Jack Herrington</author>
<title>Podcasting Hacks</title>
<publisher>O'Reilly</publisher>
</book>
</books>
example.php:
復(fù)制代碼 代碼如下:
$doc = new DOMDocument();
$doc->load('test1.xml');
$books = $doc->getElementsByTagName("book");
foreach($books as $book){
$authors = $book->getElementsByTagName("author");
$author = $authors->item(0)->nodeValue;
$publishers = $book->getElementsByTagName( "publisher" );
$publisher = $publishers->item(0)->nodeValue;
$titles = $book->getElementsByTagName( "title" );
$title = $titles->item(0)->nodeValue;
echo "$title - $author - $publisher\n";
}
二.使用simple生成和讀取xml文件
實(shí)例一:
復(fù)制代碼 代碼如下:
<?
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<books>
<book>
<title>Great American Novel</title>
<characters>
<character>
<name>Cliff</name>
<desc>really great guy</desc>
</character>
<character>
<name>Lovely Woman</name>
<desc>matchless beauty</desc>
</character>
<character>
<name>Loyal Dog</name>
<desc>sleepy</desc>
</character>
</characters>
<plot>
Cliff meets Lovely Woman. Loyal Dog sleeps, but wakes up to bark
at mailman.
</plot>
<success type='bestseller'>4</success>
<success type='bookclubs'>9</success>
</book>
</books>
XML;
//提取節(jié)點(diǎn)內(nèi)容
$xml = new SimpleXMLElement($xmlstr);
foreach ($xml->book[0]->success as $success) {
switch((string) $success['type']) { // Get attributes as element indices
case 'bestseller':
echo $success. ' months on bestseller list<br>';
break;
case 'bookclubs':
echo $success. ' bookclub listings';
break;
}
}
//修改文本節(jié)點(diǎn)內(nèi)容
$xml = new SimpleXMLElement($xmlstr);
$xml->book[0]->characters->character[0]->name = 'Big Cliff';
echo $xml->asXML();
//添加子元素的文本節(jié)點(diǎn)
$xml = new SimpleXMLElement($xmlstr);
$character = $xml->book[0]->characters->addChild('character');
$character->addChild('name', 'Yellow Cat');
$character->addChild('desc', 'aloof');
$success = $xml->book[0]->addChild('success', '2');
$success->addAttribute('type', 'reprints');
echo $xml->asXML();
?>
實(shí)例二:
復(fù)制代碼 代碼如下:
if (file_exists('test1.xml')) { //讀取xml文件
$xml = simplexml_load_file('test1.xml');
var_dump(xml);
} else {
exit('Failed to open test1.xml.');
}
三.DOM和simple互操作
DOM導(dǎo)入simpleXML:
復(fù)制代碼 代碼如下:
<?php
$sxe = simplexml_load_string('<books><book><title>Great American
Novel</title></book></books>');
if ($sxe === false) {
echo 'Error while parsing the document';
exit;
}
$dom_sxe = dom_import_simplexml($sxe);
if (!$dom_sxe) {
echo 'Error while converting XML';
exit;
}
$dom = new DOMDocument('1.0');
$dom_sxe = $dom->importNode($dom_sxe, true);
$dom_sxe = $dom->appendChild($dom_sxe);
$test2 = $dom->saveXML(); // put string in test2
$dom -> save('test2.xml'); // save as file
?>
simpleXML導(dǎo)入DOM:
復(fù)制代碼 代碼如下:
<?php
$dom = new domDocument;
$dom->loadXML('<books><book><title>Great American
Novel</title></book></books>');
if (!$dom) {
echo 'Error while parsing the document';
exit;
}
$s = simplexml_import_dom($dom);
echo $s->book[0]->title; // Great American Novel
?>
您可能感興趣的文章:
- PHP讀取XML值的代碼(推薦)
- PHP中使用xmlreader讀取xml數(shù)據(jù)示例
- PHP讀取XML格式文件的方法總結(jié)
- DOM基礎(chǔ)及php讀取xml內(nèi)容操作的方法
- php通過(guò)正則表達(dá)式記取數(shù)據(jù)來(lái)讀取xml的方法
- php中使用DOM類讀取XML文件的實(shí)現(xiàn)代碼
- php讀取XML的常見(jiàn)方法實(shí)例總結(jié)
- PHP簡(jiǎn)單讀取xml文件的方法示例
- PHP使用DOM和simplexml讀取xml文檔的方法示例
- PHP讀取XML文件的方法實(shí)例總結(jié)【DOMDocument及simplexml方法】
相關(guān)文章
PHP實(shí)現(xiàn)圖片自動(dòng)清理的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)圖片自動(dòng)清理的方法,可實(shí)現(xiàn)清除固定日期內(nèi)沒(méi)有訪問(wèn)的圖片,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07PHP基于單例模式實(shí)現(xiàn)的數(shù)據(jù)庫(kù)操作基類
這篇文章主要介紹了PHP基于單例模式實(shí)現(xiàn)的數(shù)據(jù)庫(kù)操作基類,涉及PHP操作數(shù)據(jù)庫(kù)的基本配置與增刪改查等操作技巧,需要的朋友可以參考下2016-01-01win7 64位系統(tǒng) 配置php最新版開(kāi)發(fā)環(huán)境(php+Apache+mysql)
作為一個(gè)phper,基礎(chǔ)的php開(kāi)發(fā)環(huán)境的配置是最基本的本事了,今天我們來(lái)看下在win7 64位系統(tǒng)中,如何配置php的開(kāi)發(fā)環(huán)境呢2014-08-08Php中文件下載功能實(shí)現(xiàn)超詳細(xì)流程分析
瀏覽器發(fā)送一個(gè)請(qǐng)求,請(qǐng)求訪問(wèn)服務(wù)器中的某個(gè)網(wǎng)頁(yè)(如:down.php),該網(wǎng)頁(yè)的代碼如下2012-06-06php中l(wèi)aravel調(diào)度執(zhí)行錯(cuò)誤解決方法
在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于php中l(wèi)aravel調(diào)度執(zhí)行錯(cuò)誤解決方法,對(duì)此有興趣的朋友們可以學(xué)習(xí)參考下。2021-02-02php實(shí)現(xiàn)字符串翻轉(zhuǎn)的方法
這篇文章主要介紹了php實(shí)現(xiàn)字符串翻轉(zhuǎn)的方法,涉及php操作字符串的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03php+js iframe實(shí)現(xiàn)上傳頭像界面無(wú)跳轉(zhuǎn)
這篇文章主要介紹了php+js實(shí)現(xiàn)的上傳頭像界面無(wú)跳轉(zhuǎn),示例中用到了iframe,需要的朋友可以參考下2014-04-04