not able to get any results or printout while fetching attribute from xml node using dom object in php
here's an example of the ticketcity.xml file im using:
<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>
and the php script to fetch the data:
$ticketcity = new DOMDocum开发者_开发知识库ent();
$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 have the $i there just for debugging, so that I'd have some printout at all... The problem is that I don't have anything. I get absolutely no printout from anything except "xml loaded, beginning update
"The script couldn't be any simpler, and it works fine with another XML file, the only difference between this and the other xml file is that the other file's data is stored in node values, and not attributes... I'm going crazy over this, what am I missing?
You should try to do this:
$ticketcity = new DOMDocument();
if (!$ticketcity->load("ticketcity.xml"))
echo "there was some kind of issue fetching the document";
else {
// your code...
}
$ticketcity
will always contain an object of class DOMDocument
but load
returns false
on failure:
Returns TRUE on success or FALSE on failure. If called statically, returns a DOMDocument and issues E_STRICT warning.
I just ran this on my local machine, and it works fine:
<?
$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++;
}
}
I suspect the file is missing, because DOMDocument will still hit the else clause even if it couldn't load the document. $ticketcity would not be empty!
精彩评论