How to Read XML Using PHP DOM

This simple example mentions how the data from the xml file can be read and displayed in a textbox in the html file. It illustrates the concept which simplifies understanding the basics.

The structure of file.xml is:-


HELLO

This code lies in  file.php:-
<?php
 $xdoc = new DOMDocument( ‘1.0’, ‘UTF-8’ );
$xdoc->Load(“file.xml”);
$candidate = $xdoc->getElementsByTagName(‘root’)->item(0);
$newElement = $xdoc ->createElement(‘root’);
$txtNode = $xdoc ->createTextNode ($root);
$newElement -> appendChild($txtNode);
$candidate -> appendChild($newElement);
$msg = $candidate->nodeValue;
?>

This code lies in file.html



It will display the data in $msg variable in the textbox names msgval.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s