0

The following code:

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

Gives me this: (All correct so far)

 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
            )

    )

)

I now need to query the array above to get the Address/DateTime/Text value based on the Name within that node:

so if I was looking for say the DateTime value from DateTimeInfo I need to query the Name (PickupDate) which will be unique and get the related property. I already have a variable which will tell me what the "value" property for that node is (like DateTime, Text etc) so I just need to know how to query the XML to get the correct node and find the values within that one. I am also happy to use another reader if need be.

Any ideas?

mauzilla
  • 3,574
  • 10
  • 50
  • 86
  • You do that with something called XPath. It is available in SimpleXML [`SimpleXMLElement::xpath()`](http://php.net/simplexmlelement.xpath). For your question, in SimpleXML, it's not useful to post the output of print_r / var_dump. Instead give the XML verbatim so that it is clear on which XML you operate. Make it smaller for your example if it is large. – hakre Mar 03 '14 at 15:22

1 Answers1

0

You can do so with an xpath expression, here fetching any <DateTimeInfo> element that has a child <name> with the value "PickupDate":

$xpath = '//DateTimeInfo[name="PickupDate"]/DateTime';
list ($first) = $xml->xpath($xpath);

See SimpleXMLElement::xpath() and Basic SimpleXML usage.


Edit: This has been also outlined in the following duplicate question:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836