PHP: DOM XML functions - Manual
PHP  
downloads | documentation | faq | getting help | | php.net sites | links 
search for in the  
previousrewinddirDomAttribute->namenext
Last updated: Tue, 16 Jul 2002
view this page in Printer friendly version | English | Brazilian Portuguese | Chinese | Czech | Dutch | Finnish | French | German | Hungarian | Italian | Japanese | Korean | Romanian | Russian | Spanish | Turkish

XXV. DOM XML functions

Wprowadzenie

Ostrze¿enie

Ten modu� jest w stadium EKSPERYMENTALNYM. Oznacza to, �e zachowanie tych funkcji, ich nazwy, w zasadzie wszystko udokumentowane tutaj mo�e zosta� zmienione w przysz�ych wersjach PHP bez wcze�niejszego uprzedzenia. U�ywaj tego modu�u na w�asne ryzyko.

The domxml extension has been overhauled in PHP 4.3.0 to provide a better compliance with the DOM standard. The extension still contains many old functions, but they should not be used anymore. Especially those non object oriented functions should be avoided.

The extension allows you to operate on an XML document with the DOM API. It also provides a function domxml_xmltree() to turn the complete XML document into a tree of PHP objects. Currently this tree should be considered read-only - you can modify it but this would not make any sense since DomDocument_dump_mem() cannot be applied to it. Therefore, if you want to read an XML file and write a modified version use the DomDocument_create_element(), DomDocument_create_text_node(), set_attribute(), etc. and finally DomDocument_dump_mem() functions.

Wymagania

This extension make use of the . Download and install this library. You will need at least libxml-2.4.14

Instalacja

This extension is only available if PHP was configured with --with-dom=[DIR].

Deprecated functions

There are quite a few functions that do not fit into the DOM standard and should not be used anymore as listed in the following table. The function DomNode_append_child() has changed its behaviour. It now actually adds a child and not a sibling. If this breaks your application use the non DOM function DomNode_append_sibling().

Tabela 1. Deprecated functions and their replacements

Old functionNew function
xmldocdomxml_open_mem()
xmldocfiledomxml_open_file()
domxml_new_xmldocdomxml_new_doc()
domxml_dump_memDomDocument_dump_mem()
domxml_dump_mem_fileDomDocument_dump_file()
DomDocument_dump_mem_fileDomDocument_dump_file()
DomDocument_add_rootDomDocument_create_element() followed by DomNode_append_child()
DomDocument_dtdDomDocument_doctype()
DomDocument_rootDomDocument_document_element()
DomDocument_childrenDomNode_child_nodes()
DomDocument_imported_nodeNo replacement.
DomNode_add_childCreate a new node with e.g. DomDocument_create_element() und add it with DomNode_append_child().
DomNode_childrenDomNode_child_nodes()
DomNode_parentDomNode_parent_node()
DomNode_new_childCreate a new node with e.g. DomDocument_create_element() und add it with DomNode_append_child().
DomNode_set_contentCreate a new node with e.g. DomDocument_create_text_node() und add it with DomNode_append_child().
DomNode_get_contentContent is just a text node and can be accessed with DomNode_child_nodes().
DomNode_set_contentContent is just a text node and can be added with DomNode_append_child().

Predefiniowane sta�e

Poni�sze sta�e s� zdefiniowane w tym rozszerzeniu i staj� si� dost�pne, gdy rozszerzenie jest dokompilowane do PHP, lub za�adowane dynamicznie przy starcie.

Tabela 2. XML constants

ConstantValueDescription
XML_ELEMENT_NODE (integer) 1Node is an element
XML_ATTRIBUTE_NODE (integer) 2Node is an attribute
XML_TEXT_NODE (integer) 3Node is a piece of text
XML_CDATA_SECTION_NODE (integer) 4 
XML_ENTITY_REF_NODE (integer) 5 
XML_ENTITY_NODE (integer) 6Node is an entity like  
XML_PI_NODE (integer) 7Node is a processing instruction
XML_COMMENT_NODE (integer) 8Node is a comment
XML_DOCUMENT_NODE (integer) 9Node is a document
XML_DOCUMENT_TYPE_NODE (integer) 10 
XML_DOCUMENT_FRAG_NODE (integer) 11 
XML_NOTATION_NODE (integer) 12 
XML_GLOBAL_NAMESPACE (integer) 1 
XML_LOCAL_NAMESPACE (integer) 2 
XML_HTML_DOCUMENT_NODE (integer)   
XML_DTD_NODE (integer)   
XML_ELEMENT_DECL_NODE (integer)   
XML_ATTRIBUTE_DECL_NODE (integer)   
XML_ENTITY_DECL_NODE (integer)   
XML_NAMESPACE_DECL_NODE (integer)   
XML_ATTRIBUTE_CDATA (integer)   
XML_ATTRIBUTE_ID (integer)   
XML_ATTRIBUTE_IDREF (integer)   
XML_ATTRIBUTE_IDREFS (integer)   
XML_ATTRIBUTE_ENTITY (integer)   
XML_ATTRIBUTE_NMTOKEN (integer)   
XML_ATTRIBUTE_NMTOKENS (integer)   
XML_ATTRIBUTE_ENUMERATION (integer)   
XML_ATTRIBUTE_NOTATION (integer)   
XPATH_UNDEFINED (integer)   
XPATH_NODESET (integer)   
XPATH_BOOLEAN (integer)   
XPATH_NUMBER (integer)   
XPATH_STRING (integer)   
XPATH_POINT (integer)   
XPATH_RANGE (integer)   
XPATH_LOCATIONSET (integer)   
XPATH_USERS (integer)   
XPATH_NUMBER (integer)   

Classes

The API of the module follows the DOM Level 2 standard as close as possible. Consequently the API is fully object oriented. It is a good idea to have the DOM standard available when using this module. Though the API is object oriented there are many functions which can be called in a non-object oriented way by passing the object to operate on as the first argument. These function are mainly to retain compatibilty to older versions of the extension but are not encouraged to use anymore in new developments.

This API differs from the official DOM API in two points. First, all class attributes are implemented as functions with the same name and secondly the function names follow the PHP naming convention. This means that a DOM function lastChild() will be written as last_child().

This module defines a number of classes, which are listed — including their method — in the following tables. Classes with an equivalent in the DOM Standard are named DOMxxx.

Tabela 3. List of classes

Class nameParent classes
DomAttributeDomNode
DomCDataDomNode
DomCommentDomCData : DomNode
DomDocumentDomNode
DomDocumentTypeDomNode
DomElementDomNode
DomEntityDomNode
DomEntityReferenceDomNode
DomProcessingInstructionDomNode
DomTextDomCData : DomNode
ParserCurrently still called DomParser
XPathContext 

Tabela 4. DomDocument class (DomDocument : DomNode)

Method nameFunction nameRemark
doctypeDomDocument_doctype() 
document_elemnentDomDocument_document_element() 
create_elementDomDocument_create_element() 
create_text_nodeDomDocument_create_text_node() 
create_commentDomDocument_create_comment() 
create_cdata_sectionDomDocument_create_cdata_section() 
create_processing_instructionDomDocument_create_processing_instruction() 
create_attributeDomDocument_create_attribute() 
create_entity_referenceDomDocument_create_entity_reference() 
get_elements_by_tagnameDomDocument_get_elements_by_tagname() 
get_element_by_idDomDocument_get_element_by_id() 
dump_memDomDocument_dump_mem()not DOM standard
dump_fileDomDocument_dump_file()not DOM standard
html_dump_memDomDocument_html_dump_mem()not DOM standard
xpath_initxpath_initnot DOM standard
xpath_new_contextxpath_new_contextnot DOM standard
xptr_new_contextxptr_new_contextnot DOM standard

Tabela 5. DomElement class (DomElement : DomNode)

Method nameFunction nameRemark
tagnameDomElement_tagname() 
get_attributeDomElement_get_attribute() 
set_attributeDomElement_set_attribute() 
remove_attributeDomElement_remove_attribute() 
get_attribute_nodeDomElement_get_attribute_node() 
get_elements_by_tagnameDomElement_get_elements_by_tagname() 
has_attributeDomElement_has_attribute() 

