473,387 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Valid XML for SimpleXML

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?

Jul 18 '07 #1
3 5314
bleen 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.
SimpleXMLElement throws an exception when the XML is invalid:

try {
$xml = new SimpleXMLElement(.....);
} catch (Exception $e) {
// Unparseable XML
}
JW
Jul 18 '07 #2
bleen wrote:
How can I check that the XML has no problems before creating my
SimpleXMLelement object.
Try tidy. Either as a standalone program, or as a PHP module.

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

"You know "that look" women get when they want sex? Me neither."
--Steve Martin
Jul 18 '07 #3
On Jul 18, 3:53 pm, bleen <blee...@gmail.comwrote:
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
}

Jul 19 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: archi | last post by:
dear all, what would be the most simple way to multisort the (HTML) output from a simplexml file. imagine i have loaded a simplexml file with names & addresses & much more, and i would like...
1
by: hwcowan | last post by:
Hello, I have started using SimpleXML and can do most things, but there are a couple of things that I can't seem to figure out. Currently, I can: 1. Open & load an XML file 2. Manually...
3
by: theboss3 | last post by:
Take, for example a typical RSS 2.0 feed (like http://lorelle.wordpress.com/feed/) and tell me how I access the tags like 'content:encoded' and 'wfw:commentRSS' using SimpleXML. I've searched far...
1
by: root | last post by:
Hi folks - hope someone can help me. Firstly, my apologies for crossposting this to alt.php (and not properly crossposting either). I have an XML file with some elements like those below ...
2
by: mandric | last post by:
Hello, Can someone please enlighten me on how to preserve the <!]> element when parsing an xml file or string with simplexml. I'm using libxml 2.6.16 and php 5.1.4. I tried a few variations,...
1
by: Andy | last post by:
Hi, I'm running in to a little issue with SimpleXML, and wondered if anyone knew if it was normal implementation for SimpleXML, or I'm not using a correct flag, or even it's a an issue that...
5
by: Pablo | last post by:
Hello, Does anybody know any tutorial about using PHP5 SimpleXML with forms? I have found some SimpleXML examples, but only modifying node attributes from the php code directly. I would like...
0
by: ty | last post by:
I have a script that takes a xml template then adds data into it using SimpleXMLElement. I then save it using asXML into a mysql database. On my development machine at home it works fine. ...
7
by: dimo414 | last post by:
So I'm trying to use SimpleXML to get some attribute information about some nodes in my XML document, but it seems like SimpleXML ignores attributes for elements with no children, For instance:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.