dregvw1801 2010-10-13 21:00
浏览 37

使用PHP有条件地解析XML属性

I am really hoping somebody can help me with this.

Basically, I am trying to parse certain XML attributes in an XML tree based on other attributes in that branch.

As an example of the type of XML I am working with: -

<root>
<employees>
  <team id="1643">
    <member id ="153461" jobtype="permament" division="cleaning" rollnumber="1364"/>
    <member id ="153461" jobtype="temporary" division="reception" rollnumber="1326"/>
    <member id ="153461" jobtype="parttime" division="cleaning" rollnumber="1312"/>
    <member id ="153461" jobtype="permament" division="cleaning" rollnumber="1326"/>
  </team>
  <team id="1633">
    <member id ="153461" jobtype="permament" division="cleaning" rollnumber="1244"/>
    <member id ="153461" jobtype="temporary" division="reception" rollnumber="1569"/>
    <member id ="153461" jobtype="parttime" division="cleaning" rollnumber="1472"/>
    <member id ="153461" jobtype="permament" division="cleaning" rollnumber="1112"/>
  </team>
  <team id="1674">
    <member id ="153461" jobtype="permament" division="cleaning" rollnumber="1488"/>
    <member id ="153461" jobtype="temporary" division="reception" rollnumber="1032"/>
    <member id ="153461" jobtype="parttime" division="cleaning" rollnumber="1886"/>
    <member id ="153461" jobtype="permament" division="cleaning" rollnumber="1445"/>
  </team>
</employees>
</root>

What I am trying to do is attain the rollnumber for all employees that fill certain categories. So I want to be able to do a that looks like "If jobtype = "permanent" and division = "cleaning" then echo rollnumber".

Now, I have tried to parse this with PHP, and basically failed. I failed because I was using syntax such as this: -

$team->xpath("//member[@jobtype='permament'][@division='cleaning']/@rollnumber")

This parses as an array. No problem. I can pull the variables out of the array. No problem.

The problem is this...

... PHP cannot deal with nested arrays, which this code produces. It holds the initial array in memory and just parses the same variables over and over again. I have tried resetting the array etc at the end of each loop, but that did not work. PHP only seems capable of destroying the array at the end of the foreach sequence.

I would really like to know if it is possible to parse this with PHP? Is there a work around for this? Any help/advice would be greaty appreciated.

  • 写回答

1条回答

  • dream02008 2010-10-14 13:50
    关注

    So this is how I would do it

    // ParseXML.class.php

    /**
     * This is the base class to load the XML.
     * Individual scripts should extend this class for 
     * explicit functionality  
     */
    
    class ParseXML {
        protected $xml;
    
        public function __construct($xml) {
            if(is_file($xml)) {
                $this->xml = simplexml_load_file($xml);
            } else {
                $this->xml = simplexml_load_string($xml);
            }
        }
    }
    

    // ParseEmployees.extends.php

    /**
     * This class extends the parseXML class
     */
    class ParseEmployees extends parseXML {
    
        public function getRollNumberArray() {
            $attr = false;
            $el = $this->xml->xpath("//member[@jobtype='permament'][@division='cleaning']");
    
            if($el && count($el) === 1) {
                $attr = (array) $el[0]->attributes();
                $attr = $attr['@attributes'];
            } 
            return $attr;
        }
    }
    

    How to use:

    // Add xml file/post
    $xml_file = '/path/to/xml_file.xml';
    
    // Need to include the two files above
    include('ParseXML.class.php');
    include('ParseEmployees.extends.php');
    
    $emp = new ParseEmployees($xml_file);
    $emp_rollnumber_arr = $emp->getRollNumberArray();
    
    // Now you should be able to access the array and see the values
    echo "Employees Role Numbers:<pre>".print_r($emp_rollnumber_arr,true)."</pre><br />";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度