Getting key/value pairs from plist-style xml using simplexml in php

Posted by Anthony on Stack Overflow See other posts from Stack Overflow or by Anthony
Published on 2010-05-19T08:18:24Z Indexed on 2010/05/19 8:20 UTC
Read the original article Hit count: 259

Filed under:
|
|
|

Here is an example bit from the xml file:

<array>
    <dict>
        <key>Name</key>
        <string>Joe Smith</string>
        <key>Type</key>
        <string>Profile</string>
        <key>Role</key>
        <string>User</string>
        <key>Some Number</key>
        <integer>1</integer>
        <key>Some Boolean</key>
        <true/>
    </dict>
</array>

I have two separate goals. The first is to extract an array from the dictnode that would look like:

[Name] => Joe Smith
[Type] => Profile
[Role] => User
[Some Number] => 1
[Some Boolean] => true

It's not crucial that the boolean be included, so if that adds too much complexity, I'd rather just know how to deal with the others for now.

The second goal is to be able to select the value node (<string>, <integer>,etc) so that I can change the value. I would need to select it based on the text value of the preceding key element.

I think the following XPath should work:

//key[.=$keyname]/following-sibling[1]

But I'm not sure.

Basically, this whole system that Apple uses seems logical, but totally contrary to the XML is supposed to work. If I ran the world, the original XML would look more like:

<dict type="array">
    <value key="Name" type="string">Joe Smith</value>
    <value key="Type" type="string">Profile</value>
    <value key="Role type="string">User</value>
    <value key="Some Number" type="integer">1</value>
    <value key="Some Boolean" type="boolean">true</value>
</dict>

But since it is fairly logical, I am wondering if I'm missing some obvious way of handling it.

© Stack Overflow or respective owner

Related posts about php

Related posts about simplexml