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:

  1. <?php
  2. // define XML data string
  3. $xmlData = <<< END
  4. <?xml version="1.0"?>
  5. <data>
  6.      <color red="128" green="0" blue="128">purple</color>
  7. </data>
  8. END;
  9. // read XML data string
  10. $xml = simplexml_load_string($xmlData)↩
  11. or die("ERROR: Cannot create SimpleXML object");
  12. // read attribute values
  13. $hexColor = sprintf("#%02x%02x%02x"$xml->color['red'],↩
  14. $xml->color['green'], $xml->color['blue']);
  15. // read node data
  16. // result: "The color purple is #800080 in hexadecimal"
  17. echo "The color " . $xml->color . " is " . $hexColor . " in hexadecimal";
  18. ?>

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:

 

  1. <?php
  2. // create XML data string
  3. $xmlData =<<< END
  4. <?xml version="1.0"?>
  5. <collection>
  6.     <color>red</color>
  7.     <color>blue</color>
  8.     <color>green</color>
  9.     <color>yellow</color>
  10. </collection>
  11. END;
  12. // read XML data
  13. $xml = simplexml_load_string($xmlData)↩
  14. or die("ERROR: Cannot create SimpleXML object");
  15. // process node collection
  16. // result: "red blue green yellow"
  17. foreach ($xml->color as $color) {
  18.     echo "$color ";
  19. }
  20. ?>

Or, if you don't know the element name, use the children() method to iterate over all the children of a particular node:

 

  1. <?php
  2. // create XML data string
  3. $xmlData =<<< END
  4. <?xml version="1.0"?>
  5. <collection>
  6.     <color>red</color>
  7.     <color>blue</color>
  8.     <color>green</color>
  9.     <color>yellow</color>
  10. </collection>
  11. END;
  12. // read XML data
  13. $xml = simplexml_load_string($xmlData)↩
  14. or die("ERROR: Cannot create SimpleXML object");
  15. // process node collection
  16. // result: "color: red color: blue color: green color: yellow "
  17. foreach ($xml->children() as $name => $data) {
  18.     echo "$name: $data ";
  19. }
  20. ?>

Note that you can also iterate over the attribute collection for a specific element with the attributes() method, as illustrated here:

 

  1. <?php
  2. // define XML data string
  3. $xmlData = <<< END
  4. <?xml version="1.0"?>
  5. <data>
  6.      <element shape="rectangle" height="10" width="5" length="7" />
  7. </data>
  8. END;
  9. // read XML data string
  10. $xml = simplexml_load_string($xmlData)↩
  11. or die("ERROR: Cannot create SimpleXML object");
  12. // print attributes
  13. // result: "shape: rectangle; height: 10; width: 5; length: 7; "
  14. foreach ($xml->element->attributes() as $name => $data) {
  15.     echo "$name: $data; ";
  16. }
  17. ?>

 

 

原创粉丝点击