开发者

Simplexml How to access all element of the same node

[Villa] => Array
    (
        [0] => SimpleXMLElement Object
            (
                [VillaID] => 6
                [VillaName] => Mary
                [Distances] => SimpleXMLElement Object
                    (
                        [Distance] => Array
                            (
                                [0] => SimpleXMLElement Object
                                    (
                                        [Destination] => Sea
                                        [Value] => 1000 m
                                    )

                                [1] => SimpleXMLElement Object
                                    (
                                        [Destination] => Market
                                        [Value] => 800 m
                                    )
                            )
                    )
            )

        [1] => SimpleXMLElement Object
            (
                [VillaID] => 21
                [VillaName] => Marion
                [Distances] => SimpleXMLElement Object
                    (
                        [Distance] => Array
                            (
                                [0] => SimpleXMLElement Object
                                    (
                                        [Destination] => Beach
                                        [Value] => 5 min
                                    )

                            )
                    )
            )
    )

I need to print all, only of 1 villa (example with id = 6) but VillaId i开发者_JAVA技巧s not an array so it's impossible to get all with foreach

I can obtain it with: echo 'Name of Villa: '.$xml->Villa[0]->VillaName.'

'; etc. etc ... but in this way have to change manually for every villa (too much) the value in the brackets. i've tried with $xml->Villa[$value]->VillaName; ($value comes from another page) but it's not working... Tanks for help!


First of all, your question starts with "i have this xml:" followed by something that is not XML. I'm not saying this to be a smartass, rather because it's important for XML beginners to understand that print_r() is not the right way to inspect SimpleXMLElements. Sometimes it will show you things that aren't in your XML, other times it will not show things that are actually in your XML. In short: do not use print_r() on SimpleXMLElement. Just use ->asXML() and look at the actual XML.

From what I understand, you want to locate and select a node based on some criteria. XML just happens to have a language for that: XPath. The official specs aren't terribly user-friendly but w3schools.com has a pretty good XPath tutorial.

I need to print all, only of 1 villa (example with id = 6) but VillaId is not an array so it's impossible to get all with foreach

Anywhere in your document, you want to select all Villa nodes with an attribute VillaID whose value is "6". In XPath:

//Villa[@VillaID="6"]

Via SimpleXML:

$xml->xpath('//Villa[@VillaID="6"]');

Attention, xpath() always return an array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