User Contributed Notes DOM XML functions |
|
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.
<?php
$docnode = xmldoc("<?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 ("\\>", ">", $xml);
$xml = str_replace ("\\<", "<", $xml);
$out =
preg_replace_callback("|\<[^\>]*\>[^\<]*|",
"_xml_format_element", $xml);
$out = str_replace ('>', '>', $out);
$out = str_replace ('<', '<', $out);
$out = str_replace ('<', '<', $out);
$out = str_replace ('>', '>', $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(" ", $indent * 4);
if (substr($found, 0, 2)=='</') {
// closing tag //
$tagOffset = 2;
$indent--;
$tab = str_repeat(" ", $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(" ", ($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.
|
|
|