Tabela 6. DomNode class

Method nameRemark
DomNode_node_name() 
DomNode_node_value() 
DomNode_node_type() 
DomNode_last_child() 
DomNode_first_child() 
DomNode_child_nodes() 
DomNode_previous_sibling() 
DomNode_next_sibling() 
DomNode_parent_node() 
DomNode_owner_document() 
DomNode_insert_before() 
DomNode_append_child() 
DomNode_append_sibling()Not in DOM standard. This function emulates the former behaviour of DomNode_append_child().
DomNode_remove_child() 
DomNode_has_child_nodes() 
DomNode_has_attributes() 
DomNode_clone_node() 
DomNode_attributes() 
DomNode_unlink_node()Not in DOM standard
DomNode_replace_node()Not in DOM standard
DomNode_set_content()Not in DOM standard, deprecated
DomNode_get_content()Not in DOM standard, deprecated
DomNode_dump_node()Not in DOM standard
DomNode_is_blank_node()Not in DOM standard

Tabela 7. DomAttribute class (DomAttribute : DomNode)

Method name Remark
nameDomAttribute_name() 
valueDomAttribute_value() 
specifiedDomAttribute_specified() 

Tabela 8. DomProcessingInstruction class (DomProcessingInstruction : DomNode)

Method nameFunction nameRemark
targetDomProcessingInstruction_target() 
dataDomProcessingInstruction_data() 

Tabela 9. Parser class

Method nameFunction nameRemark
add_chunkParser_add_chunk() 
endParser_end() 

Tabela 10. XPathContext class

Method nameFunction nameRemark
evalXPathContext_eval() 
eval_expressionXPathContext_eval_expression() 
register_nsXPathContext_register_ns() 

Tabela 11. DomDocumentType class (DomDocumentType : DomNode)

Method nameFunction nameRemark
nameDomDocumentType_name() 
entitiesDomDocumentType_entities() 
notationsDomDocumentType_notations() 
public_idDomDocumentType_public_id() 
system_idDomDocumentType_system_id() 
internal_subsetDomDocumentType_internal_subset() 

The classes DomDtd is derived from DomNode. DomComment is derived from DomCData

Przyk�ady

Many examples in this reference require an XML string. Instead of repeating this string in any example it will be put into a file and be included by each example. This include file is shown in the following example section. You could also create an XML document read it with DomDocument_open_file().

Przyk�ad 1. Include file example.inc with XML string

<?php
$xmlstr = "<?xml version='1.0' standalone='yes'?>
<!DOCTYPE chapter SYSTEM '/share/sgml/Norman_Walsh/db3xml10/db3xml10.dtd'
[ <!ENTITY sp \"spanish\">
]>
<!-- lsfj  -->
<chapter language='en'><title language='en'>Title</title>
 <para language='ge'>
  &amp;sp;
  <!-- comment -->
  <informaltable ID='findme' language='&amp;sp;'>
   <tgroup cols='3'>
    <tbody>
     <row><entry>a1</entry><entry
morerows='1'>b1</entry><entry>c1</entry></row>
<row><entry>a2</entry><entry>c2</entry></row>
     <row><entry>a3</entry><entry>b3</entry><entry>c3</entry></row>
    </tbody>
   </tgroup>
  </informaltable>
 </para>
</chapter>";
?>

