【发布时间】:2012-12-11 17:08:55
【问题描述】:
我正在尝试将 xpath 与 DOMDocument 结合使用来尝试解析我的 xml 并插入到表中。
除了 $halftimescore 之外,我的所有变量都正确插入 - 这是为什么呢?
这是我的代码:
<?php
define('INCLUDE_CHECK',true);
require 'db.class.php';
$dom = new DOMDocument();
$dom ->load('main.xml');
$xpath = new DOMXPath($dom);
$queryResult = $xpath->query('//live/Match/Results/Result[@name="HT"]');
foreach($queryResult as $resulty) {
$halftimescore=$resulty->getAttribute("value");
}
$Match = $dom->getElementsByTagName("Match");
foreach ($Match as $match) {
$matchid = $match->getAttribute("id");
$home = $match->getElementsByTagName("Home");
$hometeam = $home->item(0)->getAttribute("name");
$homeid = $home->item(0)->getAttribute("id");
$away = $match->getElementsByTagName("Away");
$awayid = $away->item(0)->getAttribute("id");
$awayteam = $away->item(0)->getAttribute("name");
$leaguename = $match->getElementsByTagName("league");
$league = $leaguename->item(0)->nodeValue;
$leagueid = $leaguename->item(0)->getAttribute("id");
foreach ($match->getElementsByTagName('Result') as $result) {
$resulttype = $result->getAttribute("name");
$score = $result->getAttribute("value");
$scoreid = $result->getAttribute("value");
}
mysql_query("
INSERT INTO blabla
(home_team, match_id, ht_score, away_team)
VALUES
('".$hometeam."', '".$matchid."', '".$halftimescore."', '".$awayteam."')
");
}
【问题讨论】:
-
...和
var_dump($halftimescore);显示...?另请显示输入的 XML 文档。 -
字符串(3)“1-3”字符串(3)“1-3”字符串(3)“1-3”
-
您将显示该结果的
var_dump()行放在哪里? (在代码的哪一行) -
第 42 行。您希望我在帖子中包含我的 xml 吗?
-
不,如果你有一个值,那么它应该可以工作 - 插入的是什么而不是值(你在操作后在数据库中看到了什么)?另外,您是否收到任何错误消息?
标签: php mysql xml-parsing feed