XMLReader mysteriously fails when parsing document
Posted
by Valrus
on Stack Overflow
See other posts from Stack Overflow
or by Valrus
Published on 2010-04-15T19:07:55Z
Indexed on
2010/04/15
19:13 UTC
Read the original article
Hit count: 335
I have a php script that takes in some XML data and parses it, displaying various bits of information I pull out of it. This has been working fine for over 6 months, until recently it now fails mysteriously on a certain tag.
The XML is generated from an outside source(a conferencing bridge), so I have no control over how it is built. I have put the xml through an online validator and it returned no errors. The code also works fine when I connect to another conferencing bridge and get the same output.
I have been using XMLReader class so far and tried switching to SmipleXML, but it fails when I create the SimpleXML object using the data.
Here is the snippet of where it fails while parsing:
<CDR_SUMMARY>
<FILE_VERSION>1001</FILE_VERSION>
<NAME>Conference Title Hidden</NAME>
<ID>10227</ID>
<STATUS_STR>Auto_termination,_everybody_quit</STATUS_STR>
<STATUS>4</STATUS>
<GMT_OFFSET>-4</GMT_OFFSET>
<START_TIME>2010-04-14T15:00:33</START_TIME>
<DURATION>
<HOUR>0</HOUR>
<MINUTE>39</MINUTE>
<SECOND>37</SECOND>
</DURATION>
<RESERVE_START_TIME>2010-04-14T15:00:33</RESERVE_START_TIME>
<RESERVE_DURATION>
<HOUR>12</HOUR>
<MINUTE>0</MINUTE>
<SECOND>0</SECOND>
</RESERVE_DURATION>
<MCU_FILE_NAME>c11</MCU_FILE_NAME>
<FILE_SAVED>0</FILE_SAVED>
<GMT_OFFSET_MINUTE>0</GMT_OFFSET_MINUTE>
</CDR_SUMMARY>
The <SOUND>
opening tag is the last one read before failing.
And this is my code: `public function processResponse($xml) { print "Processing List...";
$reader = new XMLReader($xml);
try {
$reader->setSchema("./schemas/response_trans_cdr_list.xsd");
} catch(Exception $e) {
print "Exception:".$e->getMessage();
}
//verify xml
if($reader->xml($xml)) {
while($reader->read()) {
//$currentstring = $reader->readstring();
if($reader->name === "ACTION") {
//into meat of resposnse
while($reader->read()) {
//print "Looping...";
//$currentstring = $reader->readInnerXML();
//print $currentstring;
if($reader->nodeType == XMLReader::ELEMENT) {
if($reader->name === "ID") {
$this->conferenceIDs[$this->counter] = $reader->readString();
print "<BR>Conf ID found: ".$this->counter." ID: ".$this->conferenceIDs[$this->counter];
$this->counter += 1;
}
else if($reader->name === "START_TIME") {
//conference start time.
print "<BR>Start Time Found! ";
$this->conferenceStarts[$this->counter-1] = $reader->readString();
}
else if($reader->name === "NAME") {
print "<BR> Name Found!";
$this->conferenceNames[$this->counter] = $reader->readString();
}
else if($reader->name === "MCU_FILE_NAME") {
print "<BR> MCU_FILE_NAME Found!";
//print $this->counter." File Name: ".$reader->readstring()."<BR>";
} else if($reader->name === "RESERVE_START_TIME") {
print "<BR> RESERVE_START_TIME Found!";
} else if($reader->name === "FILE_VERSION") {
print "<BR> FILE_VERSION Found!";
} else if($reader->name === "STATUS_STR") {
print "<BR> STATUS_STR Found!";
} else if($reader->name === "STATUS") {
print "<BR> STATUS Found!";
} else if($reader->name === "GMT_OFFSET") {
print "<BR> GMT_OFFSET Found!";
} else if($reader->name === "DURATION") {
print "<BR> DURATION Found!";
} else if($reader->name === "RESERVE_START_TIME") {
print "<BR> RESERVE_START_TIME Found!";
} else if($reader->name === "RESERVE_DURATION") {
print "<BR> RESERVE_DURATION Found!";
} else if($reader->name === "FILE_SAVED") {
print "<BR> FILE_SAVED Found!";
} else if($reader->name === "GMT_OFFSET_MINUTE") {
print "<BR> GMT_OFFSET_MINUTE Found!";
} else if($reader->name === "HOUR") {
print "<BR> HOUR Found!";
} else if($reader->name === "MINUTE") {
print "<BR> MINUTE Found!";
} else if($reader->name === "SECOND") {
print "<BR> SECOND Found!";
}
}else if($reader->nodeType == XMLReader::END_ELEMENT) {
print "Close Element".$reader->name;
}
}
print "End Action Loop";
}
}
}
else {
print "Error reading response: Bad XML returned";
}
}`
Anyone have any ideas what could be causing this failure? the code exits gracefully and I see the "End Action Loop" message in my output. It's like it just spontaneously exits the loop.
© Stack Overflow or respective owner