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

search for in the

domxml_version> <domxml_open_file
Last updated: Fri, 14 Aug 2009

view this page in

domxml_open_mem

(PHP 4 >= 4.2.0)

domxml_open_memCrée un objet DOM pour un document XML

Description

DomDocument domxml_open_mem ( string $str [, int $mode [, array &$error ]] )

Cette fonction analyse le document XML dans le fichier donné.

Liste de paramètres

str

Le contenu du fichier XML.

mode

Le paramètre optionnel peut être utilisé pour changer le comportement de cette fonction.

Vous pouvez utiliser une de ces constantes suivantes : DOMXML_LOAD_PARSING (défaut), DOMXML_LOAD_VALIDATING ou DOMXML_LOAD_RECOVERING. Vous pouvez également ajouter à ces valeurs DOMXML_LOAD_DONT_KEEP_BLANKS, DOMXML_LOAD_SUBSTITUTE_ENTITIES et DOMXML_LOAD_COMPLETE_ATTRS avec une manipulation sur les bits avec un OU.

error

Si utilisé, il contiendra les messages d'erreur. error doit être passé par référence.

Valeurs de retour

Retourne une instance DomDocument du contenu XML donné.

Historique

Version Description
4.3.0 Les paramètres mode et error ont été ajoutés.

Exemples

Exemple #1 Ouvrir un document XML à partir d'une chaîne

<?php
include("exemple.inc");

if(!
$dom domxml_open_mem($xmlstr)) {
  echo 
"Erreur lors de l'analyse du document\n";
  exit;
}

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

Voir aussi



domxml_version> <domxml_open_file
Last updated: Fri, 14 Aug 2009
 
add a note add a note User Contributed Notes
domxml_open_mem
ardent at ahmeni dot org
07-Feb-2006 09:31
Although undocumented, domxml_open_mem does not exist in PHP5.  See this site for a quick fix:
http://alexandre.alapetite.net/doc-alex/domxml-php4-php5/index.en.html
msh at onliners dot dk
28-Feb-2005 06:51
It's always nice to see just where in the XML the error is so I've enhanced the error message example above to this:

// Read and parse dynamic PHP/XML document.
ob_start();
require($_REQUEST['page'].'.php');
$xml = ob_get_contents();
ob_end_clean();
$xmldoc = domxml_open_mem("<?xml version='1.0'  encoding='ISO-8859-1'?>".$xml, DOMXML_LOAD_PARSING, $error);
// Show error.
if(!$xmldoc) {
    // Assign each line to array.
    $arXML = explode("\n", $xml);

    echo "<pre>";
           
    foreach ($error as $errorline) {
        echo $errorline['errormessage'].
        "Node: ".$errorline['nodename']."\n".
        "Line: ".$errorline['line']."\n".
        "Column: ".$errorline['col']."\n";

        // Locate actual line number.
        $arErrorMessage = explode(' line ', $errorline['errormessage']);
        $arErrorMessage = explode(' ', $arErrorMessage[1]);
        $errorLineNumber = $arErrorMessage[0];
       
        // Show +/- 10 lines around error.
        $minErrorLineNumber = max(0, $errorLineNumber-10);
        $maxErrorLineNumber = min(sizeof($arXML), $errorLineNumber+10);
        echo "XML snip (linies: ".$minErrorLineNumber." to ".$maxErrorLineNumber."):\n";
        for($n = $minErrorLineNumber; $n < $maxErrorLineNumber; $n++)
            if($n == $errorLineNumber)
                echo "<B>".htmlentities($arXML[$n])."</B>\n";
            else
                echo htmlentities($arXML[$n])."\n";
        echo "\n\n";
    }

    echo "</pre>";
}

This will actually render +/- 10 lines around the actual error. Hope others will find this usefull aswell.
Lian Yuen Chi <yuenqi at hotmail dot com>
08-Oct-2004 04:40
Regarding the 'magic_quotes_runtime = On' issue in XML parsing, I will suggest using the method below, for those who do not want to turn off the setting in the configuration file.

<?php

$original
-magic-quotes-runtime-value = get_magic_quotes_runtime();

set_magic_quotes_runtime(0);

your-xml-parsing-codes();

set_magic_quotes_runtime($original-magic-quotes-runtime-value);

?>
heiko[dot]weber[at]softwareag[dot]com
19-Aug-2004 11:28
Careful, this function will not work with XML documents which are in UTF-16 or UTF-32 encoding and include a BOM at the beginning, so your code will need a special handling for these encodings.
Carl Wurtz
13-Jul-2004 10:01
A recursive function that parses XML into objects:

<?php

    $xmlstg
= '<?xml version="1.0"?';
   
$xmlstg .= '><candy><anatomically_shaped><head_to_bite_off>chocolate bunnies</head_to_bite_off><headless>gummi worms</headless></anatomically_shaped></candy>';
       
   
   
$dom = domxml_open_mem($xmlstg);
   
$root = $dom->document_element();
   
$root->name = '$candy';
    function
parse_node($node) {
        global
$candy;
        if (
$node->has_child_nodes()) {
            foreach(
$node->child_nodes() as $n) {
                if (
$n->node_name() == '#text') {
                    eval(
"$node->name=\"" . $n->node_value() . "\";");
                }
                else {
                   
$n->name = $node->name . '->' . $n->node_name();
                   
parse_node($n);
                }
            }
        }
    }

   
parse_node($root);
    echo
$candy->anatomically_shaped->head_to_bite_off;
// echos "chocolate bunnies"

?>
voituk [on] asg [dot] kiev [dot] ua
07-Jul-2004 02:48
There is some interesting feature with "magic_quotes_runtime" ini directive and "domxml_open_mem" function.
If we have "magic_quotes_runtime=on" in out php.ini file, this code generates many warning

$xml_str = file_get_contents($xml_file));
$Document = domxml_open_mem($xml_str));

if "magic_quotes_runtime=off" all is good.
ej at campbell *dot* name
01-Dec-2003 04:54
The DOM XML parser does not automatically free memory when a DomDocument goes out of scope.

If you're using the DOM parser to parse several XML files as part of a long running script, you must free the DomDocument manually or a memory leak will occur.

To free a DOM document allocated by domxml_open_mem, do the following:

$doc = domxml_open_mem("$contents");

$doc->free();
chris at schaake dot nu
01-Nov-2003 07:20
Finally figured out the error handling. The sample below will give a nice error message. Should also work for domxml_open_file.

<?
# Small sample xml file with 2 errors
#     1. root tag not closed
#    2. Missing ; in &amp;
$xml = "<root>&amp";

$xmldoc = @domxml_open_mem($xml,DOMXML_LOAD_PARSING,$error);

echo
"<pre>";    # Just for nice layout
foreach ($error as $errorline) {    # Loop through all errors
   
echo $errorline['errormessage'];
    echo
" Node   : " . $errorline['nodename'] . "\n";
    echo
" Line   : " . $errorline['line'] . "\n";
    echo
" Column : " . $errorline['col'] . "\n\n";
}

?>
jon at hiveminds dot net
16-Oct-2003 07:10
You can also create a new DomDocument instance by calling its constructor:

<?php
  $doc
= new DomDocument($xml);
?>

where $xml is any string of well-formed XML.

domxml_version> <domxml_open_file
Last updated: Fri, 14 Aug 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites