2014-03-03 65 views
0

下面的代码:PHP的SimpleXML获得基于其他属性值值

// Get sub category: 
$xml = simplexml_load_string($update_booking_info->XML); 

给了我这样的:(正确到目前为止)

SimpleXMLElement Object 
(
[@attributes] => Array 
    (
     [sub_category_id] => 1 
    ) 

[LocationClass] => Array 
    (
     [0] => SimpleXMLElement Object 
      (
       [@attributes] => Array 
        (
         [Type] => LocationIdentification 
        ) 

       [Lat] => 0 
       [Long] => 0 
       [VZeroQuestionLifeTimeKey] => SimpleXMLElement Object 
        (
        ) 

       [VZeroQuestionsCollectionID] => SimpleXMLElement Object 
        (
        ) 

       [Name] => FromLocation 
       [Address] => Some address 1 
      ) 

     [1] => SimpleXMLElement Object 
      (
       [@attributes] => Array 
        (
         [Type] => LocationIdentification 
        ) 

       [Lat] => 0 
       [Long] => 0 
       [VZeroQuestionLifeTimeKey] => SimpleXMLElement Object 
        (
        ) 

       [VZeroQuestionsCollectionID] => SimpleXMLElement Object 
        (
        ) 

       [Name] => ToLocation 
       [Address] => Some Address 2 
      ) 

    ) 

[DateTimeInfo] => SimpleXMLElement Object 
    (
     [@attributes] => Array 
      (
       [Type] => DateTime 
      ) 

     [Lat] => 0 
     [Long] => 0 
     [VZeroQuestionLifeTimeKey] => 3842 
     [VZeroQuestionsCollectionID] => 916 
     [Name] => PickupDate 
     [DateTime] => 2014-03-28 00:59:08 
    ) 

[TextField] => Array 
    (
     [0] => SimpleXMLElement Object 
      (
       [Lat] => 0 
       [Long] => 0 
       [VZeroQuestionLifeTimeKey] => 3846 
       [VZeroQuestionsCollectionID] => 916 
       [Name] => Cellphone 
       [Text] => 1234567891 
      ) 

     [1] => SimpleXMLElement Object 
      (
       [Lat] => 0 
       [Long] => 0 
       [VZeroQuestionLifeTimeKey] => 3843 
       [VZeroQuestionsCollectionID] => 916 
       [Name] => ContactName 
       [Text] => John Smit 
      ) 

     [2] => SimpleXMLElement Object 
      (
       [Lat] => 0 
       [Long] => 0 
       [VZeroQuestionLifeTimeKey] => 3848 
       [VZeroQuestionsCollectionID] => 916 
       [Name] => NumberPassengers 
       [Text] => 2 
      ) 

    ) 

我现在需要查询上面的数组获取基于该节点内的名称的地址/日期时间/文本值:

因此,如果我正在寻找比如说DateTimeInfo中的DateTime值,我需要查询Name(PickupDate),它将是唯一的并获取相关属性。我已经有一个变量,告诉我该节点的“值”属性是什么(如DateTime,Text等),所以我只需要知道如何查询XML以获取正确的节点并找到其中的值。如果需要,我也很乐意使用另一位读者。

任何想法?

+0

你做到这一点与一些所谓的XPath。它在SimpleXML ['SimpleXMLElement :: xpath()'](http://php.net/simplexmlelement.xpath)中可用。对于你的问题,在SimpleXML中,发布print_r/var_dump的输出是没有用的。请逐字提供XML,以便清楚了解您操作的XML。如果尺寸较大,请将它缩小。 – hakre

回答