PHP SimpleXML - 无法获得单个节点值,我疯了吗?

时间:2017-03-24 08:20:58

标签: php simplexml

我将我的PHP代码简化到最低限度,仍然无法获得简单的xml节点的值。我重新阅读了文档,以确保我没有错过一些细节,但我无法让它工作。

首先我加载这个非常基本的XML

<?php
/*
 * BEFORE RUNNING:
 * ---------------
 * 1. If not already done, enable the Google Sheets API
 *    and check the quota for your project at
 *    https://console.developers.google.com/apis/api/sheets
 * 2. Install the PHP client library with Composer. Check installation
 *    instructions at https://github.com/google/google-api-php-client.
 */

// Autoload Composer.
require_once __DIR__ . '/vendor/autoload.php';

$client = getClient();

$service = new Google_Service_Sheets($client);

// The ID of the spreadsheet to retrieve data from.
$spreadsheetId = '';  // TODO: Update placeholder value.

$optParams = [];

// The A1 notation of the values to retrieve.
$optParams['ranges'] = [];  // TODO: Update placeholder value.

// How values should be represented in the output.
// The default render option is ValueRenderOption.FORMATTED_VALUE.
$optParams['valueRenderOption'] = '';  // TODO: Update placeholder value.

// How dates, times, and durations should be represented in the output.
// This is ignored if value_render_option is
// FORMATTED_VALUE.
// The default dateTime render option is [DateTimeRenderOption.SERIAL_NUMBER].
$optParams['dateTimeRenderOption'] = '';  // TODO: Update placeholder value.

$response = $service->spreadsheets_values->batchGet($spreadsheetId, $optParams);

// TODO: Change code below to process the `response` object:
echo '<pre>', var_export($response, true), '</pre>', "\n";

function getClient() {
  // TODO: Change placeholder below to generate authentication credentials. See
  // https://developers.google.com/sheets/quickstart/php#step_3_set_up_the_sample
  //
  // Authorize using one of the following scopes:
  //   'https://www.googleapis.com/auth/drive'
  //   'https://www.googleapis.com/auth/drive.readonly'
  //   'https://www.googleapis.com/auth/spreadsheets'
  //   'https://www.googleapis.com/auth/spreadsheets.readonly'
  return null;
}
?>

然后我无法获取详细信息节点&#39;值,我只是不断获得 SimpleXMLElement对象

    $xmlStrShipping = simplexml_load_string('<?xml version="1.0" encoding="utf-8" ?> 
    <Shipping>
        <Orders>
            <Order>
                <number>Order number</number>
                <details></details>
            </Order>
        </Orders>
    </Shipping>');

var_dump($xmlStrShipping); // So far, so good
/* Returns:
SimpleXMLElement Object
(
[Orders] => SimpleXMLElement Object
    (
        [Order] => SimpleXMLElement Object
            (
                [number] => Order number
                [details] => SimpleXMLElement Object
                    (
                    )

            )

    )

)
*/

var_dump($xmlStrShipping->Orders[0]->Order->number); // Why is this happening?
/* Returns
SimpleXMLElement Object
(
    [0] => Order number
)
*/

为什么我无法检索号码
为什么详细信息 SimpleXMLElement对象而不是空字符串?

1 个答案:

答案 0 :(得分:2)

SimpleXML将所有内容都转换为SimpleXMLElement对象。只需将其转换为字符串(或其他):

(string) $xmlStrShipping->Orders[0]->Order->number;

或者如果你在字符串上下文中调用它,它也会起作用,因为SimpleXMLElement实现了神奇的__toString()方法:

echo $xmlStrShipping->Orders[0]->Order->number;