3

I am currently using the following code, but no result is returned:

<?php   
    $url = 'http://myknowledge.org.uk/xml';
    $xml = new SimpleXMLElement(file_get_contents($url));
    foreach ($xml->item as $item) {
        $title = $item->title;
    }

    echo $title;    
?>

The XML Code:

<?xml version="1.0" encoding="utf-8"?>
<item>
    <title>Apple</title>
    <notableFor>Fruit</notableFor>
    <wikiid>Apple</wikiid>
    <description>The apple is the pomaceous fruit of the apple tree, Malus domestica of the rose family. It is one of the most widely cultivated tree fruits, and the most widely known of the many members of genus Malus that are used by humans.</description>
    <img></img>
    <website></website>
    <translate>
        <de>Äpfel</de>
        <fr>pomme</fr>
        <it>mela</it>
        <es>manzana</es>
        <ru>яблоко</ru>
    </translate>
    <nutritionalInfomation name="Apple" quantity="100g">
        <calories>52</calories>
        <carb>14</carb>
        <fibre>2.4</fibre>
        <protein>0.3</protein>
        <fat>0.2</fat>
    </nutritionalInfomation>
</item>

If anybody has an idea how to fix this I would love to know. Thanks.

William Perron
  • 485
  • 7
  • 16
user3287037
  • 63
  • 1
  • 1
  • 6
  • Is your xml well formatted? – toesslab Sep 13 '14 at 20:16
  • That is a fair point.. No being the answer... Sorry. – user3287037 Sep 13 '14 at 20:18
  • because of the `'String could not be parsed as XML'` – toesslab Sep 13 '14 at 20:18
  • There was a mistake in the XML code, which I have now corrected, but this does not seem to make a difference. – user3287037 Sep 13 '14 at 20:22
  • Just a side note: “Äpfel” is the plural of “Apfel”. – Gumbo Sep 13 '14 at 20:30
  • I shall endeavour to correct. Thanks. – user3287037 Sep 13 '14 at 20:32
  • please consult the PHP manual on how you can track, report and/or display errors. Additionally, please consult the PHP manual about dealing with exceptions. A reference question is here: http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php - another one I closed your question against, specifically http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12772851#12772851 – hakre Sep 14 '14 at 18:34

2 Answers2

7

The XML appears to be invalid in lines 4 and 5:

<notableFor>Fruit</title>
<wikiid>Apple</title>

Which should be:

<notableFor>Fruit</notableFor>
<wikiid>Apple</wikiid>

I recommend using the XML validator at http://www.w3schools.com/xml/xml_validator.asp to debug errors with your XML code.

Also, as your root element is item, you may want to change your PHP code:

<?php   
    $url = 'http://myknowledge.org.uk/xml';
    $item = new SimpleXMLElement(file_get_contents($url));
    $title = $item->title;
    $description = $item->description;
?>

(I don't have a copy of PHP on hand to test this, but according to http://php.net/manual/en/simplexml.examples-basic.php, this should work)

Peter
  • 2,654
  • 2
  • 33
  • 44
0

Xml can have only 1 root element, in your example the root is item

When loading to SimpleXML you can get the root name with $xml->getName()

$url = 'http://myknowledge.org.uk/xml';
$item = new SimpleXMLElement($url, null, true);
$title = $item->title;
$description = $item->description;

Or you should enclose you items in another root i.e items if you need multiple

Imre L
  • 6,159
  • 24
  • 32