Spis tre�ci
DomAttribute->name --  Returns name of attribute
DomAttribute->specified --  Checks if attribute is specified
DomAttribute->value --  Returns value of attribute
DomDocument->add_root [deprecated] --  Adds a root node
DomDocument->create_attribute --  Create new attribute
DomDocument->create_cdata_section --  Create new cdata node
DomDocument->create_comment --  Create new comment node
DomDocument->create_element --  Create new element node
DomDocument->create_entity_reference -- 
DomDocument->create_processing_instruction --  Creates new PI node
DomDocument->create_text_node --  Create new text node
DomDocument->doctype --  Returns the document type
DomDocument->document_element --  Returns root element node
DomDocument->dump_file --  Dumps the internal XML tree back into a file
DomDocument->dump_mem --  Dumps the internal XML tree back into a string
DomDocument->get_element_by_id --  Searches for an element with a certain id
DomDocument->get_elements_by_tagname -- 
DomDocument->html_dump_mem --  Dumps the internal XML tree back into a string as HTML
DomDocumentType->entities --  Returns list of entities
DomDocumentType->internal_subset --  Returns internal subset
DomDocumentType->name --  Returns name of document type
DomDocumentType->notations --  Returns list of notations
DomDocumentType->public_id --  Returns public id of document type
DomDocumentType->system_id --  Returns system id of document type
DomElement->get_attribute_node --  Returns value of attribute
DomElement->get_attribute --  Returns value of attribute
DomElement->get_elements_by_tagname --  Adds new attribute
DomElement->has_attribute --  Adds new attribute
DomElement->remove_attribute --  Adds new attribute
DomElement->set_attribute --  Adds new attribute
DomElement->tagname --  Returns name of element
DomNode->append_child --  Adds new child at the end of the children
DomNode->append_sibling --  Adds new sibling to a node
DomNode->attributes --  Returns list of attributes
DomNode->child_nodes --  Returns children of node
DomNode->clone_node --  Clones a node
DomNode->dump_node --  Dumps a single node
DomNode->first_child --  Returns first child of node
DomNode->get_content --  Gets content of node
DomNode->has_attributess --  Checks if node has attributes
DomNode->has_child_nodes --  Checks if node has children
DomNode->insert_before --  Inserts new node as child
DomNode->is_blank_node --  Checks if node is blank
DomNode->last_child --  Returns last child of node
DomNode->next_sibling --  Returns the next sibling of node
DomNode->node_name --  Returns name of node
DomNode->node_type --  Returns type of node
DomNode->node_value --  Returns value of a node
DomNode->owner_document --  Returns the document this node belongs to
DomNode->parent_node --  Returns the parent of the node
DomNode->prefix --  Returns name space prefix of node
DomNode->previous_sibling --  Returns the previous sibling of node
DomNode->remove_child --  Removes child from list of children
DomNode->replace_child --  Replaces a child
DomNode->replace_node --  Replaces node
DomNode->set_content --  Sets content of node
DomNode->set_name --  Sets name of node
DomNode->unlink_node --  Deletes node
DomProcessingInstruction->data --  Returns data of pi node
DomProcessingInstruction->target --  Returns target of pi node
domxml_new_doc --  Creates new empty XML document
domxml_open_file -- Creates a DOM object from XML file
domxml_open_mem -- Creates a DOM object of an XML document
domxml_version --  Get XML library version
domxml_xmltree --  Creates a tree of PHP objects from an XML document
xpath_eval_expression --  Evaluates the XPath Location Path in the given string
xpath_eval --  Evaluates the XPath Location Path in the given string
xpath_new_context --  Creates new xpath context
xptr_eval --  Evaluate the XPtr Location Path in the given string
xptr_new_context --  Create new XPath Context
User Contributed Notes
DOM XML functions
add a note about notes

12-Aug-2000 05:46

I'm libxml maintainer, I suggest anybody using
the DOM functions to:
  - not stay with 2.0.x versions, upgrade please
     
  - have a look at xmlsoft.org if you want more
     information, but this might be misleading too
     since PHP-DOM doesn't expose all the
     libxml API
  - for the DOM reference 
  - if you have trouble when parsing an
    XML/HTML files, check first that they are
     correct (xmllint distributed with libxml may
     help), if no fix them, if yes send me a bug
     report with the input exhibiting the problem

 happy DOM hacking,

[email protected]

plalande (at) gameloft (dot) com
15-Aug-2000 05:10

It should also be noted that the presence of zlib compression library is
necessary in order to compile php with DOM XML support.


17-Aug-2000 10:39

Maybe it is of interest for someone:

This is an example for getting the attribute of a
node "article" using the xmldoc-function.

