如何在PHP中解析XML文件?

问题描述:

我想解析rss xml送纸器,但在解析时有一些内容丢失。我已经附上了我的RSS饲料和解析的反应,请告诉我,如果有其他方式?如何在PHP中解析XML文件?

<?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); 
+0

我意识到我的答案已被破坏(对不起,在移动设备上写道),所以我删除了它。 SimpleXML不支持访问节点之间的数据。在[此答案](http://*.com/a/20177247/2057919)中查看解决方案/解决方法。 –

如果你错过了一些内容,它是由该饲料的不良设计(每个值造成的应该以一个元素为边界),所以它不能通过simplexml_load_ *函数完全解析。

如果您需要使用此提要,您需要通过一些正则表达式函数解析它,或者也可能有一些库可以解析这些提要。