0

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

2
  • 1
    I suggest reading the Basic SimpleXML usage in the PHP documentation. Commented Nov 2, 2024 at 7:45
  • 1
    As a side note, echo print_r makes no sense. Use either echo or print_r. Commented Nov 2, 2024 at 7:46

1 Answer 1

1

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; } 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! This worked, with a little tweak. - I edited my code and commented why
@MaryAnneWelch I'm glad that helped, but just to note, there was no need to change $xml to $xmlObject. I didn't notice in my answer that I had changed the variable name you used in your question, so that was actually a mistake!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.