&lt;?php
$docnode = xmldoc("&lt;?xml version=\"1.0\"
encoding=\"UTF-8\"?>\n".
                  "<!DOCTYPE webdoctype SYSTEM
\"webdoc.dtd\">\n".
                  "<webdoc>\n".
                  "  <article id=\"651\"/>\n".
                  "</webdoc>\n");

$node = $docnode->children();
$node = $node[1]->children();
$attr = $node[1]->attributes();
printf("%s=%s", $attr[0]->name,
$attr[0]->children[0]->content);
?>


06-Nov-2000 03:03

As of libxml-2.2.7 and php-4.0.2, the DOM API is *not* implemented. You
cannot expect to read the W3C DOM reference manual and use the function
names, this will not work. The php-4.0.2 "DOM" implementation
offer important functionalities to work with XML documents but its name is
misleading.


29-Dec-2000 08:35

check out an article about using DOM in php at


01-Mar-2001 04:45

(Copying note posted to the wrong section):

[email protected]
           24-Feb-2001 01:43pm
           To install domxml under Windows, runnning Apache with php4.04.


           Add two lines in c:\windows\php.ini under "Windows
extensions" 

           extension_dir=c:\php\extensions           
 extension=c:\php_domxml.dll 

                     / Mads �sterby


01-Mar-2001 08:17

when configuring DOM-XML on FreeBSD, i ran into some trouble. Just make
sure that you have previously installed the following ports: libiconv and
libxml2.

Then edit the Makefile on freebsd's port of php4. adding the line --with-dom=/usr/local. Make sure you use FreeBSD's port, 'cause if you try to build php from the basic distribution you will be missing several FreeBSD patches for trouble-free building. =)

After that, create symbolic links like these:

libxml.a -> libxml2.a

libxml.so -> libxml2.so.5

on /usr/lib . it should compile and install correctly.


05-Apr-2001 10:06
In addition to the symlinks to the compiled libs, there's the following one
for the includes needed:
# ln -s /usr/local/include/libxml2/libxml /usr/lib/libxml


08-May-2001 04:39

xpath_new_context(), as well as xpath_eval() are members of xmldoc object
as of 4.0.6-dev. Aslo a lot of function names have changed. Better take a
look at the header files (in php4.0.6/ext/domxml/) instead of the summary
above. But xpath works much better than in 4.0.4!


10-May-2001 07:33

If you read your XML file with the fread() function, be carefull with the
magic_quotes (compiler-) option. Magic Quotes _must_ be turned off before
reading the data.
To be save use something like

set_magic_quotes_runtime(0); 
before fread() and
set_magic_quotes_runtime(get_magic_quotes_gpc()); 
after.


11-May-2001 11:59

An alternative to turning off magic quotes is the stripslashes() function:

$xmldata = stripslashes(fread ($fd, filesize ($filename)));

Then, you can domxml $xmldata all you want. =) (makes for cleaner code)



09-Jun-2001 12:54

I tried to use domxml with 4.0.5 on FreeBSD. 

[email protected] helps me a lot but he makes some mistake in second
post

# ln -s /usr/local/include/libxml2/libxml /usr/include/libxml

or even

# ln -s /usr/local/include/libxml2/libxml /usr/local/include/libxml

is the right answer.

I bult without problems now.

Thanks


21-Jun-2001 03:51

If I read from a domxml instance I created, the attribute 

  $mydomxml->type

may contain 14 numbers according to the table above. anybody knows what it
means that both XML_ELEMENT_NODE and XML_GLOBAL_NAMESPACE are represented
by 1 resp. XML_ATTRIBUTE_NODE and XML_LOCAL_NAMESPACE by 2? context?


05-Sep-2001 05:22

WINDOWS PHP_DOMXML PROBLEM

i have a solution for the php_domxml.dll problem in win. you have to
download the binary version of libxml for windows and copy them into your
windows system directory (e.g. c:\winnt\system32 or c:\windows\system). 

that's it - all will be load fine!


24-Sep-2001 09:10

Quite possibley the most useful class ever to grace php, phpDOM 
- 


07-Oct-2001 04:40

there is a function to delete a node, in the DOMXML API !!!! (since 4.04 i
believe) ...

domxml_unlink_node( node ); 

sure !


09-Oct-2001 06:50

I have encountered various problems getting Dom XML to run on both Linux
and NT [but never on 2000], sometimes dependant on the actual release of
PHP 4.
If you are just parsing some xml, save time by using PHP's standard XML
functions. Easy to use, well documented. Sorted timesaver :)


06-Nov-2001 02:35

php changed the name of the attributes from 'name' to 'tagname', the member
function 'getattr' no longer available but 'get_attribute' is still there.


15-Nov-2001 11:37

On 26-Sep-2001 "stevenf at shellnet dot co dot uk" posted a
message about compiling php on FreeBSD.

The same problem he's talking about goes for RedHat 7.1.
If you installed libxml2-2.4.10 you have to make a symbolic link from
/usr/include/libxml to /usr/include/libxml2/libxml.
Now the php configure script will find the DOM path.


24-Nov-2001 12:27

DOMXML on Windows 9x!

I update this site frequently with latest news regarding Apache/PHP:
www.mdz.dk 

DOMXML 
To make this work, I spend a year looking and trying to get the answer...
here it is (I hope)--

Copy the file \php\php4ts.dll to c:\windows\system\

Open and edit "apache.conf". PHP MUST be loaded as a module,
or it wont work with domxml.

Here is a part of my apache.conf:


#ScriptAlias /php/ "c:/webserver/php/"
#AddType application/x-httpd-php .php	
#Action application/x-httpd-php "/php/php.exe	
LoadModule php4_module c:/webserver/php/sapi/php4apache.dll
AddType application/x-httpd-php .php

Now you should tjeck if you can start apache, and see that no errors
occour. :)

Open "c:\windows\php.ini" in notepad.

Find and change these two lines to: (remember to change to rigth path!)

extension_dir= "c:\WebServer\php\extensions";
extension=php_domxml.dll

Now the Last part:

I got it to work with: a) libxml2-v.2.4.6 and b)libxml2-2.4.10

Download libxml2 from
www.fh-frankfurt.de/~igor/projects/libxml/index.html.

Unpack and copy "lib2xml.dll" to "c:\windows\system\"

NOTE:
Using libxml2-v.2.4.10, you must also download "iconv binaries,
version 1.7 for Win32" from the same site
and unpack "iconv.dll" to "c:\windows\system\"

Thats all.. I got it to work, so can you!

---- Test DOMXML

With phpInfo() you�ll see this somewhere on the page:

DOM/XML enabled 
libxml Version 2.3.7 
XPath Support enabled 
XPointer Support enabled 


This piece of code will write "What employees?" if domxml not is
installed correct.

Note that a) you must have a file named employees.xml, and the you MUST
write
down the whole path to the xml-file.

$doc = xmlDocfile("c:\webserver\xml\employees.xml") or
die("What employees?");

Here is the proof that domxml can be runned under Windows 98. www.mdz.dk


22-Dec-2001 02:46

It seems like DomNode class attribute 'name' has changed to 'tagname' for
nodes of type XML_TEXT_NODE.

The documentation does not mention anything about the changes to domxml
(seems to be more than this one).


07-Jan-2002 12:31

Work-Around for the unlink-problem ...

With the new version of php 4.1.x, the unlink-function does not work as
expected - in my case - the script hangs up and is interrupted after the
timeout ...

The following code is a simple work-around:

$useless = domxml_node( "newnode" );
$useless->append_child( $node_to_unlink );

The append_child-method simply appends the node to the $useless node and
removes it automatically from the original tree. Doing nothing with
$useless gives you the desired result.

Regards


22-Jan-2002 11:04

I have an application which works perfectly in 4.0.6, but not 4.1.1.  A few
notes about the new version:

node_name works
node_value does NOT work
nodes no longer have a contents property, and it is not clear how to get
the contents of a node.


30-Jan-2002 07:48

Well, actually, it works. Try the following code:

<?php

    $myXML =
"<mrow><mo>Coca-Cola</mo></mrow>";

    $docTree = xmltree($myXML);
    print("<pre>");
    
    # print whole document structure
    print_r( $docTree );  

    # print content of <mo> element
    print
