PHP - XML - Recursively process an XML document.

Write a recursive function to traverse the XML document tree:

  1. <?php
  2. // create XML data string
  3. $xmlData =<<< END
  4. <?xml version="1.0"?>
  5. <movie>
  6.     <title>The Matrix</title>
  7.     <credits>
  8.         <actor>
  9.             <name>Keanu Reeves</name>
  10.             <character>Neo</character>
  11.         </actor>
  12.         <actor>
  13.             <name>Laurence Fishburne</name>
  14.             <character>Morpheus</character>
  15.         </actor>
  16.         <actor>
  17.             <name>Carrie-Anne Moss</name>
  18.             <character>Trinity</character>
  19.         </actor>
  20.         <director>
  21.             <name>Andy Wachowski</name>
  22.         </director>
  23.         <director>
  24.             <name>Larry Wachowski</name>
  25.         </director>
  26.     </credits>
  27.     <year>1999</year>
  28.     <duration units="min">120</duration>
  29. </movie>
  30. END;
  31. // read XML data
  32. $xml = simplexml_load_string($xmlData)↩
  33. or die("ERROR: Cannot create SimpleXML object");
  34. // function to recursively iterate over XML tree
  35. // printing node names and values
  36. function xmlTraverse($node) {
  37.     foreach ($node->children() as $name => $data) {
  38.         if (trim($data) != "") {
  39.             echo "$name: [$data, " . strlen($data) . "]/n";
  40.         }
  41.         xmlTraverse($data);
  42.     }
  43. }
  44. // traverse XML tree
  45. // result: "title: [The Matrix, 10] name: [Keanu Reeves, 12] …"
  46. xmlTraverse($xml);
  47. ?>

Comments

Because an XML document is a hierarchical tree of nested elements, the most efficient way to process it is with a recursive function that calls itself to traverse the entire tree. This is the technique illustrated in the previous listing.

The recursive xmlTraverse() function begins with the root element and looks for children with SimpleXML's children() function. If children exist, the function loops over the child list, repeatedly calling itself to process each node until it reaches the end of the list. The process continues until no further nodes remain to be processed. At each stage, the current node name and value are printed.

Alternatively, consider using an Iterator from the Standard PHP Library (SPL). Iterators are ready-made, extensible constructs designed specifically to loop over item collections—directories, files, class methods, and XML trees. A predefined SimpleXMLIterator already exists and it's not difficult to extend this for recursive array processing. Here's how:

 

  1. <?php
  2. // create XML data string
  3. $xmlData =<<< END
  4. <?xml version="1.0"?>
  5. <movie>
  6.     <title>The Matrix</title>
  7.     <credits>
  8.         <actor>
  9.             <name>Keanu Reeves</name>
  10.             <character>Neo</character>
  11.         </actor>
  12.         <actor>
  13.             <name>Laurence Fishburne</name>
  14.             <character>Morpheus</character>
  15.         </actor>
  16.         <actor>
  17.             <name>Carrie-Anne Moss</name>
  18.             <character>Trinity</character>
  19.         </actor>
  20.         <director>
  21.                <name>Andy Wachowski</name>
  22.         </director>
  23.         <director>
  24.             <name>Larry Wachowski</name>
  25.         </director>
  26.     </credits>
  27.     <year>1999</year>
  28.     <duration units="min">120</duration>
  29. </movie>
  30. END;
  31. // read XML data
  32. $xml = simplexml_load_string($xmlData"SimpleXMLIterator")↩
  33. or die("ERROR: Cannot create SimpleXML object");
  34. // recursively iterate over XML tree
  35. foreach(new RecursiveIteratorIterator($xml, true) as $name => $data)
  36. {
  37.     if (trim($data) != "") {
  38.         echo "$name: [$data, " . strlen($data) . "]/n";
  39.     }
  40. }
  41. ?>

The process of traversing a series of nested directories is significantly simpler with the SPL at hand. First, initialize a SimpleXMLIterator() object and pass it the XML tree to be processed. Next, initialize a RecursiveIteratorIterator() object (this is an Iterator designed solely for the purpose of iterating over other recursive Iterators) and pass it the newly-minted SimpleXMLIterator(). You can now process the results with a foreach() loop.

You can read more about the SimpleXMLIterator and the RecursiveIteratorIterator http://www.php.net/~helly/php/ext/spl/.

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值