电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

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

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

瀏覽:49日期: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
相關文章:
主站蜘蛛池模板: 刺绳_刀片刺网_刺丝滚笼_不锈钢刺绳生产厂家_安平县浩荣金属丝网制品有限公司-安平县浩荣金属丝网制品有限公司 | 彩信群发_群发彩信软件_视频短信营销平台-达信通 | 北京开源多邦科技发展有限公司官网| 钢格板|热镀锌钢格板|钢格栅板|钢格栅|格栅板-安平县昊泽丝网制品有限公司 | 大倾角皮带机-皮带输送机-螺旋输送机-矿用皮带输送机价格厂家-河南坤威机械 | 塑料撕碎机_编织袋撕碎机_废纸撕碎机_生活垃圾撕碎机_废铁破碎机_河南鑫世昌机械制造有限公司 | 热回收盐水机组-反应釜冷水机组-高低温冷水机组-北京蓝海神骏科技有限公司 | 小型高低温循环试验箱-可程式高低温湿热交变试验箱-东莞市拓德环境测试设备有限公司 | 广西教师资格网-广西教师资格证考试网 | 自动配料系统_称重配料控制系统厂家 | 美国HASKEL增压泵-伊莱科elettrotec流量开关-上海方未机械设备有限公司 | 合肥制氮机_合肥空压机厂家_安徽真空泵-凯圣精机 | 密封无忧网 _ 专业的密封产品行业信息网 | 直读光谱仪,光谱分析仪,手持式光谱仪,碳硫分析仪,创想仪器官网 | 微波消解仪器_智能微波消解仪报价_高压微波消解仪厂家_那艾 | 生产自动包装秤_颗粒包装秤_肥料包装秤等包装机械-郑州鑫晟重工科技有限公司 | 深圳办公室装修,办公楼/写字楼装修设计,一级资质 - ADD写艺 | 干粉砂浆设备_干混砂浆生产线_腻子粉加工设备_石膏抹灰砂浆生产成套设备厂家_干粉混合设备_砂子烘干机--郑州铭将机械设备有限公司 | 耐驰泵阀管件制造-耐驰泵阀科技(天津)有限公司 | 全自动固相萃取仪_高通量真空平行浓缩仪-勤业永为 | 国产离子色谱仪,红外分光测油仪,自动烟尘烟气测试仪-青岛埃仑通用科技有限公司 | 聚丙烯酰胺PAM-聚合氯化铝PAC-絮凝剂-河南博旭环保科技有限公司 巨野电机维修-水泵维修-巨野县飞宇机电维修有限公司 | 有机废气处理-rto焚烧炉-催化燃烧设备-VOC冷凝回收装置-三梯环境 | 薪动-人力资源公司-灵活用工薪资代发-费用结算-残保金优化-北京秒付科技有限公司 | 重庆私家花园设计-别墅花园-庭院-景观设计-重庆彩木园林建设有限公司 | 全自动包装机_灌装机生产厂家-迈驰包装设备有限公司 | 淘趣英语网 - 在线英语学习,零基础英语学习网站 | 皮带输送机-大倾角皮带输送机-皮带输送机厂家-河南坤威机械 | 北京森语科技有限公司-模型制作专家-展览展示-沙盘模型设计制作-多媒体模型软硬件开发-三维地理信息交互沙盘 | 全自动包装机_灌装机生产厂家-迈驰包装设备有限公司 | 齿轮减速电机一体机_蜗轮蜗杆减速马达-德国BOSERL齿轮减速机带电机生产厂家 | 挖掘机挖斗和铲斗生产厂家选择徐州崛起机械制造有限公司 | 塑料撕碎机_编织袋撕碎机_废纸撕碎机_生活垃圾撕碎机_废铁破碎机_河南鑫世昌机械制造有限公司 | 合肥地磅_合肥数控切割机_安徽地磅厂家_合肥世佳电工设备有限公司 | 正压密封性测试仪-静态发色仪-导丝头柔软性测试仪-济南恒品机电技术有限公司 | 最新电影-好看的电视剧大全-朝夕电影网 | 北京百度网站优化|北京网站建设公司-百谷网络科技 | 拉曼光谱仪_便携式|激光|显微共焦拉曼光谱仪-北京卓立汉光仪器有限公司 | 路斯特伺服驱动器维修,伦茨伺服驱动器维修|万骏自动化百科 | 电磁铁_小型推拉电磁铁_电磁阀厂家-深圳市宗泰电机有限公司 | 海德莱电力(HYDELEY)-无功补偿元器件生产厂家-二十年专业从事电力电容器 |