1

I have an XML file like this:

<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>

I have tried this code, and I did succeed in outputting the string.

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

However, my expectation is that I just want to get the table in the description attribute only.

I have also tried $html .= $item->description->table->Attribute('<tr>'; //line 7, but I failed. the error message looked like this:

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

Your help will be appreciated. Thanks

Phuong Ng
  • 59
  • 7
  • So if you just want the values in the table, did you try `$html .= $item->description->table;`? – miken32 May 18 '19 at 05:01
  • @miken32 Yes, and it returned blank page without any warning message. – Phuong Ng May 18 '19 at 08:29
  • Possible duplicate of [How do you parse and process HTML/XML in PHP?](https://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – hanshenrik May 18 '19 at 09:34
  • ```$domd=@DOMDocument::loadHTML($xml);$xp=new DOMXPath($domd);foreach($xp->query("//description//table") as $table){var_dump($table->textContent);}``` – hanshenrik May 18 '19 at 09:36

3 Answers3

0
$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;

Hope this can help you find the table value. you have to traverse value using -> notation as it will always be found using object notation.

  • use $html .= $item->description->table->asXML(); instead of $item->description->table; to get the whole table data.. – Brain Developer May 18 '19 at 08:32
  • `$xml = simplexml_load_file("file.xml"); $html = ''; foreach($xml->channel->item as $item){ //echo '
    '; 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->asXML();;
        $html .= "
    "; } echo $html;` @phuong-ng
    – Brain Developer May 18 '19 at 08:37
  • Try this and let me know if you are getting result or not ? @PhuongNg – Brain Developer May 18 '19 at 08:42
  • Your piece of code is very much the same as Nigel Ren's answer below, and it returned blank page without any warning message. – Phuong Ng May 18 '19 at 08:42
  • @PhuongNg It should run proprtly check if your server is running and just check any simple code if it is running okay or not. and check the file location it may be not there. and check as correct xml file or not. – Brain Developer May 18 '19 at 08:51
0

If you need the full XML of the table and not just the string values, you will need to change the line to...

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

So this takes the <table> element in the <description> element and recreates the original XML of the source document using asXML().

So your code would be...

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

Edit:

It could be that the description has the HTML entitites encoded, which when viewed in a browser may look OK, but the source actually has all of the &gt; type codes. If this is the case, then you can extract the description, then decode it and load it again in a new document (you need to add a dummy root node as it's actuall a document fragment)...

$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;
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • So can you help give me the complete code for this as I don't know how to echo the only `` element. Sorry for this, but your help is appreciated.
    – Phuong Ng May 18 '19 at 08:31
  • Just tried, and it returned blank page without any warning message. – Phuong Ng May 18 '19 at 08:40
  • I tried _echo $xml->asXML();_, but it returned the whole things, but not the table element only. Any idea? – Phuong Ng May 18 '19 at 08:54
  • your edited piece of code worked perfectly. Thanks very much. – Phuong Ng May 18 '19 at 08:57
  • If this has worked then please consider marking it as answered - https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Nigel Ren May 18 '19 at 12:36
0

to parse the XML you can use DOMDocument, and to get all "table" elements that is a descendant of a "description" element, use the xpath //description//table, and once you've fetched the table elements, you can get their text content with the textContent property, something like this:

$domd=@DOMDocument::loadHTML($xml);
$xp=new DOMXPath($domd);
foreach($xp->query("//description//table") as $table){
    var_dump($table->textContent);
}
hanshenrik
  • 19,904
  • 4
  • 43
  • 89