XML内容无法解析

huangapple go评论92阅读模式
英文:

xml content not parsing

问题

你创建了一个用于获取XML内容的函数,但出现了警告问题。警告信息是:「尝试在curlToGetXmlContent()函数中读取布尔属性“item”。」

这个警告的原因是在以下这行代码中:

$xml = simplexml_load_string($xml_file_url);

应该是要加载XML文件的内容而不是URL,应该修改为:

$xml = simplexml_load_string($xml_file);

这个修改应该能解决你在foreach循环中没有获取到内容id和title的问题。

英文:

I have created a function to get xml content and below is function to get xml content. curl is enabled on the server.XMl is validated on online xml validator tool.

function curlToGetXmlContent($url)
{
    $xml_file_url = $url;
    $curl = curl_init(); 
    // Setup curl. 
    curl_setopt($curl, CURLOPT_URL,"$xml_file_url"); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 
    
    $xml_file = curl_exec($curl);
    $err = curl_error($curl);
    
    curl_close($curl);
    
    $xml = simplexml_load_string($xml_file_url );
    if ($err) { 
        echo "cURL Error:" . $err;
    } else {
        return $xml->item;
    }  
}

Call function in the content

  $xmlContent = curlToGetXmlContent("https://example.com/abc.xml");
  foreach ($xmlContent as $content) {
        $var = $content->id.''. $content->title;
  } 

below is the xml of abc.xml file

<?xml version="1.0" encoding="UTF-8" ?>
<items>
  <item>
    <id>1</id>
    <title>Example title, example.</title>
  </item>
  <item>
    <id>2</id>
    <title>Example title -1, example.</title>
  </item>
  <item>
    <id>3</id>
    <title>Example title -2, example.</title>
  </item>
  <item>
    <id>4</id>
    <title>Example title -3, example.</title>
  </item>
  <item>
    <id>5</id>
    <title>Example title -4, example.</title>
  </item>
  <item>
    <id>6</id>
    <title>Example title -5, example.</title>
  </item>
  <item>
    <id>7</id>
    <title>Example title -5, example.</title>
  </item>
  <item>
    <id>8</id>
    <title>Example title -7, example.</title>
  </item>
  <item>
    <id>9</id>
    <title>Example title -8, example.</title>
  </item>
  <item>
    <id>10</id>
    <title>Example title -9, example.</title>
  </item>
  <item>
    <id>11</id>
    <title>Example title -10, example.</title>
  </item>
  <item>
    <id>12</id>
    <title>Example title -11, example.</title>
  </item>
  <item>
    <id>13</id>
    <title>Example title -12, example.</title>
  </item>
  <item>
    <id>14</id>
    <title>Example title -13, example.</title>
  </item>
  <item>
    <id>15</id>
    <title>Example title -14, example.</title>
  </item>
  <item>
    <id>16</id>
    <title>Example title -15, example.</title>
  </item>
  <item>
    <id>17</id>
    <title>Example title -16, example.</title>
  </item>
  <item>
    <id>18</id>
    <title>Example title -17, example.</title>
  </item>
  <item>
    <id>19</id>
    <title>Example title -18, example.</title>
  </item>
  <item>
    <id>20</id>
    <title>Example title -19, example.</title>
  </item>
  <item>
    <id>21</id>
    <title>Example title -20, example.</title>
  </item>
  <item>
    <id>22</id>
    <title>Example title -21, example.</title>
  </item>
  <item>
    <id>23</id>
    <title>Example title -22, example.</title>
  </item>
  <item>
    <id>25</id>
    <title>Example title -23, example.</title>
  </item>
  <item>
    <id>26</id>
    <title>Example title -24, example.</title>
  </item>
  <item>
    <id>27</id>
    <title>Example title -25, example.</title>
  </item>
  <item>
    <id>28</id>
    <title>Example title -26, example.</title>
  </item>
  <item>
    <id>29</id>
    <title>Example title -27, example.</title>
  </item>
  <item>
    <id>30</id>
    <title>Example title -28, example.</title>
  </item>
  <item>
    <id>31</id>
    <title>Example title -29, example.</title>
  </item>
</items>

I am not getting content id and title and getting warning :

1. Warning: Attempt to read property "item" on bool in curlToGetXmlContent() 

I am not sure i missed and why i am getting warning with any data render in foreach

答案1

得分: 1

以下是已翻译的内容:

问题似乎出在您加载XML内容并在curlToGetXmlContent函数中处理它的方式上。您应该将$xml_file而不是$xml_file_url传递给simplexml_load_string函数,其中包含使用cURL获取的实际XML内容。此外,您需要使用simplexml_load_string而不是simplexml_load_string,因为前者从字符串加载XML,而后者尝试从文件加载XML。

