Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP解释XML字符串以获取其中的一部分_Php_Xml - Fatal编程技术网

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

使用PHP解释XML字符串以获取其中的一部分,php,xml,Php,Xml,我有这样一个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> -<

我有这样一个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>
    Fatal error: Call to undefined method SimpleXMLElement::Attribute() in /home/content/12/1232354/html/views/file.html on line 7.

谢谢你的帮助。谢谢

$xml=simplexml\u load\u文件(“file.xml”);
$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;
foreach($xml->channel->item as$item){ 回显“”;打印($item->description->table); //无法将表数据作为字符串获取。必须对其进行处理,以便echo$html不显示任何内容 $html.=$item->description->table; $html.=“
”; } echo$html;

希望这能帮你找到表中的值。您必须使用->表示法遍历值,因为它总是使用对象表示法找到的。

$xml=simplexml\u load\u file(“file.xml”); foreach($xml->channel->item as$item){ 回显“”;打印($item->description->table); //无法将表数据作为字符串获取。必须对其进行处理,以便echo$html不显示任何内容 $html.=$item->description->table; $html.=“
”; } echo$html;
希望这能帮你找到表中的值。您必须使用->表示法遍历值,因为它总是使用对象表示法找到。

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

$xml = simplexml_load_file(file.xml); 
foreach($xml->channel->item as $item){        
    $html .= $item->description->table->asXML();
    $html .= "<hr />";  
}
echo $html;
因此,这将获取
元素中的
元素,并使用重新创建源文档的原始XML

所以你的代码是

$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,而不仅仅是字符串值,则需要将行更改为

$xml = simplexml_load_file(file.xml); 
foreach($xml->channel->item as $item){        
    $html .= $item->description->table->asXML();
    $html .= "<hr />";  
}
echo $html;
因此,这将获取
元素中的
元素,并使用重新创建源文档的原始XML

所以你的代码是

$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,要获取作为“description”元素后代的所有“table”元素,可以使用xpath
//description//table
,一旦获取了table元素,就可以使用
textContent
属性获取它们的文本内容,如下所示:

<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>
    Fatal error: Call to undefined method SimpleXMLElement::Attribute() in /home/content/12/1232354/html/views/file.html on line 7.

要解析XML,可以使用DOMDocument,要获取作为“description”元素后代的所有“table”元素,可以使用xpath
//description//table
,一旦获取了table元素,就可以使用
textContent
属性获取它们的文本内容,如下所示:

<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>
    Fatal error: Call to undefined method SimpleXMLElement::Attribute() in /home/content/12/1232354/html/views/file.html on line 7.


因此,如果您只是想要表中的值,您是否尝试过
$html.=$item->description->table?@miken32是,它返回空白页,没有任何警告消息。可能重复
$domd=@DOMDocument::loadHTML($xml)$xp=新的DOMXPath($domd);foreach($xp->query(“//description//table”)as$table){var_dump($table->textContent);}
因此,如果您只是想要表中的值,是否尝试了
$html.=$item->description->table?@miken32是,它返回空白页,没有任何警告消息。可能重复
$domd=@DOMDocument::loadHTML($xml)$xp=新的DOMXPath($domd);foreach($xp->query(//description//table)as$table){var_dump($table->textContent);}
所以你能帮我提供完整的代码吗,因为我不知道如何回显唯一的
元素。对此表示抱歉,但非常感谢您的帮助。刚刚尝试,它返回了空白页,没有任何警告消息。我尝试了echo$xml->asXML();,但是它返回了全部内容,而不仅仅是table元素。你知道吗?你编辑的代码工作得很好。非常感谢。如果这是有效的,请考虑把它标记为回答-所以你能帮我完整的代码,因为我不知道如何回应唯一的<代码> <代码>元素。对此表示抱歉,但非常感谢您的帮助。刚刚尝试,它返回了空白页,没有任何警告消息。我尝试了echo$xml->asXML();,但是它返回了全部内容,而不仅仅是table元素。你知道吗?你编辑的代码工作得很好。非常感谢。如果这已经生效,请考虑将它标记为应答-使用$html。= $item > >描述>表-> asxML();而不是$item->description->table;要获取整个表数据,..
$xml=simplexml\u load\u文件(“file.xml”)$html='';foreach($xml->channel->item as$item){//echo'';print_r($item->description->table);//无法将表数据作为字符串获取。必须对其进行处理,以便echo$html不显示任何$html。=$item->description->table->asXML();$html.=“
”;}echo$html@phuong Ng试试这个,告诉我你是否得到了结果@Phuongg您的代码与下面Nigel Ren的答案非常相似,它返回空白页,没有任何警告消息。@Phuongg它应该正确运行,检查您的服务器是否正在运行,检查任何简单的代码是否正常运行。并检查文件位置,它可能不在那里。并检查xml文件是否正确;而不是$item->description->table;要获取整个表数据,..
$xml=simplexml\u load\u文件(“file.xml”)$html='';foreach($xml->channel->item as$item){//echo'';print_r($item->description->table);//无法将表数据作为字符串获取。必须对其进行处理,以便echo$html不显示任何$html。=$item->description->table->asXML();$html.=“
”;}echo$html@phuong Ng试试这个,告诉我你是否得到了结果@Phuongg您的代码与下面Nigel Ren的答案非常相似,它返回空白页,没有任何警告消息。@Phuongg它应该正确运行,检查您的服务器是否正在运行,检查任何简单的代码是否正常运行。并检查文件位置,它可能不在那里。并检查xml文件是否正确。