日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

PHP擴展之XML操作(一)——SimpleXML

瀏覽:16日期:2022-09-16 10:04:17
一、簡介及安裝

SimpleXML 擴展提供了一個非常簡單和易于使用的工具集,能將 XML 轉換成一個帶有一般屬性選擇器和數組迭代器的對象。

此擴展需要?libxml?PHP 擴展。這表示需要使用?--enable-libxml?,盡管這將隱式完成因為 libxml 是缺省開啟的。

需要PHP5及以上版本。

此擴展默認為啟用,編譯時可通過下列選項禁用:?--disable-simplexml

二、使用示例基本使用

Example #1 Include file example.php with XML string

<?php$xmlstr?=?<<<XML<?xml?version=’1.0’?standalone=’yes’?><movies><movie><title>PHP:?Behind?the?Parser</title><characters><character><name>Ms.?Coder</name><actor>Onlivia?Actora</actor></character><character><name>Mr.?Coder</name><actor>El?Act&#211;r</actor></character></characters><plot>So,?this?language.?It’s?like,?a?programming?language.?Or?is?it?ascripting?language??All?is?revealed?in?this?thrilling?horror?spoofof?a?documentary.</plot><great-lines><line>PHP?solves?all?my?web?problems</line></great-lines><rating?type='thumbs'>7</rating><rating?type='stars'>5</rating></movie></movies>XML;?>

The simplicity of SimpleXML appears most clearly when one extracts a string or number from a basic XML document.

Example #2 Getting?<plot>

<?phpinclude?’example.php’;

$movies?=?new?SimpleXMLElement($xmlstr);

echo?$movies->movie[0]->plot;?>

以上例程會輸出:

So, this language. It’s like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary.

Accessing elements within an XML document that contain characters not permitted under PHP’s naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.

Example #3 Getting?<line>

<?phpinclude?’example.php’;

$movies?=?new?SimpleXMLElement($xmlstr);

echo?$movies->movie->{’great-lines’}->line;?>

以上例程會輸出:

PHP solves all my web problems

Example #4 Accessing non-unique elements in SimpleXML

When multiple instances of an element exist as children of a single parent element, normal iteration techniques apply.

<?phpinclude?’example.php’;

$movies?=?new?SimpleXMLElement($xmlstr);

/*?For?each?<character>?node,?we?echo?a?separate?<name>.?*/foreach?($movies->movie->characters->character?as?$character)?{echo?$character->name,?’?played?by?’,?$character->actor,?PHP_EOL;}

?>

以上例程會輸出:

Ms. Coder played by Onlivia ActoraMr. Coder played by El Actór

Note:

Properties ($movies->movie?in previous example) are not arrays. They are?iterable?and?accessible?objects.

Example #5 Using attributes

So far, we have only covered the work of reading element names and their values. SimpleXML can also access element attributes. Access attributes of an element just as you would elements of an?array.

<?phpinclude?’example.php’;

$movies?=?new?SimpleXMLElement($xmlstr);

