PHP - Retrieving Node and Attribute Values From XML
来源:互联网 发布:上海办网络宽带哪家好 编辑:程序博客网 时间:2024/06/05 17:59
Use SimpleXML to locate the node or attribute and retrieve its value:
- <?php
- // define XML data string
- $xmlData = <<< END
- <?xml version="1.0"?>
- <data>
- <color red="128" green="0" blue="128">purple</color>
- </data>
- END;
- // read XML data string
- $xml = simplexml_load_string($xmlData)↩
- or die("ERROR: Cannot create SimpleXML object");
- // read attribute values
- $hexColor = sprintf("#%02x%02x%02x", $xml->color['red'],↩
- $xml->color['green'], $xml->color['blue']);
- // read node data
- // result: "The color purple is #800080 in hexadecimal"
- echo "The color " . $xml->color . " is " . $hexColor . " in hexadecimal";
- ?>
Comments
In this listing, a call to simplexml_load_string() converts the XML data into a SimpleXML object. Once such an object has been initialized, elements are represented as object properties and attribute collections as associative arrays. Node values can thus be accessed using standard object->property notation, beginning with the root element and moving down the hierarchical path of the document tree, while attribute values can be accessed as keys of the attribute array associated with each object property.
If there is more than one element with the same name at a particular level of the XML hierarchy, it is represented, with its partners, in a numerically indexed array. Such a collection can be processed with a foreach() loop, as in the following listing:
- <?php
- // create XML data string
- $xmlData =<<< END
- <?xml version="1.0"?>
- <collection>
- <color>red</color>
- <color>blue</color>
- <color>green</color>
- <color>yellow</color>
- </collection>
- END;
- // read XML data
- $xml = simplexml_load_string($xmlData)↩
- or die("ERROR: Cannot create SimpleXML object");
- // process node collection
- // result: "red blue green yellow"
- foreach ($xml->color as $color) {
- echo "$color ";
- }
- ?>
Or, if you don't know the element name, use the children() method to iterate over all the children of a particular node:
- <?php
- // create XML data string
- $xmlData =<<< END
- <?xml version="1.0"?>
- <collection>
- <color>red</color>
- <color>blue</color>
- <color>green</color>
- <color>yellow</color>
- </collection>
- END;
- // read XML data
- $xml = simplexml_load_string($xmlData)↩
- or die("ERROR: Cannot create SimpleXML object");
- // process node collection
- // result: "color: red color: blue color: green color: yellow "
- foreach ($xml->children() as $name => $data) {
- echo "$name: $data ";
- }
- ?>
Note that you can also iterate over the attribute collection for a specific element with the attributes() method, as illustrated here:
- <?php
- // define XML data string
- $xmlData = <<< END
- <?xml version="1.0"?>
- <data>
- <element shape="rectangle" height="10" width="5" length="7" />
- </data>
- END;
- // read XML data string
- $xml = simplexml_load_string($xmlData)↩
- or die("ERROR: Cannot create SimpleXML object");
- // print attributes
- // result: "shape: rectangle; height: 10; width: 5; length: 7; "
- foreach ($xml->element->attributes() as $name => $data) {
- echo "$name: $data; ";
- }
- ?>
- PHP - Retrieving Node and Attribute Values From XML
- PHP - XML - Modifying Node and Attribute Values
- XML DOM Get Node Values
- Get and Set Attribute Values with Properties
- HiWords and LoWords from Long Values
- Groovy Samples (attribute validators,transient attribute value calculations,bind variable and attribute default values )
- Class Attribute and XML doc
- 打开eclipse时 提示 D:\eworkplace\appcompat_v7\res\values-v23\styles_base.xml:20: error: Error retrieving
- 导入appcompat_v7报错:appcompat_v7\res\values-v23\styles_base.xml:20: error: Error retrieving parent for
- C:\bq\appcompat_v7\res\values-large\themes_base.xml:19: error: Error retrieving parent for item: No
- E:\Java app\apk\res\values\styles.xml:7: error: Error retrieving parent for item: No resource fo
- Retrieving Photos and Videos from the Photo Library(从手机中检索相片,视频 )
- Save and Load from XML
- Save and Load from XML
- GeneGAN: Learning Object Transfiguration and Attribute Subspace from Unpaired Data
- How to: Qualify XML Element and XML Attribute Names
- python xml parse and modify node
- 【转】Save and Load from XML
- 两分钟用C#搭建IE BHO勾子,窃取密码
- 前言的乱七八糟
- C#开发BHO插件UrlTrack
- 函数重载
- 第1章 欢迎进入软件构建的世界
- PHP - Retrieving Node and Attribute Values From XML
- 照片冲洗尺寸问题
- 第2章 用隐喻来更充分地理解软件开发
- eXtremeComponents介绍
- Microsoft JET Database Engine (0x80004005) 未指定的错误
- Javascript 常用代码
- C# 获得当前的路径
- Javascript+CSS实现Flash动态新闻效果(pp原创)
- (转)eclipse常用快捷方式