您的位置:首页 > Web前端 > Node.js

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";

?>

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; ";

}

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