php xpath问题

时间:2010-05-18 20:12:18

标签: php xml xpath parsing

我正在进行cURL POST并返回错误响应,将其解析为数组,但现在遇到xpath问题。

// XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<errors xmlns="http://host/project">
   <error code="30" description="[] is not a valid email address."/>
   <error code="12" description="id[] does not exist."/>
   <error code="3" description="account[] does not exist."/>
   <error code="400" description="phone[] does not exist."/>
</errors>

//功能/类

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

    public function getErrorMessage() {
        $in_arr = false;
        $el = $this->xml->xpath("//@errors");        
        $returned_errors = count($el);

        if($returned_errors > 0) {
            foreach($el as $element) {
                if(is_object($element) || is_array($element)) {
                    foreach($element as $item) {
                        $in_arr[] = $item;
                    }
                }
            }
        } else {
            return $returned_errors;
        }            
        return $in_arr;
    }
}

//调用函数

// $errorMessage is holding the XML value in an array index
// something like: $arr[3] = $xml;
$errMsg = new parseXML($arr[3]); 
$errMsgArr = $errMsg->getErrorMessage();

我想要的是所有错误代码和描述属性值

编辑:

好的,这是print_r($ this-&gt; xml,true);

SimpleXMLElement Object
(
    [error] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [code] => 30
                            [description] => [] is not a valid email address.
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [code] => 12
                            [description] => Id[12345] does not exist.
                        )

                )

            [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [code] => 3
                            [description] => account[] does not exist.
                        )

                )

            [3] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [code] => 400
                            [description] => phone[] does not exist.
                        )

                )

        )

)

对于我的生活,我无法弄清楚为什么我可以得到代码和描述,任何想法?

编辑#2。编辑#2 好的,我想我会把它分解。

我正在使用cURL将请求发送到我们的一个服务器,我解析出HTTP响应头和xml(如果返回xml)。 header / xml中的每一行我都会爆炸成一个数组。所以,如果有错误,我会看到数组的额外索引。然后我做这样的事情。

$if_err_from_header = $http_return_response[10]; 
// I know that index 10 is where if any the error message in xml is (the one posted above).

之后我这样做:

$errMsg = new parseXML($if_err_from_header); 
$errMsgArr = $errMsg->getErrorMessage();

我仍然无法从错误的属性中获取代码和描述,我缺少什么?

编辑#3 好的,为什么这有用呢?

$in_arr = false;
// This returns all the code attributes
$el = $this->xml->xpath("//@code");

# if $el is false, nothing returned from xpath(), set to an empty array
$el = $el == false ? array() : $el;

foreach($el as $element) {
    $in_arr[] = array("code" => $element["code"], "description" => $element["description"]);
}
return $in_arr;

编辑#4:

好的,这可以获得我想要的值,但它有点像黑客,想选择特定的元素,但是......

$el = $this->xml->xpath("//*");

2 个答案:

答案 0 :(得分:2)

确保考虑名称空间:

$this->xml->registerXPathNamespace('n', 'http://host/project');
$el = $this->xml->xpath("/n:errors/n:error");
$returned_errors = count($el);

访问降低值的示例..

foreach($el as $element) {
   print "code: " . $element["code"] . "\n";
}

答案 1 :(得分:1)

XPath中的

@是属性选择器。您正在尝试选择根元素,因此它应该是:

  $el = $this->xml->xpath("/errors");

如果要选择所有错误元素,请使用

  $el = $this->xml->xpath("/errors/error");

  $el = $this->xml->xpath("//error");