PHP:如何从这个XML获取关联数组?

时间:2022-08-26 14:15:17

This is my code so far... needless to say, it's not working :(

到目前为止,这是我的代码……不用说,这是行不通的。

$feed = <<< THEXML

<programs>
<program>
<date>2009-04-16</date>
<start_time>17:00</start_time>
<leadtext>hello hello!
</leadtext>
<name>Program 1</name>
<b-line>Comedy</b-line>
<synopsis>Funny stuff
</synopsis>
<url>http://www.domain.tld/program_name</url>
</program>
<programs>
THEXML;


$xml  = (array) simplexml_load_string($feed);

print_r($xml);exit;

Would appreciate any help, have been around the php.net site for hours now and feeling braindead.

非常感谢大家的帮助,我们已经在php.net网站上呆了好几个小时了,感觉自己像个死人。

Please note that in the example xml above there is just one

请注意,在上面的示例xml中只有一个

 <program>...</program>

tag, but in reality I have one or more of them that I need to use. For example

标签,但实际上我有一个或多个标签需要使用。例如

 <programs>
 <program>...</program>
 <program>...</program>
 <program>...</program>
 </programs>

I figured if I can get one to work then I can loop it, but just thought I would explain what I am going for here.

我想如果我能让它工作,那么我就可以循环它,但是我想我可以解释我在这里要做什么。

Thanks in advance!

提前谢谢!

2 个解决方案

#1


2  

Assuming the provider gives you valid XML, then you just have to iterate through the programs and use any information you need off it, like so:

假设提供者提供了有效的XML,那么您只需遍历这些程序并使用您需要的任何信息,比如:

<?php
foreach ( $xml as $node ) {
    echo $node->synopsis;
    //or whatever property you want to access on the SimpleXMLElement Object
}

Since you don't have correct XML (being an unclosed <programs> tag, then you might want to modify your script to correct that error like so:

由于您没有正确的XML(作为未关闭的 标记,那么您可能需要修改您的脚本以纠正错误,如下所示:

$feed = preg_replace('/<programs>$/', '</programs>', trim($feed));

#2


2  

You're missing the end closing tag.

你错过了结束标签。

$feed = <<< THEXML

<programs>
<program>
<date>2009-04-16</date>
<start_time>17:00</start_time>
<leadtext>hello hello!
</leadtext>
<name>Program 1</name>
<b-line>Comedy</b-line>
<synopsis>Funny stuff
</synopsis>
<url>http://www.domain.tld/program_name</url>
</program>
</programs> // <========= here
THEXML;

#1


2  

Assuming the provider gives you valid XML, then you just have to iterate through the programs and use any information you need off it, like so:

假设提供者提供了有效的XML,那么您只需遍历这些程序并使用您需要的任何信息,比如:

<?php
foreach ( $xml as $node ) {
    echo $node->synopsis;
    //or whatever property you want to access on the SimpleXMLElement Object
}

Since you don't have correct XML (being an unclosed <programs> tag, then you might want to modify your script to correct that error like so:

由于您没有正确的XML(作为未关闭的 标记,那么您可能需要修改您的脚本以纠正错误,如下所示:

$feed = preg_replace('/<programs>$/', '</programs>', trim($feed));

#2


2  

You're missing the end closing tag.

你错过了结束标签。

$feed = <<< THEXML

<programs>
<program>
<date>2009-04-16</date>
<start_time>17:00</start_time>
<leadtext>hello hello!
</leadtext>
<name>Program 1</name>
<b-line>Comedy</b-line>
<synopsis>Funny stuff
</synopsis>
<url>http://www.domain.tld/program_name</url>
</program>
</programs> // <========= here
THEXML;