Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法将PHP-字符串解析为XML_Php_Xml_Simplexml - Fatal编程技术网

无法将PHP-字符串解析为XML

无法将PHP-字符串解析为XML,php,xml,simplexml,Php,Xml,Simplexml,我当前正在使用以下代码,但未返回任何结果: <?php $url = 'http://myknowledge.org.uk/xml'; $xml = new SimpleXMLElement(file_get_contents($url)); foreach ($xml->item as $item) { $title = $item->title; } echo $title; ?> <?x

我当前正在使用以下代码,但未返回任何结果:

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

    echo $title;    
?>
<?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>
项目作为$item){
$title=$item->title;
}
echo$标题;
?>
XML代码:

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

    echo $title;    
?>
<?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>

苹果
果
苹果
苹果是蔷薇科苹果树的苹果状果实。它是栽培最广泛的树木果实之一,也是人类使用的苹果属许多成员中最广为人知的。
Äpfel
波姆
梅拉
曼扎纳
яблоко
52
14
2.4
0.3
0.2

如果有人知道如何解决这个问题,我很想知道。谢谢。

第4行和第5行中的XML似乎无效:

<notableFor>Fruit</title>
<wikiid>Apple</title>
水果
苹果
应该是:

<notableFor>Fruit</notableFor>
<wikiid>Apple</wikiid>
水果
苹果
我建议使用位于的XML验证器来调试XML代码中的错误

此外,由于根元素是item,您可能需要更改PHP代码:

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

(我手头没有一份PHP副本来测试这一点,但根据,这应该可以工作)

Xml只能有一个根元素,在您的示例中,根元素是

加载到SimpleXML时,可以使用
$xml->getName()


或者,您应该将您的项目包含在另一个根目录中,即
items
如果您需要多个您的xml格式是否正确?这是一个公平的观点。。没有答案。。。抱歉。由于
“字符串无法解析为XML”
,XML代码中有一个错误,我现在已经更正了,但这似乎没有什么区别。只需补充说明:“Äpfel”是“Apfel”的复数形式。我刚刚更正了此错误,但回显了“$title”仍然没有返回值:我已经用更新的PHP代码编辑了我的答案。