Php Catalyst XML数据源
我试图从一个XML文件datafeed输出数据,支持文档非常糟糕,我在循环XML时遇到了问题Php Catalyst XML数据源,php,xml,Php,Xml,我试图从一个XML文件datafeed输出数据,支持文档非常糟糕,我在循环XML时遇到了问题 SimpleXMLElement Object ( [@attributes] => Array ( [version] => 6 [type] => WEB [date] => 20120220 ) [account] => SimpleXMLElement Object ( [@attributes] =&g
SimpleXMLElement Object (
[@attributes] => Array (
[version] => 6 [type] => WEB [date] => 20120220 )
[account] => SimpleXMLElement Object (
[@attributes] => Array (
[code] => XXXX
)
[sites] => SimpleXMLElement Object (
[site] => SimpleXMLElement Object (
[@attributes] => Array (
[code] => XXXX
)
[name] => Name [address1] => Address 1 [address2] => town [address3] => UK [county] => County [postcode] => Postcode [phone] => 0123456789 [fax] => 0123456789
[vehicles] => SimpleXMLElement Object (
[vehicle] => Array (
[0] => SimpleXMLElement Object (
[@attributes] => Array (
[code] => 5956 [new] => N
[engineSize] => 998
[mileage] => 8000
[type] => MC
[reg] => XXX
[regDate] => 20070414
[created] => 20110928090130
[modified] => 20110928090130
) [manufacturer] => Honda
[description] => CBR 1000 RR-7
[colour] => Black
[body] => Motorcycle
[fuel] => Petrol
[status] => In Stock
)
[1] => SimpleXMLElement Object (
[@attributes] => Array (
[code] => 5958
[new] => Y
[engineSize] => 125
[type] => MC
[created] => 20110930090254
[modified] => 20110930090254
)
[manufacturer] => Honda
[description] => WW125EX2A
[colour] => BLACK(NHA35)
[body] => SCOOTER
[status] => In Stock
[category] => SCOOTER
)
)
)
)
)
)
数据通过以下方式加载:
$request =
"<"."?xml version='1.0' encoding='iso-8859-1'?".">".
"<download account='XXXX' password='XXXX' version='6' request='EXP' format='XML' dealer='XXXX' vehicles='Y' />";
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/xml", "Content-Length: " . strlen($request)));
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'https://www.catalyst-findit.co.uk/download.php');
$response = curl_exec($ch);
$xml = simplexml_load_string($response);
print_r($xml);
虽然这似乎不起作用
如果有人能提供任何建议,请心存感激:)
提前谢谢
编辑XML文件(仅显示两辆车以便于阅读):
XXX
XXX
XXX
XXX
XXX
XXX
01472 123456
01472 123456
本田
CBR 1000 RR-7
黑色
摩托车
汽油
有现货的
本田
XXX
黑色(NHA35)
小型摩托车
有现货的
小型摩托车
使用XPath搜索特定节点数据
$text = <<<EOF
... XML data ...
EOF;
$xml = new SimpleXMLElement($text);
# register xpath prefix for default namespace
$namespaces = $xml->getDocNamespaces();
$xml->registerXPathNamespace('__ns', $namespaces['']);
# get first vehicle data
$result = $xml->xpath('//__ns:vehicles/__ns:vehicle[1]');
# get all vehicles in array
$vehicles = $xml->xpath('//__ns:vehicles/__ns:vehicle');
print_r($result); # or
echo($result[0]->colour);
foreach ($vehicles as $v) {
echo ($v->colour . "\n");
}
$text=xpath('//\uu-ns:vehicle/\uu-ns:vehicle[1]”);
#把所有车辆排成一排
$vehicles=$xml->xpath('//\u-ns:vehicles/\u-ns:Vehicler');
打印($result);#或
回声($result[0]->颜色);
foreach(车辆为$v){
回声($v->颜色。“\n”);
}
错误是什么?请尝试curl\u error
获取错误没有显示错误,print\u r
显示上述数据,我只是无法访问车辆数据。我测试并修复了(必须使用名称空间)示例代码:)谢谢!这很好,如果可能的话,你可以在一个循环中演示它,这样就不必指定车辆编号了吗?我添加了更多的示例代码。如果xpath中没有[1],则会得到数组中的所有车辆(如注释中所述)。
<?xml version="1.0" encoding="iso-8859-1"?>
<findit xmlns="http://www.catalyst-findit.co.uk/download" version="6" type="WEB" date="20120220">
<account code="XXX">
<sites>
<site code="BRI">
<name>XXX</name>
<address1>XXX</address1>
<address2>XXX</address2>
<address3>XXX</address3>
<county>XXX</county>
<postcode>XXX</postcode>
<phone>01472 123456</phone>
<fax>01472 123456</fax>
<vehicles>
<vehicle code="XXX" new="N" engineSize="998" mileage="8000" type="MC" reg="XXX" regDate="20070414" created="20110928090130" modified="20110928090130">
<manufacturer>Honda</manufacturer>
<description>CBR 1000 RR-7</description>
<colour>Black</colour>
<body>Motorcycle</body>
<fuel>Petrol</fuel>
<status>In Stock</status>
</vehicle>
<vehicle code="XXX" new="Y" engineSize="125" type="MC" created="20110930090254" modified="20110930090254">
<manufacturer>Honda</manufacturer>
<description>XXX</description>
<colour>BLACK(NHA35)</colour>
<body>SCOOTER</body>
<status>In Stock</status>
<category>SCOOTER</category>
</vehicle>
</vehicles>
</site>
</sites>
</account>
</findit>
$text = <<<EOF
... XML data ...
EOF;
$xml = new SimpleXMLElement($text);
# register xpath prefix for default namespace
$namespaces = $xml->getDocNamespaces();
$xml->registerXPathNamespace('__ns', $namespaces['']);
# get first vehicle data
$result = $xml->xpath('//__ns:vehicles/__ns:vehicle[1]');
# get all vehicles in array
$vehicles = $xml->xpath('//__ns:vehicles/__ns:vehicle');
print_r($result); # or
echo($result[0]->colour);
foreach ($vehicles as $v) {
echo ($v->colour . "\n");
}