简体   繁体   English

Simplexml 使用命名空间获取属性

[英]Simplexml get attributes with a namespace

I have an XML Document which has attributes with a namespace.我有一个具有命名空间属性的 XML 文档。 The XML looks like: XML 看起来像:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sunil.tweet"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.sunil.tweet.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

How can I extract the android:name attribute from the activity tag?如何从activity标签中提取android:name属性?

You need to use the namespace when getting the attribute.获取属性时需要使用命名空间。 The android namespace is defined as: android命名空间定义为:

http://schemas.android.com/apk/res/android http://schemas.android.com/apk/res/android

So you need to pass that to the attributes() method, like this:因此,您需要将其传递给attributes()方法,如下所示:

$xml = simplexml_load_string($xmlStr);

echo (string) $xml->application->activity->attributes('http://schemas.android.com/apk/res/android')->name;

Outputs输出

com.sunil.tweet.MainActivity com.sunil.tweet.MainActivity

Codepad Demo键盘演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM