解释XML字符串以使用PHP获取其中的一部分



我有一个XML文件:

<rss version="2.0">
<channel>
-<title>Match Probabilities</title>
 -<link>abc[dot]com</link>
  -<description>abc.com RSS feed - match results and soccer predictions.</description>
    -<item>
      -<title>Manchester City v Watford</title>
       -<link>abc[dot]com/h2h/manchester-city-watford/</link>
       -<pubDate>05/18/2019</pubDate>
       -<description><a href="abc[dot]com/h2h/manchester-city-watford/" target="_blank">Manchester City v Watford</a><br/><br/><table border="1" width="100%"><tr><td width="40%"><strong>Result prediction</strong></td> <td>Manchester City to win</td></tr><tr><td><strong>Over/Under prediction</strong></td><td>over 2.5 goals</td></tr><tr><td><strong>HT / FT prediction</strong></td><td>draw / Manchester City to win</td></tr><tr><td><strong>Goal Difference prediction</strong></td><td>2 goals</td></tr><tr><td><strong>Total Goals prediction</strong></td><td>6 goals</td></tr><tr><td><strong>Team to Score prediction</strong></td><td>both teams</td></tr><tr><td><strong>Team to Win without Conceding a Goal prediction</strong></td><td>none</td></tr><tr><td><strong>Anytime Goalscorer prediction</strong></td><td>S. Agüero(Manchester City)<br/>Gabriel Jesus(Manchester City)<br/>P. Foden(Manchester City)<br/></td></tr></table><br/>
      </description>
   </item>
  </channel>
</rss>

我尝试了此代码,我确实成功地输出了字符串。

$xml = simplexml_load_file(file.xml); 
foreach($xml->channel->item as $item){        
    $html .= $item->description;
    $html .= "<hr />";  
}
echo $html;

但是,我的期望是我只想在description属性中获得table

我还尝试了$html .= $item->description->table->Attribute('<tr>';//第7行,但我失败了。错误消息看起来像这样:

    Fatal error: Call to undefined method SimpleXMLElement::Attribute() in /home/content/12/1232354/html/views/file.html on line 7.

您的帮助将不胜感激。谢谢

$xml = simplexml_load_file("file.xml");
foreach($xml->channel->item as $item){
    echo '<pre>'; print_r($item->description->table);
    //You cannot get the table data as string. You have to process them so echo  $html don't show anything
    $html .= $item->description->table;
    $html .= "<hr />";  
}
echo $html;

希望这可以帮助您找到表值。您必须使用 ->表示法遍历值,因为它始终使用对象表示法找到。

如果您需要表格的完整XML,而不仅仅是字符串值,则需要将行更改为...

$html .= $item->description->table->asXML();

因此,这将在<description>元素中获取<table>元素,并使用asXML()重新创建源文档的原始XML。

,您的代码将是...

$xml = simplexml_load_file(file.xml); 
foreach($xml->channel->item as $item){        
    $html .= $item->description->table->asXML();
    $html .= "<hr />";  
}
echo $html;

编辑:

可能是描述具有编码的HTML合格,当在浏览器中查看时,它们看起来还可以,但是源实际上具有所有&gt;类型代码。如果是这种情况,那么您可以提取描述,然后将其解码并再次加载到新文档中(您需要添加一个虚拟根节点,因为它是Actuall Actuall a文档片段(...

$xml = simplexml_load_file("file.xml");
$html = '';
foreach($xml->channel->item as $item){
    $desc = html_entity_decode((string)$item->description);
    $descXML = simplexml_load_string('<desc>'.$desc.'</desc>');
    $html .= $descXML->table->asXML();
    $html .= "<hr />";
}
echo $html;

要解析XML您可以使用domdocument,并获取所有"表"元素,这些元素是"描述"元素的后代,请使用XPATH //description//table,然后获取后,表元素,您可以使用textContent属性获得他们的文本内容,类似的内容:

$domd=@DOMDocument::loadHTML($xml);
$xp=new DOMXPath($domd);
foreach($xp->query("//description//table") as $table){
    var_dump($table->textContent);
}

最新更新