首頁 >後端開發 >php教程 >关于 PHP 中如何解析 XML 的问题

关于 PHP 中如何解析 XML 的问题

WBOY
WBOY原創
2016-06-06 20:15:08989瀏覽

我现在要处理这样一个 XML

关于 PHP 中如何解析 XML 的问题

<code><?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="mode_label_longest_standby">超长待机</string>
    <string name="battery_info_remaining_discharging_time">"续航时间: "</string>
    <string name="battery_info_remaining_charging_time">"充电预估: "</string>
    <string name="ongoing_notification_battery_info_remaining_discharging_time">"续航时间"</string>
    <string name="ongoing_notification_battery_info_remaining_charging_time">"充电预估"</string>
    <string name="battery_info_value_unknown">未知</string>
    <string name="battery_info_details_title">电池详情</string>
    <string name="uid_label_android_system">Android 系统</string>
    <string name="uid_label_wlan_idle">WLAN 空闲状态</string>
    <string name="uid_label_bluetooth_idle">蓝牙空闲状态</string>
    <string name="uid_label_cpu_idle">CPU 空闲状态</string>
    <string name="uid_label_radio_active">语音通话</string>
    <string name="uid_label_radio_data">数据</string>
    <string name="uid_label_radio_idle">待机</string>
    <string name="hw_label_unknown">未知</string>
    <string name="hw_label_screen">屏幕</string>
    <string name="hw_label_wlan">WLAN</string>
</resources></code>

我的 PHP 代码为

<code><?php $xmlDoc = new DOMDocument();
$xmlDoc->load("ZH-CNstrings.xml");