/*?Access?the?<rating>?nodes?of?the?first?movie.*?Output?the?rating?scale,?too.?*/foreach?($movies->movie[0]->rating?as?$rating)?{switch((string)?$rating[’type’])?{?//?Get?attributes?as?element?indicescase?’thumbs’:echo?$rating,?’?thumbs?up’;break;case?’stars’:echo?$rating,?’?stars’;break;}}?>

以上例程會輸出:

7 thumbs up5 stars

Example #6 Comparing Elements and Attributes with Text

To compare an element or attribute with a string or pass it into a function that requires a string, you must cast it to a string using?(string). Otherwise, PHP treats the element as an object.

<?phpinclude?’example.php’;

$movies?=?new?SimpleXMLElement($xmlstr);

if?((string)?$movies->movie->title?==?’PHP:?Behind?the?Parser’)?{print?’My?favorite?movie.’;}

echo?htmlentities((string)?$movies->movie->title);?>

以上例程會輸出:

My favorite movie.PHP: Behind the Parser

Example #7 Comparing Two Elements

Two SimpleXMLElements are considered different even if they point to the same element since PHP 5.2.0.

<?phpinclude?’example.php’;

$movies1?=?new?SimpleXMLElement($xmlstr);$movies2?=?new?SimpleXMLElement($xmlstr);var_dump($movies1?==?$movies2);?//?false?since?PHP?5.2.0?>

以上例程會輸出:

bool(false)

Example #8 Using XPath

SimpleXML includes built-in?XPath?support. To find all?<character>?elements:

<?phpinclude?’example.php’;

$movies?=?new?SimpleXMLElement($xmlstr);

foreach?($movies->xpath(’//character’)?as?$character)?{echo?$character->name,?’played?by?’,?$character->actor,?PHP_EOL;}?>

’//’ serves as a wildcard. To specify absolute paths, omit one of the slashes.

以上例程會輸出:

Ms. Coder played by Onlivia ActoraMr. Coder played by El Actór

Example #9 Setting values

Data in SimpleXML doesn’t have to be constant. The object allows for manipulation of all of its elements.

<?phpinclude?’example.php’;$movies?=?new?SimpleXMLElement($xmlstr);

$movies->movie[0]->characters->character[0]->name?=?’Miss?Coder’;

echo?$movies->asXML();?>

以上例程會輸出:

<?xml version='1.0' standalone='yes'?><movies> <movie> <title>PHP: Behind the Parser</title> <characters> <character> <name>Miss Coder</name> <actor>Onlivia Actora</actor> </character> <character> <name>Mr. Coder</name> <actor>El Act&#xD3;r</actor> </character> </characters> <plot> So, this language. It’s like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. </plot> <great-lines> <line>PHP solves all my web problems</line> </great-lines> <rating type='thumbs'>7</rating> <rating type='stars'>5</rating> </movie></movies>

Example #10 Adding elements and attributes

Since PHP 5.1.3, SimpleXML has had the ability to easily add children and attributes.

<?phpinclude?’example.php’;$movies?=?new?SimpleXMLElement($xmlstr);

$character?=?$movies->movie[0]->characters->addChild(’character’);$character->addChild(’name’,?’Mr.?Parser’);$character->addChild(’actor’,?’John?Doe’);

$rating?=?$movies->movie[0]->addChild(’rating’,?’PG’);$rating->addAttribute(’type’,?’mpaa’);

echo?$movies->asXML();?>

以上例程會輸出:

<?xml version='1.0' standalone='yes'?><movies> <movie> <title>PHP: Behind the Parser</title> <characters> <character> <name>Ms. Coder</name> <actor>Onlivia Actora</actor> </character> <character> <name>Mr. Coder</name> <actor>El Act&#xD3;r</actor> </character> <character><name>Mr. Parser</name><actor>John Doe</actor></character></characters> <plot> So, this language. It’s like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. </plot> <great-lines> <line>PHP solves all my web problems</line> </great-lines> <rating type='thumbs'>7</rating> <rating type='stars'>5</rating> <rating type='mpaa'>PG</rating></movie></movies>

Example #11 DOM Interoperability

PHP has a mechanism to convert XML nodes between SimpleXML and DOM formats. This example shows how one might change a DOM element to SimpleXML.

<?php$dom?=?new?DOMDocument;$dom->loadXML(’<books><book><title>blah</title></book></books>’);if?(!$dom)?{echo?’Error?while?parsing?the?document’;exit;}

$books?=?simplexml_import_dom($dom);

echo?$books->book[0]->title;?>

以上例程會輸出:

blah處理XML錯誤

Example #1 Loading broken XML string

<?phplibxml_use_internal_errors(true);$sxe?=?simplexml_load_string('<?xml?version=’1.0’><broken><xml></broken>');if?($sxe?===?false)?{echo?'Failed?loading?XMLn';foreach(libxml_get_errors()?as?$error)?{echo?'t',?$error->message;}}?>

以上例程會輸出:

Failed loading XML Blank needed here parsing XML declaration: ’?>’ expected Opening and ending tag mismatch: xml line 1 and broken Premature end of data in tag broken line 1三、相關函數simplexml_import_dom?— Get a SimpleXMLElement object from a DOM node.simplexml_load_file?— Interprets an XML file into an objectsimplexml_load_string?— Interprets a string of XML into an object四、相關類及成員函數

SimpleXMLElement類

SimpleXMLElement::addAttribute?— Adds an attribute to the SimpleXML elementSimpleXMLElement::addChild?— Adds a child element to the XML nodeSimpleXMLElement::asXML?— Return a well-formed XML string based on SimpleXML elementSimpleXMLElement::attributes?— Identifies an element’s attributesSimpleXMLElement::children?— Finds children of given nodeSimpleXMLElement::__construct?— Creates a new SimpleXMLElement objectSimpleXMLElement::count?— Counts the children of an elementSimpleXMLElement::getDocNamespaces?— Returns namespaces declared in documentSimpleXMLElement::getName?— Gets the name of the XML elementSimpleXMLElement::getNamespaces?— Returns namespaces used in documentSimpleXMLElement::registerXPathNamespace?— Creates a prefix/ns context for the next XPath querySimpleXMLElement::saveXML?— 別名 SimpleXMLElement::asXMLSimpleXMLElement::__toString?— Returns the string contentSimpleXMLElement::xpath?— Runs XPath query on XML data

SimpleXMLIterator類

SimpleXMLIterator::current?— Returns the current elementSimpleXMLIterator::getChildren?— Returns the sub-elements of the current elementSimpleXMLIterator::hasChildren?— Checks whether the current element has sub elements.SimpleXMLIterator::key?— Return current keySimpleXMLIterator::next?— Move to next elementSimpleXMLIterator::rewind?— Rewind to the first elementSimpleXMLIterator::valid?— Check whether the current element is valid
標簽: PHP
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
国产在线欧美| 高清av一区| 久久久国产亚洲精品| 国产一区二区三区国产精品| 精品一区二区三区亚洲| 国产极品模特精品一二| 狠狠久久伊人| 国产精品777777在线播放| 日韩激情av在线| 国产精品久久久久久久久久白浆 | 免费在线观看视频一区| 狠狠干成人综合网| 99精品99| 亚洲bt欧美bt精品777| 国产情侣一区| 精品视频国内| 日韩欧美综合| 女人av一区| 亚洲专区视频| 美腿丝袜亚洲三区| 精品久久久久中文字幕小说| 亚洲电影有码| 视频国产精品| 精品国产中文字幕第一页| 99久久久国产精品美女| 日韩精品一卡二卡三卡四卡无卡| 亚洲伊人精品酒店| 美女国产精品久久久| 久久精品主播| 午夜欧美精品| 欧美综合精品| 激情欧美一区| 国产精品巨作av| 老鸭窝亚洲一区二区三区| 国产精久久久| 日韩制服丝袜av| 成人在线黄色| 久久亚洲影院| 精品精品99| 亚洲综合激情在线| 欧美国产先锋| 人人爽香蕉精品| 久久久久欧美精品| 国产极品嫩模在线观看91精品| 免播放器亚洲| 久久婷婷激情| 激情综合五月| 国产一卡不卡| 亚洲精品字幕| 欧美另类专区| 欧美成人基地| 精品视频免费| 国产精品分类| 国产精品一区二区中文字幕| 日韩精品一级二级| 亚洲黄色网址| 97精品国产福利一区二区三区| 天堂va欧美ⅴa亚洲va一国产| 久久久成人网| 亚洲韩日在线| 成人看片网站| 久久蜜桃资源一区二区老牛| 免费看久久久| 精品久久影院| 天堂√中文最新版在线| 国产成人免费av一区二区午夜| 欧美a在线观看| 国产一区二区精品福利地址| 丰满少妇一区| 日韩啪啪电影网| 国产精品国产一区| 久久久久久亚洲精品美女| 欧美aⅴ一区二区三区视频| 国产精品探花在线观看| 国产欧美自拍一区| 国产一区二区三区成人欧美日韩在线观看| 日韩高清一区二区| 国产精品男女| 日韩欧美精品一区| 好吊日精品视频| 亚洲三级在线| 老司机免费视频一区二区| 红杏一区二区三区| 亚洲精品在线影院| 好看不卡的中文字幕| 国产一区导航| 亚洲精品少妇| 国产极品久久久久久久久波多结野 | 婷婷成人av| 免费精品一区| 久久久9色精品国产一区二区三区| 欧美69视频| 日韩欧美2区| 四虎精品一区二区免费| 久久xxxx| 国产精品嫩草影院在线看| 国产一区二区三区四区五区| 日本免费一区二区三区四区| 亚洲精品2区| 国产精品调教| 热久久久久久久| 丰满少妇一区| 中文字幕一区二区三区在线视频| 久久99青青| 亚洲视频电影在线| 亚洲成人不卡| 国产福利资源一区| 一区二区三区四区精品视频| 久久人人99| 福利在线免费视频| 国产激情综合| 精品女同一区二区三区在线观看| 国产成人精品一区二区三区视频 | 亚洲精品在线观看91| 少妇精品久久久一区二区| 97精品国产一区二区三区| 日韩美女精品| 蜜臀av亚洲一区中文字幕| 一区二区三区四区在线看| 精品国产aⅴ| 亚洲精品乱码| 亚洲精品九九| 免费在线欧美视频| 亚洲一区二区免费看| 91精品久久久久久久久久不卡| 国产成人免费视频网站视频社区| 国产亚洲一区二区三区不卡| 中文字幕一区二区三区四区久久 | 日本久久二区| 日韩欧美高清一区二区三区| 国产视频一区三区| 在线人成日本视频| 精品国产免费人成网站| sm捆绑调教国产免费网站在线观看| 欧美亚洲网站| 国产精品国码视频| 久久伊人久久| 99久久99久久精品国产片果冰| 91精品国产自产在线观看永久∴| 色爱av综合网| 国产模特精品视频久久久久| 久久国产福利| 国产剧情一区| 精品久久精品| 亚洲精品一区二区在线看| 首页亚洲欧美制服丝腿| 美国欧美日韩国产在线播放| 日韩精品一区二区三区免费视频| 91精品在线免费视频| 精品美女在线视频| 久久九九国产| 中文字幕一区二区av| 久久精品97| 亚洲五月婷婷| 日本成人在线网站| 亚洲啊v在线| 亚洲久久一区| 国产精品不卡| 中文字幕乱码亚洲无线精品一区| 久久wwww| 日韩专区一卡二卡| 国产麻豆一区二区三区| 91精品婷婷色在线观看| 欧美片第1页综合| 久久精品影视| 国产精品66| 日韩在线观看一区二区| 国产一区一一区高清不卡| 美女日韩在线中文字幕| 日本一二区不卡| 欧美精品国产一区| 亚洲作爱视频| 欧美激情视频一区二区三区在线播放| 久久三级福利| 久久av资源| 欧美一级全黄| 亚洲天堂一区二区| 日韩成人午夜精品| 国产高清一区| 日韩av一区二| 久久精品播放| 精品视频高潮| 日韩精品亚洲一区二区三区免费| 久久久天天操| 午夜久久中文| 国产一区二区亚洲| 国产乱码精品一区二区三区四区| 首页国产欧美久久| 中文久久精品| 久久理论电影| 99久久精品网| 国产成人精品三级高清久久91| 日韩欧美少妇| 最新日韩av| 欧美日一区二区| 久久精品亚洲人成影院| 欧美日韩视频免费观看| 98精品视频| 久久久久久一区二区| 久久三级毛片|