通过 SimpleXML & 创建 XML 文件simple_html_dom
希望标题没问题。
我的问题是我想生成一个 XML 文件,其中包含所有 ISO 4217 货币,包括它们的名称、代码和使用的国家/地区。
为此,我使用 simple_html_dom 获取 HTML,并从中选择特定数据页面。然后使用SimpleXML 构建XML。我希望输出如下:
<currency>
<code>USD</code>
<name>United States Dollars</name>
<location>United States of America</location>
</currency>
目前我可以填充所有代码的代码,但无法获取名称或位置以及货币中包含的代码
这是我当前的代码,第二个 for 循环返回货币的名称,但我不知道如何将其放置在货币内的代码标签下方:
<?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 <td> elements that are nested within <table class="wikitable"><tr> and put them into an array
$content = $html->find('table.wikitable tr td');
$newsXML = new SimpleXMLElement("<currencies></currencies>");
$newsXML->addAttribute('type', 'ISO_4217');
Header('Content-type: text/xml');
//loop to add each currency code in <currency><code>HERE</code></currency>
//this loop gets all the codes of the currencies
for($i = 0; $i <= 885; $i += 5){
$currency = $newsXML->addChild('currency');
$code = $currency->addChild('code',strip_tags($content[$i]));
}
//this loop gets all the names of the currencies
for($n = 3; $n <= 531; $n += 5){
$name = $currency->addChild('name',strip_tags($content[$n]));
}
//echo the XML
echo $newsXML->asXML();
?>
我大约一个月前才开始学习 PHP,因此将不胜感激任何建议或正确方向的观点。
(希望格式/标题命名约定没问题)。
Hope the title was OK.
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.
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:
<currency>
<code>USD</code>
<name>United States Dollars</name>
<location>United States of America</location>
</currency>
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
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:
<?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 <td> elements that are nested within <table class="wikitable"><tr> and put them into an array
$content = $html->find('table.wikitable tr td');
$newsXML = new SimpleXMLElement("<currencies></currencies>");
$newsXML->addAttribute('type', 'ISO_4217');
Header('Content-type: text/xml');
//loop to add each currency code in <currency><code>HERE</code></currency>
//this loop gets all the codes of the currencies
for($i = 0; $i <= 885; $i += 5){
$currency = $newsXML->addChild('currency');
$code = $currency->addChild('code',strip_tags($content[$i]));
}
//this loop gets all the names of the currencies
for($n = 3; $n <= 531; $n += 5){
$name = $currency->addChild('name',strip_tags($content[$n]));
}
//echo the XML
echo $newsXML->asXML();
?>
I have only started learning PHP a month or so back, so would appreciate any advice, or a point in the correct direction.
(Hope the formatting/title naming convention is OK).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题是第二个循环中的
$currency
是静态的,导致类似You need to add name and code to the same
$currency
object 的情况。Your problem is that the
$currency
in the second loop is static, resulting in something likeYou need to add both name and code to the same
$currency
object.我从未找到这个问题的答案,最终只是
echo
'ing XML,而不是通过SimpleHTMLDom 构建它。I never found the answer to this issue, and just ended up
echo
'ing the XML rather than constructing it via SimpleHTMLDom.