这是使用以下内容的ticketcity.xml文件的示例:
<Events Version="3.0" Method="GetEvents" StatusCode="0" StatusMsg="Success">
−
<Event ID="569402" Name="Hair" SeatingChart="http://www.ticketcity.com/images/seatingcharts/MARTINBECK_THEATRE_NYC.GIF" Page="http://www.ticketcity.com/theatre-tickets/broadway-tickets/hair-tickets/hair-tickets-al-hirschfeld-theatre-february-3-200pm.html" EventDateTime="02/03/2010 2:00PM">
<Performer ID="463" Name="Hair" Primary="true"/>
−
<Venue ID="961" Name="Al Hirschfeld Theatre">
<City ID="36469" Name="New York"/>
<State ID="34" Abbr="NY" Name="New York"/>
<Country ID="1" Abbr="US" Name="United States"/>
</Venue>
</Event>
−
</Events>
和获取数据的php脚本:
$ticketcity = new DOMDocument();
$ticketcity->load("ticketcity.xml");
if (empty($ticketcity))
echo "there was some kind of issue fetching the document";
else {
echo "xml loaded, beginning update<br>\n";
$events = $ticketcity->getElementsByTagName("Event");
$i=0;
foreach ($events as $event){
echo $i."<br>\n";
$eventid = $event->getAttribute('ID');
$eventname = $event->getAttribute('Name');
$eventmap = $event->getAttribute('SeatingChart');
$eventpage = $event->getAttribute('Page');
echo "$eventid, $eventname, $eventmap, $eventpage<br>\n";
$i++;
}
我有$ i只是为了调试,所以我有一些打印输出...问题是我什么都没有。除了“xml loaded,begin update
”
脚本不能更简单,它可以与另一个XML文件一起使用,这个和另一个xml文件的唯一区别是另一个文件的数据存储在节点值中,而不是属性......我我为此疯狂,我错过了什么?
答案 0 :(得分:0)
我刚在本地机器上运行它,它运行正常:
<?
$xml = '<Events Version="3.0" Method="GetEvents" StatusCode="0" StatusMsg="Success">
−
<Event ID="569402" Name="Hair" SeatingChart="http://www.ticketcity.com/images/seatingcharts/MARTINBECK_THEATRE_NYC.GIF" Page="http://www.ticketcity.com/theatre-tickets/broadway-tickets/hair-tickets/hair-tickets-al-hirschfeld-theatre-february-3-200pm.html" EventDateTime="02/03/2010 2:00PM">
<Performer ID="463" Name="Hair" Primary="true"/>
−
<Venue ID="961" Name="Al Hirschfeld Theatre">
<City ID="36469" Name="New York"/>
<State ID="34" Abbr="NY" Name="New York"/>
<Country ID="1" Abbr="US" Name="United States"/>
</Venue>
</Event>
−
</Events>
';
$ticketcity = new DOMDocument();
$ticketcity->loadXML($xml);
if (empty($ticketcity))
echo "there was some kind of issue fetching the document";
else {
echo "xml loaded, beginning update<br>\n";
$events = $ticketcity->getElementsByTagName("Event");
$i=0;
foreach ($events as $event){
echo $i."<br>\n";
$eventid = $event->getAttribute('ID');
$eventname = $event->getAttribute('Name');
$eventmap = $event->getAttribute('SeatingChart');
$eventpage = $event->getAttribute('Page');
echo "$eventid, $eventname, $eventmap, $eventpage<br>\n";
$i++;
}
}
我怀疑该文件丢失了,因为即使DOMDocument无法加载文档,它仍然会触及else子句。 $ ticketcity不会是空的!
答案 1 :(得分:0)
你应该尝试这样做:
$ticketcity = new DOMDocument();
if (!$ticketcity->load("ticketcity.xml"))
echo "there was some kind of issue fetching the document";
else {
// your code...
}
$ticketcity
将始终包含类DOMDocument
的对象,但load
在失败时返回false
:
成功时返回TRUE,失败时返回FALSE。如果静态调用,则返回DOMDocument并发出E_STRICT警告。