0

I've had a look round similar articles such as this one and I can't quite get it to work, quite possible I'm just misunderstanding.

I've got a simple script which parses a bit of xml and prints out specific fields - what I'm having trouble doing is accessing the data of SimpleXMLElement Objects.

XML (simplified for clarity)

<channel>
  <item> 
    <title><![CDATA[Title is in here ...]]></title> 
    <description>Our description is in here!</description>
  </item>
</channel>

PHP

$url = "file.xml";
$xml = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA);

foreach ($xml->channel->item as $item) {
$articles = array();
$articles['title'] = $item->title;
$articles['description'] = $item->description;
}

Up to this point, everything seems ok. I end up with an array of content which I can confirm with print_r, this is what I get back:

Array
(
    [title] => SimpleXMLElement Object
        (
            [0] => Title is in here ...
        )

    [description] => SimpleXMLElement Object
        (
            [0] => Our description is in here!
        )
)

The key question

How do I then access [title][0] or [description][0]?

I've tried a couple of variations with no success, most likely a rookie error somewhere!

foreach ($articles as $article) {
        echo $article->title;
    }

and

foreach ($articles as $article) {
        echo $article['title'][0];
    }

and

foreach ($articles as $article) {
        echo $article['title'];
    }
2
  • Is there a special reason why you want to stuff each $item->title and $item->description in an array? I.e. why can't you simply use the simplexmlelement where you need it? Commented Aug 19, 2010 at 12:27
  • There are likely to be numerous sets of title/descriptions - I want to gather them together then iterate through them with another process ... if that sounds like the wrong process though I would love other suggestions :) Commented Aug 19, 2010 at 12:35

2 Answers 2

1

If you really don't want to simply pass the SimpleXMLelement but put the values in an array first....

<?php
// $xml = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA);
$xlm = getData();

$articles = array();
foreach ($xml->channel->item as $item) {
  // with (string)$item->title you get rid of the SimpleXMLElements and store plain strings
  // but you could also keep the SimpleXMLElements here - the output is still the same.
  $articles[] = array(
    'title'=>(string)$item->title, 
    'description'=>(string)$item->description
  );
}

// ....
foreach( $articles as $a ) {
  echo $a['title'], ' - ', $a['description'], "\n";
}

function getData() {
  return new SimpleXMLElement('<foo><channel>
    <item> 
      <title><![CDATA[Title1 is in here ...]]></title> 
      <description>Our description1 is in here!</description>
    </item>
    <item> 
      <title><![CDATA[Title2 is in here ...]]></title> 
      <description>Our description2 is in here!</description>
    </item>
  </channel></foo>');
}

prints

Title1 is in here ... - Our description1 is in here!
Title2 is in here ... - Our description2 is in here!
Sign up to request clarification or add additional context in comments.

1 Comment

Nailed it. Didn't know about (string)$object->value ... very helpful.
0

I think you have an error when you assign value to to array:

foreach ($xml->channel->item as $item) {
    $articles = array();
    $articles['title'] = $item->title;
    $articles['description'] = $item->description;
}

if you have foreach why are your creating on every step new array $articles = array();

$articles = array();
foreach ($xml->channel->item as $item) {
        $articles['title'] = $item->title;
        $articles['description'] = $item->description;
}

2 Comments

Even then you only get a maximum of one article since the next iteration overwrites the old values.
Quite right, I've only been testing with one set of values so far - the main question still stands despite my crappy code :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.