使用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个回答

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 />";
douhuan7862
douhuan7862 我是否可以问这是否嵌入到XML树迭代的foreach循环中?
大约 10 年之前 回复
doudi8829
doudi8829 非常感谢。 我必须说这些东西比我以前的东西更先进。
大约 10 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