PHP - Retrieving Node and Attribute Values From XML

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. ?>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值