iTechtalk

Quick Search

Go Advanced

Member Login

Not registered? | Forgot Password
 
Register
Welcome
 
iTechtalk > Tutorials > Programming » Loading an XML document with SimpleXML
Reply
Old 10-10-2008, 06:25 AM   #1 (permalink)
 
Ebuggs's Avatar
 
Junior Member
Join Date: Oct 2008
Posts: 12
Default Loading an XML document with SimpleXML

SimpleXML uses a built-in class called SimpleXMLElement. It converts an XML document into a simpleXMLElement object. This contains an array of all the element, text, and attribute nodes, each of which is, in turn, a SimpleXMLElement object. What makes it so easy to use is that the name of each node becomes a property of the object, giving you direct access to it.

First, you need to load the XML document that you want to process. There are several ways of doing this, depending on whether the XML document is stored as a physical file (either locally or on a remote server) or as a string (e.g., after using the Pos_RemoteConnector).

Loading XML from a file

The easiest way to load an XML document from a file is to use the simplexml_load_file() function. This requires a single argument: a string containing either the path to a local file or the URL of the file on a remote server. You use it like this:

$xml = simplexml_load_file($location);

Alternatively, you can use the SimpleXMLElement class constructor. When used with a file, it requires two extra arguments, in addition to the file’s path or URL, like this:

$xml = new SimpleXMLElement($location, null, true); .

The second argument sets Libxml parameters (see PHP: Predefined Constants - Manual),
which are rarely used. Even though you don’t normally want them, the argument needs to be set to null because the third argument is required when the XML document is stored as a physical file, rather than a string. The third argument is a Boolean value that needs to be set to true.

There’s no advantage to using the SimpleXMLElement constructor unless you need the advanced features offered by the Libxml parameters. Keep things simple by using simplexml_load_file().
Ebuggs is offline   Reply With Quote
 
Reply

Bookmarks

Tags
document, php, simplexml, xml

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with loading the User’s profile. prem Windows xp Support 1 02-23-2009 10:53 AM
Error while loading OS? crazy Vista Support 1 02-20-2009 08:12 AM
How to create MS-DOS boot loading CD? bubli Vista Support 1 02-18-2009 07:51 AM
Loading Variables in Flash genius4ever Graphics 0 11-11-2008 10:40 AM
Loading classes automatically in PHP crazy4ever Programming 0 11-04-2008 09:24 AM


 

Content Relevant URLs by vBSEO 3.3.0