Extracting XML attributes that have namespace prefixes

I’m having trouble extracting attributes from the following XML using SimpleXML functions. I’m trying to get android:versionName, minSdkVersion, normalScreens and largeScreens.

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionCode="20"
    android:versionName="1.0.0.5"
    package="com.s.android.lite"
    >
    <uses-sdk
        android:minSdkVersion="1"
        >
    </uses-sdk>
    <supports-screens
            android:anyDensity="true"
            android:normalScreens="true"
            android:largeScreens="true"
            >
    </supports-screens>
    <application
        android:label="@7F080001"
        android:icon="@7F020056"
        android:name=".SApplication"
        android:persistent="false"
        android:debuggable="false"
        >
        <activity
            android:name=".SActivity"
            >
            <intent-filter
                >
                <action
                    android:name="android.intent.action.MAIN"
                    >
                </action>
                <category
                    android:name="android.intent.category.LAUNCHER"
                    >
                </category>
            </intent-filter>
        </activity>
        <activity
            android:name=".Signin"
            >
        </activity>
        <activity
            android:name=".ChatScreen"
            >
        </activity>
        <activity
            android:name=".ContactList"
            >
            <intent-filter
                <action
                    android:name="android.intent.action.CREATE_LIVE_FOLDER"
                    >
                </action>
                <category
                    android:name="android.intent.category.DEFAULT"
                    >
                </category>
            </intent-filter>
        </activity>
    </application>
    <uses-permission
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:name="android.permission.INTERNET"
        >
    </uses-permission>
    <uses-permission
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:name="android.permission.READ_CONTACTS"
        >
    </uses-permission>
</manifest>

I’ve tried various things with xpath() which haven’t worked, for example…


$mfxml = simplexml_load_file('manifest.xml');
$mfxml->registerXPathNamespace('default', 'http://schemas.android.com/apk/res/android');
$result = $mfxml->xpath('//default:manifest/supports-screens/@anyDensity');
var_dump($result);

Any help would be appreciated.

Thanks

Look here, I think there is a solution in the comments:

Here’s a basic example using the XML from the first post.


$xml = simplexml_load_file('manifest.xml');
$xpaths = array(
	"@android:versionName",
	"uses-sdk/@android:minSdkVersion",
	"supports-screens/@android:normalScreens",
	"supports-screens/@android:largeScreens"
);
$nodes = $xml->xpath(implode("|", $xpaths));
foreach ($nodes as $node) {
	printf("%s = %s\
", $node->getName(), (string) $node);
}

Which should output something like:


versionName = 1.0.0.5
minSdkVersion = 1
normalScreens = true
largeScreens = true

Thanks for the responses.

Salathe, I can’t seem to get your example working - I get no values:


versionName =
minSdkVersion =
normalScreens =
largeScreens =

Thanks

Not sure if this is the problem or not, but it looks like the XML document is not well formed. The opening tag for the 2nd intent filter is missing the closing >. Edit: I ran Salathe’s code with the corrected XML and it works for me.

It looks like my local PHP was the cause (although the invalid XML didn’t help either so thanks for highlighting that anonymousdude).

My apologies and thanks for your help!