// print $xmlDoc->saveXML();
$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item)
  {
      echo "<pre class="brush:php;toolbar:false">";
  print var_dump($item);
  echo "
"; } ?>

现在我可以去到 string 的内容 “超长待机”如 但 name 的内容 “mode_label_longest_standby” 我不知道怎么取出来

回复内容:

我现在要处理这样一个 XML

关于 PHP 中如何解析 XML 的问题

<code><?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="mode_label_longest_standby">超长待机</string>
    <string name="battery_info_remaining_discharging_time">"续航时间: "</string>
    <string name="battery_info_remaining_charging_time">"充电预估: "</string>
    <string name="ongoing_notification_battery_info_remaining_discharging_time">"续航时间"</string>
    <string name="ongoing_notification_battery_info_remaining_charging_time">"充电预估"</string>
    <string name="battery_info_value_unknown">未知</string>
    <string name="battery_info_details_title">电池详情</string>
    <string name="uid_label_android_system">Android 系统</string>
    <string name="uid_label_wlan_idle">WLAN 空闲状态</string>
    <string name="uid_label_bluetooth_idle">蓝牙空闲状态</string>
    <string name="uid_label_cpu_idle">CPU 空闲状态</string>
    <string name="uid_label_radio_active">语音通话</string>
    <string name="uid_label_radio_data">数据</string>
    <string name="uid_label_radio_idle">待机</string>
    <string name="hw_label_unknown">未知</string>
    <string name="hw_label_screen">屏幕</string>
    <string name="hw_label_wlan">WLAN</string>
</resources></code>

我的 PHP 代码为

<code><?php $xmlDoc = new DOMDocument();
$xmlDoc->load("ZH-CNstrings.xml");

// print $xmlDoc->saveXML();
$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item)
  {
      echo "<pre class="brush:php;toolbar:false">";
  print var_dump($item);
  echo "
"; } ?>

现在我可以去到 string 的内容 “超长待机”如 但 name 的内容 “mode_label_longest_standby” 我不知道怎么取出来

这么整齐的XML,lz要不要用正则试试?

<code><?php $str = '<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="mode_label_longest_standby">超长待机</string>
    <string name="battery_info_remaining_discharging_time">"续航时间: "</string>
    <string name="battery_info_remaining_charging_time">"充电预估: "</string>
    <string name="ongoing_notification_battery_info_remaining_discharging_time">"续航时间"</string>
    <string name="ongoing_notification_battery_info_remaining_charging_time">"充电预估"</string>
    <string name="battery_info_value_unknown">未知</string>
    <string name="battery_info_details_title">电池详情</string>
    <string name="uid_label_android_system">Android 系统</string>
    <string name="uid_label_wlan_idle">WLAN 空闲状态</string>
    <string name="uid_label_bluetooth_idle">蓝牙空闲状态</string>
    <string name="uid_label_cpu_idle">CPU 空闲状态</string>
    <string name="uid_label_radio_active">语音通话</string>
    <string name="uid_label_radio_data">数据</string>
    <string name="uid_label_radio_idle">待机</string>
    <string name="hw_label_unknown">未知</string>
    <string name="hw_label_screen">屏幕</string>
    <string name="hw_label_wlan">WLAN</string>
</resources>';

preg_match_all("|<string name='\"(.*)\"'>(.*)</string>|", $str, $result, PREG_SET_ORDER);
var_dump($result);</code>
<code>
array(17) {
  [0]=>
  array(3) {
    [0]=>
    string(63) "<string name="mode_label_longest_standby">超长待机</string>"
    [1]=>
    string(26) "mode_label_longest_standby"
    [2]=>
    string(12) "超长待机"
  }
  [1]=>
  array(3) {
    [0]=>
    string(80) "<string name="battery_info_remaining_discharging_time">"续航时间: "</string>"
    [1]=>
    string(39) "battery_info_remaining_discharging_time"
    [2]=>
    string(16) ""续航时间: ""
  }
  [2]=>
  array(3) {
    [0]=>
    string(77) "<string name="battery_info_remaining_charging_time">"充电预估: "</string>"
    [1]=>
    string(36) "battery_info_remaining_charging_time"
    [2]=>
    string(16) ""充电预估: ""
  }
  [3]=>
  array(3) {
    [0]=>
    string(99) "<string name="ongoing_notification_battery_info_remaining_discharging_time">"续航时间"</string>"
    [1]=>
    string(60) "ongoing_notification_battery_info_remaining_discharging_time"
    [2]=>
    string(14) ""续航时间""
  }
  [4]=>
  array(3) {
    [0]=>
    string(96) "<string name="ongoing_notification_battery_info_remaining_charging_time">"充电预估"</string>"
    [1]=>
    string(57) "ongoing_notification_battery_info_remaining_charging_time"
    [2]=>
    string(14) ""充电预估""
  }
  [5]=>
  array(3) {
    [0]=>
    string(57) "<string name="battery_info_value_unknown">未知</string>"
    [1]=>
    string(26) "battery_info_value_unknown"
    [2]=>
    string(6) "未知"
  }
  [6]=>
  array(3) {
    [0]=>
    string(63) "<string name="battery_info_details_title">电池详情</string>"
    [1]=>
    string(26) "battery_info_details_title"
    [2]=>
    string(12) "电池详情"
  }
  [7]=>
  array(3) {
    [0]=>
    string(63) "<string name="uid_label_android_system">Android 系统</string>"
    [1]=>
    string(24) "uid_label_android_system"
    [2]=>
    string(14) "Android 系统"
  }
  [8]=>
  array(3) {
    [0]=>
    string(61) "<string name="uid_label_wlan_idle">WLAN 空闲状态</string>"
    [1]=>
    string(19) "uid_label_wlan_idle"
    [2]=>
    string(17) "WLAN 空闲状态"
  }
  [9]=>
  array(3) {
    [0]=>
    string(67) "<string name="uid_label_bluetooth_idle">蓝牙空闲状态</string>"
    [1]=>
    string(24) "uid_label_bluetooth_idle"
    [2]=>
    string(18) "蓝牙空闲状态"
  }
  [10]=>
  array(3) {
    [0]=>
    string(59) "<string name="uid_label_cpu_idle">CPU 空闲状态</string>"
    [1]=>
    string(18) "uid_label_cpu_idle"
    [2]=>
    string(16) "CPU 空闲状态"
  }
  [11]=>
  array(3) {
    [0]=>
    string(59) "<string name="uid_label_radio_active">语音通话</string>"
    [1]=>
    string(22) "uid_label_radio_active"
    [2]=>
    string(12) "语音通话"
  }
  [12]=>
  array(3) {
    [0]=>
    string(51) "<string name="uid_label_radio_data">数据</string>"
    [1]=>
    string(20) "uid_label_radio_data"
    [2]=>
    string(6) "数据"
  }
  [13]=>
  array(3) {
    [0]=>
    string(51) "<string name="uid_label_radio_idle">待机</string>"
    [1]=>
    string(20) "uid_label_radio_idle"
    [2]=>
    string(6) "待机"
  }
  [14]=>
  array(3) {
    [0]=>
    string(47) "<string name="hw_label_unknown">未知</string>"
    [1]=>
    string(16) "hw_label_unknown"
    [2]=>
    string(6) "未知"
  }
  [15]=>
  array(3) {
    [0]=>
    string(46) "<string name="hw_label_screen">屏幕</string>"
    [1]=>
    string(15) "hw_label_screen"
    [2]=>
    string(6) "屏幕"
  }
  [16]=>
  array(3) {
    [0]=>
    string(42) "<string name="hw_label_wlan">WLAN</string>"
    [1]=>
    string(13) "hw_label_wlan"
    [2]=>
    string(4) "WLAN"
  }
}
</code>

如果xml里面的string标签的name值是唯一的,你可以这样子试试:
header("Content-type:text/html;charset=utf-8");
$xmlDoc = new DOMDocument();
$xmlDoc->load("test.xml");

$x = $xmlDoc->getElementsByTagName("string");
$arr = array();
foreach ($x as $item){

<code>$arr[$item->attributes->item(0)->nodeValue] = $item->nodeValue;</code>

}
print_r($arr);

得到的结果是
Array
(

<code>[mode_label_longest_standby] => 超长待机
[battery_info_remaining_discharging_time] => "续航时间: "
[battery_info_remaining_charging_time] => "充电预估: "
[ongoing_notification_battery_info_remaining_discharging_time] => "续航时间"
[ongoing_notification_battery_info_remaining_charging_time] => "充电预估"
[battery_info_value_unknown] => 未知
[battery_info_details_title] => 电池详情
[uid_label_android_system] => Android 系统
[uid_label_wlan_idle] => WLAN 空闲状态
[uid_label_bluetooth_idle] => 蓝牙空闲状态
[uid_label_cpu_idle] => CPU 空闲状态
[uid_label_radio_active] => 语音通话
[uid_label_radio_data] => 数据
[uid_label_radio_idle] => 待机
[hw_label_unknown] => 未知
[hw_label_screen] => 屏幕
[hw_label_wlan] => WLAN</code>

)

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn