Welcome Guest, Not a member yet? Register   Sign In
simplexml parsing fails if only one item
#1

[eluser]bobbob[/eluser]
I have an odd problem with a script that has been running for months successfully.
The feed format has not been changed.
I may not have noticed that with one item the script fails to read the xml.
My function has this:

Code:
<?php
$this->load->library('parser');

              

              
        $filename = 'path/to/file.dat';
                $xmlfile="./xml/".$filename;
        $xmlRaw = file_get_contents($filename);
        $this->load->library('simplexml');
        $xmlData = $this->simplexml->xml_parse($xmlRaw);



foreach($xmlData['ARTICLE'] as $row)
                {
            $id = $this->input->xss_clean($row['@attributes']["ID"]);
            
            $posting_date = $this->input->xss_clean($row['@attributes']["POSTING_DATE"]);
            
            $posting_time = $this->input->xss_clean($row['@attributes']["POSTING_TIME"]);
            
            $archive_date = $this->input->xss_clean($row['@attributes']["ARCHIVE_DATE"]);
            
            $news_type = $this->input->xss_clean($row["NEWS_TYPE"]);


//etc

}

?>

Then each item is added to the database.

Any idea why with one ARTICLE it fails but with more than one it runs fine?

XML:

Code:
<?xml version="1.0"?>
<!DOCTYPE NEWSFEED SYSTEM "####passwordetc">
<NEWSFEED>
<ARTICLE ID="4545" POSTING_DATE="29-Aug-2009" POSTING_TIME="09:00" ARCHIVE_DATE="19-Aug-2010">
<NEWS_TYPE>News</NEWS_TYPE>

.....etc

</ARTICLE>
</NEWSFEED>
One article. More are:
Code:
<ARTICLE ID="4546" POSTING_DATE="29-Aug-2009" POSTING_TIME="09:00" ARCHIVE_DATE="19-Aug-2010">
....etc
</ARTICLE>
<ARTICLE ID="4547" POSTING_DATE="29-Aug-2009" POSTING_TIME="09:00" ARCHIVE_DATE="19-Aug-2010">
....etc
</ARTICLE>
etc
#2

[eluser]bobbob[/eluser]
Nothing??
Very rarely is there only one feed but if someone can see the possible error that would be great
#3

[eluser]pistolPete[/eluser]
[quote author="bobbob" date="1251665858"]... Any idea why with one ARTICLE it fails but with more than one it runs fine?
...[/quote]

What does "fail" mean exactly? Do you get an error message?
Is the XML valid? http://www.validome.org/xml/
#4

[eluser]bobbob[/eluser]
What happens is nothing gets added to the database. The script does add one line of 0 which is a strtotime of an index that apparently isn't set. It shows that one loop happened. The errors implies that some of the indexes in the xml aren't set but they are there. Anyway this doesn't normally matter as some indexes aren't set and my script doesn't mind. It takes what is there and adds it. For some reason it can't see any of it in this case.
Example:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined index: FEATURE_BLURB
Any other ideas?
#5

[eluser]pistolPete[/eluser]
Do you have to use this library? Do you use PHP4 or PHP5?
If you have access to PHP5 you could use the SimpleXML class:

Code:
$this->load->helper('security');

$xmlRaw = file_get_contents($filename);

$xml = new SimpleXMLElement($xmlRaw);
foreach ($xml->ARTICLE as $article)
{
        $id = xss_clean($article['ID']);
        $posting_date = xss_clean($article['POSTING_DATE']);
        ...                
}

I just tried it with one and several ARTICLE entries and it worked.




Theme © iAndrew 2016 - Forum software by © MyBB