如何在php中解析XML文件?

时间:2015-11-06 06:00:37

标签: php xml rss

我正在尝试解析rss xml feed,但在解析时有一些内容丢失了。我已经在下面添加了我的RSS提要和解析的响应,请提示我是否有其他方法?

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<item>
<ids>701</ids>
<post_date>2015-10-30 12:51:30</post_date>
<post_content><blockquote>I personally feel that there is a need to re?invent the very dynamic of contemporary art, to approach it afresh...     </blockquote>

Since 2008, the Devi Art Foundation has been making waves as a unique non?profit centre for contemporary artistic practice in India.

<strong>Heading:</strong> As founder of the Devi Art Foundation, you pioneered a unique and avant?

<strong>Anupam Poddar: </strong>There are many factors that culminated in the Foundation being established.
 </post_content> 
 </item>
 </channel>

响应是这样的

 Array
 (
 [@attributes] => Array
    (
        [version] => 2.0
    )

 [channel] => Array
    (
        [item] => Array
            (
                [0] => Array
                    (

                        [ids] => 701
                        [post_date] => 2015-10-30 12:51:30
                        [post_content] => Array
                            (
                                [blockquote] => I personally feel that there is a need to re?invent the very dynamic of contemporary art, to approach it afresh...
                                [strong] => Array
                                    (
                                        [0] => Heading:
                                        [1] => Anupam Poddar: 

                                    )
                            )
                    )
            )
        )
      )

我的解析方法是

  $xml   = simplexml_load_string($details, "SimpleXMLElement",        LIBXML_NOCDATA);
  $json  = json_encode($xml);
  $array = json_decode($json, TRUE);

1 个答案:

答案 0 :(得分:0)

如果你遗漏了一些内容,这是因为这个Feed的设计不合理(每个值应该以一个元素为边界),所以不能通过simplexml_load_ *函数进行完整的解析。

如果你需要使用这个feed你需要通过一些正则表达式函数解析它,或者也许有一些库也可以解析这些feed。