【问题标题】:Conditionally parsing XML attributes with PHP使用 PHP 有条件地解析 XML 属性
【发布时间】:2010-10-13 21:00:53
【问题描述】:

我真的希望有人可以帮助我。

基本上,我正在尝试根据该分支中的其他属性解析 XML 树中的某些 XML 属性。

作为我正在使用的 XML 类型的示例:-

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

我想要做的是获得所有符合特定类别的员工的 rollnumber。所以我希望能够做一个看起来像 "If jobtype = "permanent" and division = "cleaning" then echo rollnumber".

现在,我尝试用 PHP 解析这个,但基本上失败了。我失败了,因为我使用了这样的语法:-

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

这会解析为一个数组。没问题。我可以将变量拉出数组。没问题。

问题是这样的……

... PHP 无法处理此代码生成的嵌套数组。它将初始数组保存在内存中,并一遍又一遍地解析相同的变量。我曾尝试在每个循环结束时重置数组等,但这不起作用。 PHP 似乎只能在 foreach 序列的末尾销毁数组。

我真的很想知道是否可以用 PHP 解析它?有解决办法吗?任何帮助/建议将不胜感激。

【问题讨论】:

    标签: php xml parsing conditional


    【解决方案1】:

    所以我会这样做

    // 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;
        }
    }
    

    使用方法:

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

    【讨论】:

    • 非常感谢。我必须说,这些东西比我习惯的要先进一些。
    • 请问是否要嵌入到 foreach 循环中以进行 XML 树的迭代?
    猜你喜欢
    • 2012-04-01
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多