Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :
03/24/2008
Times viewed :
277
PHP SimpleXML: Function Open_File
PHP Version: PHP 4 >= 4.2.1
Description
PHPs DOM functions include domxml_new_doc() to create new XML
documents, dmxml_open_file() to open an XML document
file as a DOM object, and domxml_open_mem() to create
a DOM object from an XML document already in memory. These functions return a
DOM object, not a String to other common data type. When you use the PHP DOM
extension functions, you typically first create a DOM document object and then
manipulate it using functions that are part of the class of that object. There
are a number of object classes available, and by starting with a DOMDocument object, you can then examine it and retrieve
new objects reflecting XML document components such as element, attributes and
so on. domxml_open_file() function creates a DOM object from XML file.
Usage:
objectdomxml_open_file ( string
filename [, int mode [, array &error]])
Optional parameter mode
can be used to change the behavior of this function. It was added in PHP 4.3.0.
You can use one of the following constants for it: DOMXML_LOAD_PARSING
(default), DOMXML_LOAD_VALIDATING or DOMXML_LOAD_RECOVERING. You can add to it
also DOMXML_LOAD_DONT_KEEP_BLANKS, DOMXML_LOAD_SUBSTITUTE_ENTITIES and
DOMXML_LOAD_COMPLETE_ATTRS byusing bitwise
or option.
Example:
<?php
echo "<p>Parsed Employee Data</p>";
$xmlfileName = $_POST['inputfile'];
$default_dir = "xml_files/";
$default_dir .= $xmlfileName;
$myDOM = domxml_open_file($default_dir);
$root
= $myDOM->document_element();
//print
$s
= simplexml_import_dom($dom);
echo "Element Name";
echo $s->employeename[0];
?>
How
it Works:
I have created a DOM object from a
xml file. Ensure that the file is well formed.
$myDOM = domxml_open_file($default_dir);
In the above code I have created a DOM object.
$root = $myDOM->document_element();
After I created a new DOM object, I extracted the
root element from the DOM object.