0

so this is the plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>New item</key>
    <array>
        <dict>
            <key>assets</key>
            <array>
                <dict>
                    <key>kind</key>
                    <string>software-package</string>
                    <key>url</key>
                    <string>http://down.weapp.com/apps/zb/002/43/12/21/58cae8840ba0628e304b1118.ipa</string>
                </dict>
                <dict>
                    <key>kind</key>
                    <string>display-image</string>
                    <key>needs-shine</key>
                    <true/>
                    <key>url</key>
                    <string>http://www.weapp.com/picture/app_ios/cn/002/43/12/21/57x57bb.png</string>
                </dict>
                <dict>
                    <key>kind</key>
                    <string>full-size-image</string>
                    <key>needs-shine</key>
                    <true/>
                    <key>url</key>
                    <string>http://www.weapp.com/picture/app_ios/cn/002/43/12/21/512x512bb.png</string>
                </dict>
            </array>
            <key>metadata</key>
            <dict>
                <key>bundle-identifier</key>
                <string>com.xiaoji.gamecenter</string>
                <key>bundle-version</key>
                <string>1.5.2</string>
                <key>kind</key>
                <string>software</string>
                <key>title</key>
                <string>小鸡模拟器</string>
            </dict>
        </dict>
    </array>
</dict>
</plist>

and i want to grab the .ipa url, to do this i am using simplexml

$xml=simplexml_load_string($plistString)

now i would access it doing

$xmlPlist->dict->array[0]->string,

but there are 2 strings there, how can i access the url one? now this is confusing for me, am i even accessing the plis in the correct way? should i mention every "dict" there is?

2 Answers2

0

Try this:

echo $xml->dict->array->dict->array->dict->string[1];
gmc
  • 3,910
  • 2
  • 31
  • 44
0

To search XML, you should use XPath. This code should do the trick:

<?php
$xml = new SimpleXMLElement($plistString);
$result = $xml->xpath('//dict[./key/text()="kind" and ./string/text()="software-package"]/key[text()="url"]/following-sibling::string');
$url = (string)$result[0];

So what's that mean? First:

dict[./key/text()="kind" and ./string/text()="software-package"]

We compare child elements and their text content; we're looking for a <dict> that contains <key>kind</key> and <string>software-package</string>.

Then:

/key[text()="url"]/following-sibling::string

We take that <dict> element, and look for a child <key> element with "url" as it's text content, and get the <string> element following it.

We still have an XML element at that point, so we use typecasting to get it to a string.

miken32
  • 42,008
  • 16
  • 111
  • 154