1

I am trying to parse information that is inside an Array. The array comes from an RSS feed that I have converted into a json. The array is:

<channel>
<item>
    <title> Title A </title>
    <description> Description A </description>
    <stuff url="google.com" type="search engine">
</item>

<item>
    <title> Title B </title>
    <description> Description B </description>
    <stuff url="yahoo.com" type="old stuff">
</item>
....

</channel>

I am parsing using something like:

$newsoutput = json_decode(json_encode($the_RSS_Link), TRUE);

foreach ($newsoutput['channel']['item'] as $item) {
   echo $item['title'];
   echo "<br>";
   echo $item['description'];
   echo "<br>";
   echo $item['stuff']['url'];
   echo "<br>";
}

I can get both "title" and "description"; but I cannot extract the "url" values inside "stuff". I have tried various combinations without luck

Please advise.

Thank you,

H.

2
  • 3
    I think you will have to process that XML using an XML parser in order to access the attributes of stuff Commented May 29, 2019 at 12:28
  • @Hernandito can you post the json too ? Commented May 29, 2019 at 13:19

3 Answers 3

2

This is a tested and working example which requires the use of simplexml_load_string()

<?php
$raw = <<<XML
<?xml version='1.0'?> 
<document>
    <channel>
        <item>
            <title>Title A</title>
            <description>Description A</description>
            <stuff url="google.com" type="search engine"></stuff>
        </item>

        <item>
            <title>Title B</title>
            <description>Description B</description>
            <stuff url="yahoo.com" type="old stuff"></stuff>
        </item>
    </channel>
</document>
XML;

$xml = simplexml_load_string($raw);
$newsoutput = json_decode(json_encode($xml), true);

foreach ($newsoutput['channel']['item'] as $item) {
    echo $item['title'];
    echo "<br>\n";
    echo $item['description'];
    echo "<br>\n";
    echo $item['stuff']['@attributes']['url'];
    echo "<br>\n";
}

Output:

Title A<br>
Description A<br>
google.com<br>
Title B<br>
Description B<br>
yahoo.com<br>

Be careful, the XML document you gave us was invalid and I had to close the tag.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you Mathieu... The @attributes is what I was missing. Problem solved! Thank you again.
Why json_decode(json_encode? Why not just access them via the SimpleXML that already makes them accessible?
It does, but it was to stick to the original code as much as possible.
What do you mean by "It does"?
It does make sense to avoid the double json encode/decode. Sorry if it was unclear!
1

Assuming you have valid XML (which you should if it is a valid RSS fee) you can use the SimpleXML parser:

$xml = '<channel>
<item>
    <title> Title A </title>
    <description> Description A </description>
    <stuff url="google.com" type="search engine"/>
</item>

<item>
    <title> Title B </title>
    <description> Description B </description>
    <stuff url="yahoo.com" type="old stuff"/>
</item></channel>';
$x = new simplexmlelement($xml);
foreach($x->item as $item){
    echo $item->title . ' ' . $item->stuff['url'] . ' ' . $item->stuff['type'] . PHP_EOL;
}

https://3v4l.org/6ICrG

Note the stuff element here has been modified so it is self-closing.

2 Comments

Thank you for your answer... I went with Mathieu's solution below as it required less re-coding from what I had done.
@Hernandito Okay, doesn't really make sense to me. Why encode to decode? The SimpleXML has all the information already..
0

If the RSS content is like the one you posted on the example you should parse RSS feed using XML parser instead of using json_* functions.

Here are some simple examples using SimpleXML https://www.php.net/manual/en/simplexml.examples-basic.php

1 Comment

Thanks Tom...I will use this for reference.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.