仅使用 SimpleXMLElement 打印值

问题描述 投票:0回答:1

我从 xml 文件中提取数据,它打印的内容比值多。我只想要价值观。 我正在使用这个代码

<?php
$url = 'site.com';

$result = file_get_contents($url, false);
if ($result === false) {
    /* Handle error */
}
 $output = $result;
 $xml = simplexml_load_string($output);?>

<pre><?php echo print_r($xml);?></pre>
给我这个:

     SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [returnVersion] => 2022v5.0
        )
    [ReturnHeader]
    [ReturnData] => SimpleXMLElement Object
            [FromForm] => SimpleXMLElement Object

                    [DataGrp]
                            [DataGrpPerson] => Array
                                (
                                    [0] => SimpleXMLElement Object
                                        (
                                            [PersonNm] => Joan Jett
                                            [USAddress] => SimpleXMLElement Object
                                                (
                                                    [AddressLine1Txt] => 0 EAST Main STREET
                                                    [CityNm] => CITY NAME
                                                    [StateAbbreviationCd] => STATE NAME
                                                    [ZIPCd] => ZIP NUMBER
                                                )

                                            [TitleTxt] => POSTION TITLE

                                        )
                                    [1] => SimpleXMLElement Object
                                        (
                                            PersonNm] => Tom Petty
                                            [USAddress] => SimpleXMLElement Object
                                                (
                                                    [AddressLine1Txt] => 1 EAST Main STREET
                                                    [CityNm] => CITY NAME
                                                    [StateAbbreviationCd] => STATE NAME
                                                    [ZIPCd] => ZIP NUMBER
                                                )

                                            [TitleTxt] => POSTION TITLE
                                        )

                                    [2] => SimpleXMLElement Object
                                        (
                                            PersonNm] => Brandi Carlile
                                            [USAddress] => SimpleXMLElement Object
                                                (
                                                    [AddressLine1Txt] => 2 EAST Main STREET
                                                    [CityNm] => CITY NAME
                                                    [StateAbbreviationCd] => STATE NAME
                                                    [ZIPCd] => ZIP NUMBER
                                        )

                                

) 这是我用来显示数据的 Html

<?php foreach ($xml->ReturnData->FromForm->DataGrp]->DataGrpPerson[0]->PersonNm as $item) {
    echo print_r($item);}?>, <?php foreach ($xml->ReturnData->FromForm->DataGrp]->DataGrpPerson[0]->TitleTxt as $item) {
    echo print_r($item);}?>

这是输出(只是名称和标题)

SimpleXMLElement Object ( [0] => Joan Jett ) 1, SimpleXMLElement Object ( [0] => POSTION TITLE ) 1
SimpleXMLElement Object ( [0] => S Tom Petty ) 1, SimpleXMLElement Object ( [0] => POSTION TITLE ) 1
SimpleXMLElement Object ( [0] => Brandi Carlile ) 1, SimpleXMLElement Object ( [0] => POSTION TITLE ) 

我怎样才能得到这些值?

我已经梳理了 stackoverflow 好几天了。我找不到任何人描述这个问题

php simplexml
1个回答
0
投票

问题是您正在打印变量的表示,然后 print_r() 函数成功。

输出看起来像这样,因为它实际上是两部分:

SimpleXMLElement Object ( [0] => Joan Jett ) 1

首先,print_r() 打印出变量本身的人类可读表示:

SimpleXMLElement Object ( [0] => Joan Jett ) 

然后你就回显了print_r()函数的结果。该函数返回

true
,因此 php 将回显该值:

1

您需要做的是直接回显值,而不是变量的表示。例如:

foreach ($xmlObject->ReturnData->FromForm->DataGrp->DataGrpPerson as $person)
{
    echo $person->PersonNm . ', ' . $person->TitleTxt . PHP_EOL;
}

会输出

Joan Jett, POSTION TITLE
Tom Petty, POSTION TITLE
Brandi Carlile,

如果您只想在有标题时打印逗号,则可以创建一个要打印的字段数组,然后将它们分解为逗号分隔的字符串:

foreach ($xmlObject->ReturnData->FromForm->DataGrp->DataGrpPerson as $person)
{
    $fields = [];

    if (!empty($person->PersonNm)) {
        $fields[] = $person->PersonNm;
    }

    if (!empty($person->TitleTxt)) {
        $fields[] = $person->TitleTxt;
    }

    echo implode(', ', $fields) . PHP_EOL;
}
© www.soinside.com 2019 - 2024. All rights reserved.