PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

domxml_open_mem> <domxml_new_doc
Last updated: Fri, 29 Aug 2008

view this page in

domxml_open_file

(PHP 4 >= 4.2.0)

domxml_open_fileXML ファイルから DOM オブジェクトを作成する

説明

DomDocument domxml_open_file ( string $filename [, int $mode [, array &$error ]] )

この関数は、ファイルで与えられた XML 文章をパースします。

パラメータ

filename

XML ファイルへのパス。ファイルは読み込み専用モードでアクセスされます。

mode

このオプションパラメータにより、この関数の動作を変更する事が可能です。

以下の定数の 1 つを使用することが可能です: DOMXML_LOAD_PARSING (デフォルト), DOMXML_LOAD_VALIDATING もしくは DOMXML_LOAD_RECOVERINGビット OR により DOMXML_LOAD_DONT_KEEP_BLANKS, DOMXML_LOAD_SUBSTITUTE_ENTITIESDOMXML_LOAD_COMPLETE_ATTRS も追加することが可能です。

error

指定された場合、エラーメッセージが代入されます。 errorリファレンス として渡す必要があります。

返り値

与えられたファイルの DomDocument インスタンスを返します。

例1 ファイルから XML 文章をオープンする

<?php

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

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

変更履歴

バージョン 説明
4.3.0 パラメータ modeerror が追加されました。



domxml_open_mem> <domxml_new_doc
Last updated: Fri, 29 Aug 2008
 
add a note add a note User Contributed Notes
domxml_open_file
A-Wing (info at a-wing dot co dot uk)
16-May-2006 11:23
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);
}

?>
connor
01-Feb-2006 05:52
But. if thr path is some hipelink like www.somth.com/data.xml
noyanoya at tin dot it
28-Dec-2005 09:47
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!
php at gungfu [dot] de
19-Mar-2005 08:13
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.
contact at richardleggett dot co dot uk
14-Feb-2005 02:00
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.
info at sgonda dot de
30-Oct-2003 01:02
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...
kevin at ieqdev dot com
15-Jun-2003 04:30
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
et at wkv dot at
24-Feb-2003 08:49
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) :-)
andrew at boxuk dot com
07-Feb-2003 05:32
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 at redshift dot co dot no dot spam dot za
29-Apr-2002 06:12
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

domxml_open_mem> <domxml_new_doc
Last updated: Fri, 29 Aug 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites