String could not be parsed as XML

This question already has an answer here:

  • Reference - What does this error mean in PHP? 30 answers

  • 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)


    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 ie items if you need multiple

    链接地址: http://www.djcxy.com/p/69404.html

    上一篇: 注意:未定义的索引:在C:\ xampp \ htdocs \

    下一篇: 字符串不能解析为XML