$docTree->children[0]->children[0]->children[0]->content; 

    print( "</pre>" );
?>


10-Feb-2002 08:06

Hopefully this will help anyone who had as much trouble as I have using
domxml across different PHP versions...

I am using 4.1.1 now, with libxml2-2.4.13.

It seems that the way the XML tree is represented has changed from
4.06/libxml2-2.3.14 to use multple node types.

I had a lot of trouble figuring out how to get to the TAGNAME and CONTENT
data for my XML tree... but I figured it out and wanted to share:

function getNodeContent($name) {
    $child_a = domxml_children(domxml_root($doc));
    foreach ($child_a as $node) {
        if ($node->tagname() == $name) {
            // the CONTENT of a node is in the content field of a 
            // CHILD node of the tag name.
            // <NODE>DATA</NODE> is actually a NODE element
            // object with a TEXT element object as its only child.
            // THe child TEXT element has the content field with
            // the actual content.
            // it helps a LOT to do a print_r(xmltree($xml)) and
            // look at this as HTML SOURCE because it's nicely indented
            $content_a = domxml_children($node);
            return $content_a[0]->content;
        }
    }
}

jon.sprague(at)itgco.com
19-Feb-2002 06:54

This is what I did to get DOM to work with PHP on a Linux box.

1.
I installed zlib-1.1.3 from source ().
Don't forget the "./" in front of configure (the README leaves
this out).

I did a ldconfig -v (and I checked to make sure libz is there).

2.
I installed libxml2-2.4.9 from source ().
I used the "--with-zlib" extention.
I looked at the output of configure to make sure it found zlib (looked
something like this: "checking for zlib.h... yes").

I did a ldconfig -v (and I checked to make sure libxml is there).

3.
Then I installed PHP (ver. 4.1.1).
I used the "--with-dom --with-zlib" extentions.
I then made sure PHP recognized them by reading the output of configure:
(checking for DOM support... yes),
(checking if the location of ZLIB install directory is defined... no) I
ignored this,
(checking whether to include ZLIB support... yes).

As always reading the INSTALL and/or README files help tremendously.

I hope this helps someone!


06-Mar-2002 11:42

Note that any line breaks or white space between child tags will be
interpreted as child text nodes. These need to be stripped out to get an
accurate array of child nodes.

sbarnum at pointsystems com
15-Mar-2002 08:37

Here's a nice function for formatting an xml document.  Adds indents and
line breaks.
-----------------------------------------------------

function xml_format($xml) {
    // v 1.1, escapes backslash brackets //
    $xml = str_replace ("\n", "", $xml);
    $xml = str_replace ("\r", "", $xml);
    $xml = str_replace ("\\>", "&GT;", $xml);
    $xml = str_replace ("\\<", "&LT;", $xml);
    $out =
preg_replace_callback("|\<[^\>]*\>[^\<]*|",
"_xml_format_element", $xml);
    $out = str_replace ('&GT;', '>', $out);
    $out = str_replace ('&LT;', '<', $out);
    $out = str_replace ('<', '&lt;', $out);
    $out = str_replace ('>', '&gt;', $out);
    return nl2br($out);
}

function _xml_format_element($array) {
    // callback function for xml_format.  Do not invoke directly //
    $found = $array[0];
    static $indent;
    $found = trim($found);
    $tagOffset = 1;
    $openingText = "\n";
    $tab = str_repeat("&nbsp;", $indent * 4);
    if (substr($found, 0, 2)=='</') {
        // closing tag //
        $tagOffset = 2;
        $indent--;
        $tab = str_repeat("&nbsp;", $indent *4);
    } else if (substr($found, -2, 1)=='/' || strpos($found, '/>')) {
        // opening and closing tag //
        // do not change indent //
    } else {
        // opening tag //
        $indent++;
    }
    // $content = substr(strrchr($found, '>'), 0, -1);
    if (substr($found, -1) != '>') {
        // indent the content //
        $found = str_replace (">", ">\n" .
str_repeat("&nbsp;", ($indent+0)*4), $found);
    }
    return $openingText . $tab . $found ;
}


