How to Use loadHTML, loadHTMLFile, and loadXML functions in PHP?

In PHP programming there might be scenarios where you need to load html strings, html files, and xml strings. These can be easily done using the functions available in PHP for this purpose. Functions like loadHTML, loadHTMLFile, and loadXML can be used to do these tasks.


Using loadHTML to load an html string

The loadHTML function can be used to load the html content available in a string source. It is not necessary that the string source should have well-formed html. It is possible to call this function statically when there is no need to set any DOMDocument properties before you load it.

The following example shows how this function is used to load an html string.

<?php
$webDoc = DOMDocument::loadHTML("<br><br><B>Loaded First html string</B>");
print $webDoc->saveHTML();

$webDoc = new DOMDocument();
$webDoc->loadHTML("<br><br><B>Loaded Second html string</B>");
print $webDoc->saveHTML();

$webDoc->loadHTML("<br><br><B>Load Third html string</B>");
print $webDoc->saveHTML();
?>

The output of the above code is show in the figure below:

Using loadHTMLFile to load an html file

You can use the loadHTMLFile function to load an html file by specifying the path of the filename. You can also invoke this function statically if you don't need to set any DOMDocument property. An example that uses this function is given below.

Consider an HTML file that has the code,

<h3>This is an external html file</h3>

This code is stored in the name of webpage.htm.

You can use the following code to load this html file and display it in the browser.

<?php
$webDoc = DOMDocument::loadHTMLFile("C:/php/xmlfiles/webpage.htm");
print $webDoc->saveHTML();
?>

The output for the above code is given as below:

Using loadXML function to load an XML string

With the loadXML function you can load the xml string from a string source. Like other load functions you can also invoke this function statically, if you are not going to set any DOMDocument property.

<?php

$strxml = <<<XML
<candidate>
<candidatename>Robert</candidatename>
<candidatecity>New York</candidatecity>
<SSN>20072007</SSN>
<organization>XYZ Corp</organization>
</candidate>
XML;

$newdom = new domDocument;
$newdom->loadXML($strxml);
$sxmldom = simplexml_import_dom($dom);
echo "Candidate Name is <B>"
. $sxmldom->candidatename[0]."</B>";
echo "<BR>The Candidate is in <B>"
. $sxmldom->candidatecity[0]."</B>";
?>

In the above code the variable $strxml is loaded with the xml string and then the loadXML file is called to load the xml in to the DOMDocument. Two elements of the node candidate is displayed using the code above.

The output for the above code is given below:



SetApp - 100 Apps for everything you ever wanted to do on Mac

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

_______________________________________



 

FREE Subscription

Stay Current With the Latest Technology Developments Realted to XML. Signup for Our Newsletter and Receive New Articles Through Email.

Name:

Email:

Note : We never rent, trade, or sell our email lists to anyone. We assure that your privacy is respected and protected.

 

 

Add to My Yahoo!

Visit XML Training Material Guide Homepage

 

 

“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

Copyright - © 2004 - 2019 - All Rights Reserved.