【问题标题】:Simplexml get attributes with a namespaceSimplexml 使用命名空间获取属性
【发布时间】:2012-11-10 18:20:15
【问题描述】:

我有一个带有命名空间属性的 XML 文档。 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>

如何从activity 标签中提取android:name 属性?

【问题讨论】:

  • 我已经用完整的 xml 文件编辑了问题

标签: php xml-parsing simplexml


【解决方案1】:

获取属性时需要使用命名空间。 android 命名空间定义为:

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

所以你需要将它传递给attributes() 方法,像这样:

$xml = simplexml_load_string($xmlStr);

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

输出

com.sunil.tweet.MainActivity

Codepad Demo

【讨论】:

    猜你喜欢
    • 2020-04-19
    • 2014-06-15
    • 2011-09-28
    • 2021-09-30
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    相关资源
    最近更新 更多