开发者

Conditionally parsing XML attributes with PHP

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 lik开发者_StackOverflow社区e "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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