这是您函数的更新版本:

function curlToGetXmlContent($url)
{
    $xml_file_url = $url;
    $curl = curl_init(); 
    // 设置curl选项
    curl_setopt($curl, CURLOPT_URL, "$xml_file_url"); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 
    
    $xml_file = curl_exec($curl);
    $err = curl_error($curl);
    
    curl_close($curl);
    
    $xml = simplexml_load_string($xml_file); // 使用$xml_file而不是$xml_file_url
    if ($err) { 
        echo "cURL错误:" . $err;
    } else {
        return $xml->item;
    }  
}

通过这个更改,您的curlToGetXmlContent函数现在应该正确加载XML内容并按预期返回item元素。

另一个建议是将返回语句更改为返回所有项作为数组,而不仅仅是单个项,尤其是因为您在调用代码中使用了foreach循环。这是更新后的返回语句:

return $xml->item;

现在,在您的调用代码中,foreach循环应该能够正确工作,而不会产生警告:

$xmlContent = curlToGetXmlContent("https://example.com/abc.xml");
foreach ($xmlContent as $content) {
    $var = $content->id . ' ' . $content->title;
    echo $var . "<br>"; // 假设您想要打印每个项的id和title
}

输出:

1 Example title, example.
2 Example title -1, example.
3 Example title -2, example.
4 Example title -3, example.
5 Example title -4, example.
6 Example title -5, example.
7 Example title -5, example.
8 Example title -7, example.
9 Example title -8, example.
10 Example title -9, example.
11 Example title -10, example.
12 Example title -11, example.
13 Example title -12, example.
14 Example title -13, example.
15 Example title -14, example.
16 Example title -15, example.
17 Example title -16, example.
18 Example title -17, example.
19 Example title -18, example.
20 Example title -19, example.
21 Example title -20, example.
22 Example title -21, example.
23 Example title -22, example.
25 Example title -23, example.
26 Example title -24, example.
27 Example title -25, example.
28 Example title -26, example.
29 Example title -27, example.
30 Example title -28, example.
31 Example title -29, example.

希望这对您有所帮助。

英文:

The issue seems to be with the way you are loading the XML content and processing it in the curlToGetXmlContent function. Instead of passing $xml_file_url to the simplexml_load_string function, you should pass $xml_file, which contains the actual XML content fetched using cURL. Additionally, you need to use simplexml_load_string instead of simplexml_load_string, as the former loads XML from a string, while the latter attempts to load the XML from a file.

Here's the updated version of your function:

function curlToGetXmlContent($url)
{
    $xml_file_url = $url;
    $curl = curl_init(); 
    // Setup curl. 
    curl_setopt($curl, CURLOPT_URL,&quot;$xml_file_url&quot;); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 
    
    $xml_file = curl_exec($curl);
    $err = curl_error($curl);
    
    curl_close($curl);
    
    $xml = simplexml_load_string($xml_file); // Use $xml_file instead of $xml_file_url
    if ($err) { 
        echo &quot;cURL Error:&quot; . $err;
    } else {
        return $xml-&gt;item;
    }  
}

With this change, your curlToGetXmlContent function should now correctly load the XML content and return the item elements as expected.

One additional suggestion is to change the return statement to return all items as an array instead of just a single item, especially since you are using a foreach loop in your calling code. Here's the updated return statement:

Now, in your calling code, the foreach loop should work correctly and not produce the warning:

$xmlContent = curlToGetXmlContent(&quot;https://example.com/abc.xml&quot;);
foreach ($xmlContent as $content) {
    $var = $content-&gt;id . &#39; &#39; . $content-&gt;title;
    echo $var . &quot;&lt;br&gt;&quot;; // Assuming you want to print the id and title for each item
}

Output:

1 Example title, example.
2 Example title -1, example.
3 Example title -2, example.
4 Example title -3, example.
5 Example title -4, example.
6 Example title -5, example.
7 Example title -5, example.
8 Example title -7, example.
9 Example title -8, example.
10 Example title -9, example.
11 Example title -10, example.
12 Example title -11, example.
13 Example title -12, example.
14 Example title -13, example.
15 Example title -14, example.
16 Example title -15, example.
17 Example title -16, example.
18 Example title -17, example.
19 Example title -18, example.
20 Example title -19, example.
21 Example title -20, example.
22 Example title -21, example.
23 Example title -22, example.
25 Example title -23, example.
26 Example title -24, example.
27 Example title -25, example.
28 Example title -26, example.
29 Example title -27, example.
30 Example title -28, example.
31 Example title -29, example.

huangapple
  • 本文由 发表于 2023年8月4日 23:35:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定