Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating an XML file, via SimpleXML & simple_html_dom
    text
    copied!<p>Hope the title was OK.</p> <p>My problem is that I want to generate an XML file, which contains all the ISO 4217 currencies, including their name, code and countries they are used in.</p> <p>To do this I'm using simple_html_dom to grab the HTML, and select specific data from the page. Then using SimpleXML to construct the XML. I would like the output like so:</p> <pre><code>&lt;currency&gt; &lt;code&gt;USD&lt;/code&gt; &lt;name&gt;United States Dollars&lt;/name&gt; &lt;location&gt;United States of America&lt;/location&gt; &lt;/currency&gt; </code></pre> <p>At the moment I can populate the code for all the codes, but cannot get the names or locations as well as the codes wrapped in currency</p> <p>Here is the current code I have, the second for loop returns the names of the currency, but I can't figure out how to place this below the code tag within currency:</p> <pre><code>&lt;?php //Source: simplehtmldom.sourceforge.net require('simple_html_dom.php'); //177 currencies //set URL to parse $url = "http://en.wikipedia.org/wiki/ISO_4217"; $html = file_get_html($url); //find all &lt;td&gt; elements that are nested within &lt;table class="wikitable"&gt;&lt;tr&gt; and put them into an array $content = $html-&gt;find('table.wikitable tr td'); $newsXML = new SimpleXMLElement("&lt;currencies&gt;&lt;/currencies&gt;"); $newsXML-&gt;addAttribute('type', 'ISO_4217'); Header('Content-type: text/xml'); //loop to add each currency code in &lt;currency&gt;&lt;code&gt;HERE&lt;/code&gt;&lt;/currency&gt; //this loop gets all the codes of the currencies for($i = 0; $i &lt;= 885; $i += 5){ $currency = $newsXML-&gt;addChild('currency'); $code = $currency-&gt;addChild('code',strip_tags($content[$i])); } //this loop gets all the names of the currencies for($n = 3; $n &lt;= 531; $n += 5){ $name = $currency-&gt;addChild('name',strip_tags($content[$n])); } //echo the XML echo $newsXML-&gt;asXML(); ?&gt; </code></pre> <p>I have only started learning PHP a month or so back, so would appreciate any advice, or a point in the correct direction.</p> <p>(Hope the formatting/title naming convention is OK).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload