Reply to Re: Valid XML for SimpleXML

Your name:

Reply:


Posted by ZeldorBlat on 07/19/07 13:32

On Jul 18, 3:53 pm, bleen <blee...@gmail.com> wrote:
> I'm trying to use SimpleXML but I've run into a conundrum. Every day
> an XML file is generated that this script grabs and manipulates. How
> can I check that the XML has no problems before creating my
> SimpleXMLelement object. Here's what I mean:
>
> // this is the code in question:
> $file_topstory = /some/xml/file.xml
> $top_story_xml = new SimpleXMLElement($file_topstory, NULL, TRUE);
>
> If the XML doc contains (for example) a URL with an '&' in it then the
> script fails. I'd like to do something like this:
>
> if(is_valid_xml($file_topstory)){
> $top_story_xml = new SimpleXMLElement($file_topstory, NULL, TRUE);
>
> }else{
> // ERROR
> }
>
> How can i accomplish this?

According to the manual at <http://www.php.net/manual/en/
function.simplexml-element-construct.php>

"Produces an E_WARNING error message for each error found in the XML
data and throws an exception if errors were detected."

So, suppress the error and catch the exception:

try {
$top_story_xml = @new SimpleXMLElement($file_topstory, NULL,
TRUE);
} catch (Exception $e) {
//some error occured
}

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация