将外部xml提要解析为php
我对php非常陌生,我试图将外部xml提要中的数据加载到php文档中,然后使用该数据生成输出 im使用的xml提要是- 我想做的是生成一个“市场”列表,并列出这些市场的名称,因此在编写列表的前3项时,xml提要是:将外部xml提要解析为php,php,xml,xml-parsing,simplexml,Php,Xml,Xml Parsing,Simplexml,我对php非常陌生,我试图将外部xml提要中的数据加载到php文档中,然后使用该数据生成输出 im使用的xml提要是- 我想做的是生成一个“市场”列表,并列出这些市场的名称,因此在编写列表的前3项时,xml提要是: 苏格兰1区-直接-直接 邓巴顿对汉密尔顿-上半场结果/下半场结果 邓巴顿对汉密尔顿-比赛障碍 目前,我正试图使用下面的代码来实现这一点,但我很快就一事无成了,关于我在这里做错了什么有什么想法吗 只是另一个背景,我使用PHP5.4.4,我认为simplexml已经预装了,对吗。。所
- 苏格兰1区-直接-直接
- 邓巴顿对汉密尔顿-上半场结果/下半场结果
- 邓巴顿对汉密尔顿-比赛障碍
您需要深入xml以获取市场,然后获取市场的详细信息:
<?php
$xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=1&marketSort=--&filterBIR=N');
foreach ($xml->response->williamhill->class->type as $type) {
$type_attrib = $type->attributes();
echo "<p><h2>Type ".$type_attrib['id'].": ".$type_attrib['name']."</h2>";
foreach ($type->market as $event) {
$event_attributes = $event->attributes();
echo $event_attributes['name']."<br />";
//commented out the following which prints all attributes
//replaced by above to just print the name
/*
echo "<p>";
foreach($event->attributes() as $attrib=>$value) {
echo "$attrib: $value <br />";
}
echo "</p>";
*/
}
echo "</p>";
}
例如,您可以显示参与者的姓名和相应的赔率:
<?php
$xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=1&marketSort=--&filterBIR=N');
$data = $xml->response->williamhill->class->type->market;
$ps = $data->participant;
foreach($ps as $p)
{
echo $p['name']." - ".$p['odds']."<br />";
}
?>
问题出在哪里?它就在这里工作。尝试打印r($xml)@IvoPereira使用print\r($xml)管理整个提要;但是我在挑选Hi@sam时遇到了麻烦,我需要这个旧api的WSDL(因为v2受一个我无法获得的ApiKey保护)。你有没有可能得到wsdl??很多感谢顺便说一句,当您在测试时,您应该将xml文件保存到本地文件并从那里加载它,这样会更快并节省带宽。感谢bencoder,它可以格式化xml数据的输出,但它会输出给定的每个属性,而不仅仅是nameHi@sam,我更改了它,因此它将只打印名称部分。我注释掉了打印所有属性的部分,以便您可以进行比较,以帮助您理解。非常感谢这一点,能够将其区分开来并查看每一位的功能确实很有帮助。再次感谢
<?php
$xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=1&marketSort=--&filterBIR=N');
$data = $xml->response->williamhill->class->type->market;
$ps = $data->participant;
foreach($ps as $p)
{
echo $p['name']." - ".$p['odds']."<br />";
}
?>