Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : DOM XML Functions : domxml_open_file

domxml_open_file

Creates a DOM object from an XML file (PHP 4 >= 4.2.0)
DomDocument domxml_open_file ( string filename [, int mode [, array &error]] )

Example 576. Opening an XML document from a file

<?php

if (!$dom = domxml_open_file("example.xml")) {
 echo
"Error while parsing the document\n";
 exit;
}

$root = $dom->document_element();
?>

Code Examples / Notes » domxml_open_file

info

You can load your own DTD's within your XML Doc like this:
<?php
$domxml = domxml_open_file('test.xml',DOMXML_LOAD_VALIDATING,$error);
?>
I hope this helps....
The DocumentType Definition (must/have) to be in the Doc root of your Server...


andrew

Using PHP 4.2.3 and Win2K.
The XML file needs to be referenced using the full filesystem path name, even if its in the same directory.


rudolfnospam

Using PHP 4.1.2, Win2K, IIS.
I found that if the path to the XML source file is too long then the file isn't picked up. I haven't tested it to see how long the path can be or whether this is still an issue in PHP 4.2


kevin

If you're working on a windows machine and don't want to use full paths , just use...
$showfile = file_get_contents($path . "/" . $fileName);
if(!$domDoc = domxml_open_mem($showfile)) {
echo "Couldn't load xml...";
exit;
}
Because file_get_contents() can use relative paths on Win, it keeps your code more portable...
twist


contact

If you want to work on both Windows and Linux, I found appending the following to the front of your file path works:
$xmlPath = dirname(__FILE__) . "/";
$xmlDOM = domxml_open_file($xmlPath . "file.xml");
(rather than the "\\" in a previous post on this page which only works on Windows).
This should get around the I/O errors.


php

For me, on Windows XP, the solution with  file_get_contents works, the one with domxml_open_file does not. It seems the latter caches the loaded file. Quite confusing.

et

domxml documentation is a moving target, for 4.2.3 a working example is:
<?
$xmlpath = dirname(__FILE__) . "\\";
$xmldoc = domxml_open_file( $xmlpath . "test.xml");
$xsldoc = domxml_xslt_stylesheet_file ( $xmlpath . "test.xsl");
$result = $xsldoc->process($xmldoc);
print $result->dump_mem();
?>
RTFS is a working method in this case (lucky guesses work also from time tor time) :-)


connor

But. if thr path is some hipelink like www.somth.com/data.xml

noyanoya

another way to resolve path's problem is to use  realpath() function
for example:
<?php
$file=$_REQUEST['file'];
$xmlPath = realpath("../xml/");
$fileXML ="$file";
 if(!$dom = domxml_open_file($xmlPath."/".$fileXML)) {
echo "error opening the file";
exit;
}
?>
N.B. you have to put the final slash ( / ) beetween path and file name!


a-wing info

A very useful undocumented feature if you are opening hundreds of xml docs (my script processes 20,000) is DomDocument->free.
This clears the allocated the memory used by the xml file which is not done automatically by just opening a new file on the same variable.
Example:
<?
$dir="/path/to/xml/files/";
if ($dh = opendir($dir)) {
   while (($file = readdir($dh)) !== false) {
       if(is_file($dir.$file))
       {
           echo $file . "\\n";
           $dom=domxml_open_file($dir.$file);
           #...
           #You xml processor here
           #...
           $dom->free();
       }
   }
   closedir($dh);
}
?>


Change Language


Follow Navioo On Twitter
DomAttribute->name
DomAttribute->set_value
DomAttribute->specified
DomAttribute->value
DomDocument->add_root
DomDocument->create_attribute
DomDocument->create_cdata_section
DomDocument->create_comment
DomDocument->create_element_ns
DomDocument->create_element
DomDocument->create_entity_reference
DomDocument->create_processing_instruction
DomDocument->create_text_node
DomDocument->doctype
DomDocument->document_element
DomDocument->dump_file
DomDocument->dump_mem
DomDocument->get_element_by_id
DomDocument->get_elements_by_tagname
DomDocument->html_dump_mem
DomDocument->xinclude
DomDocumentType->entities()
DomDocumentType->internal_subset()
DomDocumentType->name()
DomDocumentType->notations()
DomDocumentType->public_id()
DomDocumentType->system_id()
DomElement->get_attribute_node()
DomElement->get_attribute()
DomElement->get_elements_by_tagname()
DomElement->has_attribute()
DomElement->remove_attribute()
DomElement->set_attribute_node()
DomElement->set_attribute()
DomElement->tagname()
DomNode->add_namespace
DomNode->append_child
DomNode->append_sibling
DomNode->attributes
DomNode->child_nodes
DomNode->clone_node
DomNode->dump_node
DomNode->first_child
DomNode->get_content
DomNode->has_attributes
DomNode->has_child_nodes
DomNode->insert_before
DomNode->is_blank_node
DomNode->last_child
DomNode->next_sibling
DomNode->node_name
DomNode->node_type
DomNode->node_value
DomNode->owner_document
DomNode->parent_node
DomNode->prefix
DomNode->previous_sibling
DomNode->remove_child
DomNode->replace_child
DomNode->replace_node
DomNode->set_content
DomNode->set_name
DomNode->set_namespace
DomNode->unlink_node
DomProcessingInstruction->data
DomProcessingInstruction->target
DomXsltStylesheet->process()
DomXsltStylesheet->result_dump_file()
DomXsltStylesheet->result_dump_mem()
domxml_new_doc
domxml_open_file
domxml_open_mem
domxml_version
domxml_xmltree
domxml_xslt_stylesheet_doc
domxml_xslt_stylesheet_file
domxml_xslt_stylesheet
domxml_xslt_version
xpath_eval_expression
xpath_eval
xpath_new_context
xpath_register_ns_auto
xpath_register_ns
xptr_eval
xptr_new_context
eXTReMe Tracker