473,396 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

PHP4 XML DOM

Currently my web server supports only php4.
I want to know how to parse the xml and get the
element value using dom. I thought they have similar
function for getting element by id or tag name, so
far i haven't find anyting like that except fro PHP5 with
Domdocument. Any help is appreciated.
$dom = domxml_open_file ('data.xml');
$e_data = $dom->document_element();

<?xml version="1.0"?>
<book>
<title>hhelo</title>
</book>

Aug 17 '06 #1
3 8706

mich dobelman wrote:
Currently my web server supports only php4.
I want to know how to parse the xml and get the
element value using dom. I thought they have similar
function for getting element by id or tag name, so
far i haven't find anyting like that except fro PHP5 with
Domdocument. Any help is appreciated.
I heartily recommend upgrading to PHP5 and DOM extension,
it's much better than PHP4's DOM XML. Nevertheless, I hope
this helps:

$xml = '<?xml version="1.0"?>
<book>
<title>hhelo</title>
</book>' ;

// here we traverse shallowly all the children nodes of a
// given node and add node values of all the text nodes
// found to the result
function get_value ( $node )
{
$result = '' ;
$sub_nodes = $node -child_nodes ( ) ;
foreach ( $sub_nodes as $sub_node )
{
if ( $sub_node -node_type ( ) == XML_TEXT_NODE )
{
$result .= $sub_node -node_value ( ) ;
}
}
return ( $result ) ;
}

$doc = domxml_open_mem ( $xml ) ;

// method #1
// here we get all the <titleelements in the document,
// then get the contents of the first one of them
$titles = $doc -get_elements_by_tagname ( 'title' ) ;
$result_1 = get_value ( $titles [ 0 ] ) ;

// method #2
// here we get the first element node child of the root
// element, then get its contents
$books = $doc -child_nodes ( ) ;
$book_ix = $title_ix = 0 ;
while ( $books [ $book_ix ++ ] -node_type ( ) !=
XML_ELEMENT_NODE ) ;
$titles = $books [ -- $book_ix ] -child_nodes ( ) ;
while ( $titles [ $title_ix ++ ] -node_type ( ) !=
XML_ELEMENT_NODE ) ;
$result_2 = get_value ( $titles [ -- $title_ix ] ) ;

// method #3
// the same thing, only using XPath
$xpath = $doc -xpath_new_context ( ) ;
$result =
$xpath -xpath_eval ( '//book/title/text()' ) ;
$result_3 = $result -nodeset [ 0 ] -content ;

--
Pavel Lepin

Aug 17 '06 #2
Thanks, Pavel
>$doc = domxml_open_mem ( $xml ) ;
The function you provided works perfectly for local file, but
not the remote one. Do i have to use the different function remote
file?

about PHP5
I want to, but My ISP said not ready to upgrade to PHP5.

Aug 17 '06 #3

Shuan wrote:
$doc = domxml_open_mem ( $xml ) ;

The function you provided works perfectly for local file,
but not the remote one. Do i have to use the different
function remote file?
Actually, domxml_open_mem() parses XML from a string. For
both local and remote files you should use
domxml_open_file() - well, depending on your definition of
'remote' and your ISP's PHP settings ('allow_url_fopen').
The following should work:

$doc = domxml_open_file
( 'http://example.org/dir/data.xml' ) ;

--
Pavel Lepin

Aug 18 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nisha_tm | last post by:
Hello: I have a form in which I have checkboxes. Right now, I wrote the form's html and the php4 script. I use associative arrays in the form to capture the checkboxes that are checked. My...
0
by: Klaus Boehmer | last post by:
Hello, I'm trying to install gd2 with gif-support as an extension with php4. I compiled the patched version of gd2 - fine. I compiled php4 with gd=shared - fine. I installed it and restarted...
1
by: Erik | last post by:
I understand, that PHP4 has a MySQL client built in. I was warned, that this client does not support the MySQL 4.1.1 system, which I installed on my RH9 box end that I need to install that version...
5
by: Tim Tyler | last post by:
I'm sure this is a FAQ - but I could not find a coherent statement of the answer: Some of my clients want PHP4. Other ones want PHP5. Can I run both PHP4 and PHP5 under the same instance of...
0
by: Dave Pham | last post by:
I just cleaned my comp, and I am trying to re-config my webserver... I am trying to setup apache 2 so it runs both php4 and php5, I also have two instances of mysql running. I know this can be...
1
by: porneL | last post by:
I've had setup on WinXP: Apache1.3 with PHP4 module and PHP5 installedas CGI for one vhost. This worked fine, till I installed Zend Studio 4 beta. I'm trying to make Zend Server use my...
2
by: Stefan Huber | last post by:
Hi I've got a really strange problem, and can't find out why it's not working as intended. in order to use php4 and 5 together on a webserver and the requirement for running as different...
12
by: Drazen Gemic | last post by:
How long will PHP4 be supported ? When is PHP4 end of life scheduled ? DG
3
by: doctorhardik | last post by:
hai all i am try configure php4.3.9 on my FC-3 machine. and my mysql database version 5.0.1, in phpinfo file it show mysql but when i run php -v command it show error like
8
by: FFMG | last post by:
Hi, I am slowly moving my code to php5. But I would like to make it backward compatible in case something bad happens, (and to make sure I understand what the changes are). The way the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.