PHP - Retrieving Node and Attribute Values From XML
2008-10-11 17:41
302 查看
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";
?>
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
// 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 - XML - Modifying Node and Attribute Values
- [PHP] To display some attribute of the node in the XML document.
- C# XML - XmlNode and XmlAttribute
- Xml/Binary SerializerHelper and XmlReader read xml node/attribute on foward-only sequential
- 转载:how to automate Microsoft Excel and return the values from a multi-cell range to an array
- 20100603 学习记录:常用类型--XMLDocument XMLNode XMLElement XMLAttribute XMLFragment XML
- 转:Manage Web.config from XML File by using configSource attribute ASP.Net 2.0
- Units Problem: How to read text size as custom attr from xml and set it to TextView in java code
- Compiling PHP and Apache 2 from source on Linux OS
- Leetcode_remove-nth-node-from-end-of-list(c++ and python version)
- A real and useful way to get/prase XML file from a site
- ORA-03113 and Getting CLOB Value from XMLTYPE
- XmlDocument中针对某Node的特定Attribute的清除操作说明
- Identifying multiple/single values of metadata and recording into XML via .NET Client Object Model
- Beginning Google Maps Applications with PHP and Ajax: From Novice to Professional
- Pro PHP XML and Web Services
- Groovy Samples (attribute validators,transient attribute value calculations,bind variable and attribute default values )
- Get and Set Attribute Values with Properties
- Storing and Retrieving Images from SQL Server using Microsoft .NET
- [WARNING] Warning: selected war files include a WEB-INF/web.xml which will be ignored (webxml attribute is missing from war task, or ignoreWebxml attribute is specified as 'true')