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



PHP : Function Reference : DOM XML Functions : domxml_open_mem

domxml_open_mem

Creates a DOM object of an XML document (PHP 4 >= 4.2.0)
DomDocument domxml_open_mem ( string str [, int mode [, array &error]] )

Example 577. Opening an XML document in a string

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

if (!
$dom = domxml_open_mem($xmlstr)) {
 echo
"Error while parsing the document\n";
 exit;
}

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

Code Examples / Notes » domxml_open_mem

jon

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.


voituk on asg dot kiev dot ua

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

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();


lian yuen chi
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);
?>


msh

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.


chris

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";
}
?>


heiko dot weber

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.

ardent

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


carl wurtz

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"
?>


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