Forgot username/password? Don't have an account?
Login with Facebook
Home » Courses » 02Hero XML » PHP XML compare
Subscribe for updates.
* New to us? log in with Facebook and get a free day pass

PHP XML compare

One fact you must memorize whenever you want to create, modify, parse or process information in an XML document: everything is a character string. There are no other content types in an XML document. This property ensures that XML remains universally flexible and compatible with a whole lot of software libraries written in different languages.

There are a few interesting programming scenarios, regarding comparison of different XML elements that belong to the SAME element category. A simple example XML document and the PHP code to process it, will make things crystal clear. Here's another real-life example:

A crime has been committed and the NYPD officers are busy trying to match descriptions and clues of the suspect with police records in the database. Unfortunately, they don't have a photograph or scanned image of the suspect, but they do have a rough sketch drawn by the police artist, based on observations from onlookers. Facial features of the sketch are stored as elements in an XML profile in profile.xml as well as the official photo features. A program runs through the official files in the database looking for a match with the sketch XML file.

The XML document format might be something like:

<?xml version="1.0" encoding="UTF-8" ?>

<profile>

<nose shape="sharp" width="narrow" nostrils="large" />

<eyes color="blue" squint="yes" />

<chin zits="yes" many="no" /> <chin cleft="yes" large="no" />

<hair color="blond" long="no" curls="yes" />

</profile>

Now the program matches suspect.xml with profile.xml for each criminal in police records. PHP code that would perform such operations would look like:

<?

$suspect = simplexml_load_file("suspect.xml");

$profile = simplexml_load_file("profile.xml");

if((string)$profile->nose['shape'] == (string)$suspect->nose['shape']){

echo "Suspect's Nose Shape Matches Record!";

if(... )

//%u2026 you get the idea...

}

?>

Here, specific attribute values of two XML documents are being matched. If the "shape" attribute value of the suspect's "nose" element matches the corresponding value in the current profile being examined, a message gets displayed in the browser. Note that we have cast the value to string type because the browser has no way to figure out the type that matches the expression being compared. Only one side of the comparison actually needs to be cast to string type: the other expression is automatically cast to string based on context.

Note that if the same document is opened twice and stored in different variables, the two still aren't identical. At runtime, each has its own memory location, and a PHP check for equality will always return false. They can be used independently, without one being affected by changes to the other.

<?

$xmlone = simplexml_load_file("profile.xml");

$xmltwo = simplexml_load_file("profile.xml");

echo ($xmlone == $xmltwo); // always returns false

?>

If this code is executed, the browser renders a blank page.

This is a very important observation. Don't worry if you're confused... run the video again and the explanations will be very useful in forming a mental picture.

Got A Question?

'I love being Prime'

Have a question but don't want to ask it publicly? If you are a prime member just ask right here and we will get back to you within 48 hours. Not prime yet? no worries you can ask publicly below.

Act now while we have available seating

Our on line school is private, affordable and interactive with trainer support.
Act now save your seat before someone else takes yours.
Act now