Simplexml_load_string无法将我的字符串加载到 XML 中


Simplexml_load_string failed to load my string into XML

我在 PHP 中有这个字符串,我认为这个字符串已经像 XML 一样格式化得很好:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Books>
<ID>1</ID>
<Title>Programming 1</Title>
<ISBN>1234567890001</ISBN>
<Author>Kevin</Author>
<Abstract>This Book is about programming</Abstract>
</Books>


所以我使用 simplexml_load_string() 函数将此字符串转换为 xml,但问题是它从未返回为 xml,所以每次我执行 print_r() 时,我都会得到这样的结果:

SimpleXMLElement Object ( )

我认为它应该像往常一样包含所有树和节点,但返回的对象始终为 null。 我已经尝试了其他几种方法。但是它们都将给出相同的结果,即空白的XML对象。
那么我的字符串格式有问题吗? 或者我的代码可能有问题。 这是我的代码:

<?php
$ISBN=$_POST["ISBN"];
$url='http://localhost:8080/Programming/services/hello_world/DatabaseSearch?ISBN='.$ISBN;
$xmlstr = file_get_contents($url);
echo $xmlstr;
$xml=simplexml_load_string($xmlstr);
echo $xml->Books->Title;
print_r($xml);
?>

呼应$xml->书籍->标题;

仅返回空值。所以我不知道到底出了什么问题,我已经尝试解决这个问题好几天了,但没有结果。请帮助我..

你可以

通过echo $xml->Title;获得标题

实际上$xml=simplexml_load_string($xmlstr);返回根节点

此外,您在XML中没有其他节点,因此您可以获得标题echo $xml->Title;

有关更多详细信息,请参阅 SimpleXML 和 print_r() - 为什么为空?