天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

用PHP編寫和讀取XML的幾種方式

一.使用DOM生成和讀取XML文件
實例一:
復制代碼 代碼如下:
<?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
?>

實例二:
復制代碼 代碼如下:
$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');


實例三:
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文件
實例一:
復制代碼 代碼如下:
<?
$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;

//提取節點內容
$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;
}
}

//修改文本節點內容
$xml = new SimpleXMLElement($xmlstr);
$xml->book[0]->characters->character[0]->name = 'Big Cliff';
echo $xml->asXML();

//添加子元素的文本節點
$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();

?>


實例二:
復制代碼 代碼如下:
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導入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導入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
?>

php技術用PHP編寫和讀取XML的幾種方式,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 手机在线国产视频 | 男人国产AV天堂WWW麻豆 | 国产人妻麻豆蜜桃色在线 | yellow免费观看完整版直播 | 人妻少妇久久久久久97人妻 | 久久中文字幕免费高清 | 久久综合网久久综合 | 蜜桃视频无码区在线观看 | 神马伦理不卡午夜电影 | 国产午夜高潮熟女精品AV | 亚洲色无码播放 | 毛片网站在线观看 | 国产成人ae在线观看网站站 | 最近免费中文字幕MV在线视频3 | 久久久无码精品亚洲欧美 | 欧洲video60| 亚洲AV一宅男色影视 | 国产午夜电影在线观看不卡 | 美国z0069| 天美传媒在线完整免费观看网站 | 粉嫩无套白浆第一次jk | 国产剧情在线精品视频不卡 | 欧美尤物射精集锦 | 久久综合久久鬼色 | 久久人妻少妇嫩草AV蜜桃99 | 国产精品婷婷久青青原 | 久久黄色录像 | 久久这里都是精品 | 成人天堂婷婷青青视频在线观看 | 最近日本字幕MV免费观看在线 | china年轻小帅脸直播飞机 | 国产午夜人成在线视频麻豆 | 亚洲日本香蕉视频观看视频 | 久久精品国产福利电影网 | 美女逼逼毛茸茸 | 国产精品无码AV天天爽色欲 | 亚洲欧美中文字幕高清在线 | 光溜溜的美女直播软件 | 四虎成人影院 | 亚洲最大成人 | 国产麻豆91网在线看 |