30-Apr-2002 11:58

in the manual says that some function are deprecated, but when I try to use
the new functions they are not implementet and I have to use the old ones.

Example:
xmldocfile  --  domxml_open_file()

Have I to upgrade or to installa any external library ??

I'm using Win32 platform to develop.
PHP-4.2.0
IIS-5.0

thank you


14-May-2002 12:52

Regarding the notes from
[email protected]
23-Nov-2001 06:27

here is a complement for win32

to make it very simple: php as an apache module

php.ini

extension_dir = c:\php\sapi

uncomment this line
extension=php_domxml.dll

drag and drop the php_domxml.dll which is originally in c:\php\extensions
into c:\php\sapi

you're done DOM is activated 

(nb same way to load the sablotron module except that you have to copy
these files which are originally inside c:\php\dlls 

sablot.dll
expat.dll
iconv.dll

into c:\winnt\system32)

two other things:

domxml_open_file() seems to require an absolute path starting at c:\....
however you can fread/file your xml file before and then use
domxml_open_mem() 

it is a good idea to validate your file before you try and parse it !! 

some urls to validate your xml:




enjoy urself !
ncik


20-Jun-2002 04:23

I have one huge problem when i use XPath implementation. My operative sysem
says that PHP script tries to get some unreachable memory.
It occures when i parse an XML document with help of XPath. I found that
it depends on file size. What can i do?


03-Jul-2002 02:54

Note that attempting to compile both the DOMXML and XMLRPC libraries 

(--with-dom and --with-xmlrpc)

into php will cause linking errors.

chiefgeek (at) ecodedesign.com
25-Jul-2002 11:13

With the release of PHP 4.2.0 exslt integration in the domxml extension was
added.  This can be configured using 
./configure --with-dom-exslt[=DIR] (and --with-dom-xslt) as can be seen in
the changelog.

The set of classes and methods that have been added are undocumented. 
After scouring the search engines I still came up with nothing...so I did
a little digging on my own.  This is what I have found so far.  I'll just
list new functions and classes then give some examples.

There are 5 new functions added: (I don't know all the paremeters yet, but
this is what i do know)

FUNCTIONS:
=> domxml_xslt_version
=> domxml_xslt_stylesheet 
=> domxml_xslt_stylesheet_doc
=> domxml_xslt_stylesheet_file
=> domxml_xslt_process

Function explanations:
=> domxml_xslt_version() - Have a guess what this does.  Hint it
returns a string.

=> domxml_xslt_stylesheet - expects only 1 parameter, a string
representation of an XSLT document.   Returns an object of class
XsltStylesheet

=> domxml_xslt_stylesheet_doc - not sure about this one yet, it takes
just one parameter, an object.  What kind?  Beats me.

=> domxml_xslt_stylesheet_file - just 1 parameter,  the filename (as a
string) of an XSLT doc.  Returns an object of class XsltStylesheet

=> domxml_xslt_process (?) still working on this one..

NEW CLASSES, as far as i can tell:

Class Name:
XsltStylesheet

Methods:
=>process

=> process - as far as I can tell this method takes in a DomDocument
representation of an XML file and returns a translated DomDocument.

Now for an example:
(Assume person.xml is a properly fomatted XML document.  Same goes for
person.xsl.)

<?php

// Create a DOM representation of the xsl file
$xsltdom = domxml_xslt_stylesheet_file("person.xsl");

// Create a DOM representation of the xml file
$xmldom = domxml_open_file("person.xml");

//Now for the magic
$result = $xsltdom->process($xmldom);

// Finally dump the contents to the browser
echo $result->html_dump_mem();

/**
 * Note $result is an object of class DomDocument and so many
 * methods can be used to modify and dump it to the browser.
 *
 * In actual fact $result is an XML representation of an HTML document
 */
?>

add a note about notes
previousrewinddirDomAttribute->namenext
Last updated: Tue, 16 Jul 2002
show source | credits | stats | mirror sites:  
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Fri Jul 26 12:06:26 2002 CEST