phpsimplexml

How does one print just the values with Simple XML Element?


I was pulling data in from an XML file and it was printing more than the values, whereas I just wanted the values using this code:

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

$result = file_get_contents($url, false);
if ($result === false) {
    /* Handle error */
}
 $output = $result;
  $xmlObject = simplexml_load_string($output);?> ////As per Paul's code suggestions. I changed `xml` to `xmlObject`

<pre><?php echo print_r($xml);?></pre> Gave me this:

     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] => POSITION 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] => POSITION TITLE
                                        )

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

                                

) This is the Html I am using to display the data

<?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);}?>

Here is the output (just the names and title)

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 ) 

How can I get just the values?

I had combed through Stackoverflow for days and yet cannot find anyone describing this problem


Solution

  • The problem is you are printing a representation of the variable, followed by the success of the print_r() function.

    The output looks like this because it is actually two parts:

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

    First, print_r() has printed out a human-readable representation of the variable itself:

    SimpleXMLElement Object ( [0] => Joan Jett ) 
    

    and then you are echoing the result of the print_r() function. That function returns true, so php will echo out the value:

    1
    

    What you need to do is directly echo out the values, not a representation of the variable. For example:

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

    would output

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

    If you only wanted to print the comma if there was a title, you could make an array of the fields you want to print, and then implode them into a comma-separated string:

    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;
    }