Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Php & Xml

case sensitive xml

  • Please log in to reply
No replies to this topic

#1 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts
  • Location:Quebec City
  • Programming Language:Java, C++, C#, PHP, JavaScript, Visual Basic .NET, Transact-SQL, ActionScript

Posted 10 November 2011 - 09:44 AM

What is xml?

Xml stands for Extensible Markup Language, but this dosen't really help you understand what is xml... in fact I had to google to know what the X was for...

In a nutshell, xml is a way to store data in a file. You can stream this file, and use it to communicate between 2 programs, or use it as a way to remember data.
Don't get me wrong, xml isn't a database, you should not start using xml to store big projets like a forum... even if it is possible to do.

A lot of time, people tell me that they don't want to learn xml because it look too complicated. Well actually, to learn xml, it take about 10minutes, and you can be an expert in xml.
You probably already read some xml, maybe even used it without even knowing it was xml.
Take a look at this page http://feeds.feedbur...call?format=xml
If you look at the source of this page, you'll see this is not normal html code. This is xml code.
Yes rss is xml.
xhtml is xml too.
But html isn't really xml... it may look like, but html and xml dosen't really have the sames rules.

For this tutorial, I'll take the example of a restaurant menu.
This is a valid xml document:
<?xml version="1.0" encoding="UTF-8"?>
<menu>
  <breakfast>
    <item price="5.50">
      <name>Toast</name>
      <description>A description of toast</description>
    </item>
    <item price="2.10">
      <name>Egg</name>
      <description>
        Two egg to eat.
      </description>
    </item>
  </breakfast>

</menu>


So as you can see, it's pretty simple.
Anyone could read this without any problem.
But the force of xml is that it's well known, so pretty much every language have library or methods to read or write a xml file!
So a first program (writted in c++) could create this menu, and send it to a second program (written in PHP) that could read it without any problem... of course, I don't see why 2 programs would want to share a menu, but you can think of better example.
And it doesn't have to be c++ and PHP, any language can do it.

As you may guess, there are a some rules to write a valid xml, but lucky for us, those rules are easy to understand.

Rules
- First rules: only one root for the file.
This means you CANNOT DO THIS
<?xml version="1.0" encoding="UTF-8"?>
<item price="5.50">
  <name>Toast</name>
  <description>A description of toast</description>
</item>
<item price="2.10">
  <name>Egg</name>
  <description>
    Two egg to eat.
  </description>
</item>

In this example, you are 2 root element (2 item)

- Next rules: every element has to be closed.
If you open a element like so <item> you have to close it </item>
You could always self close it like this <item />
And do not forget, element are case sensitive
so <item></Item> will NOT work.

- Element cannot begin with the letter xml, a number, a punctuation or any special characters, except for the underscore (_) character, and cannot contain space

- All attribute (<item price="2.10">, in this example, price is an attribute) has to be quoted.

- And last rules: elements has to be properly nested. So
<menu>
  <item>
    <breakfast>
     ...
    </item>
  </breakfast>
</menu>
Is not a valid xml code.


A question that is often asked is should I use element or attribute?
There is no right or wrong answer. You can have as many attribute you want in a element, simply go with what feel right.
In my first example, I use an attribute for the price, but I could have ise a attribute for the name too, or put the price in a element.
None of them are faster, or better, but don't ever do something like that
<item desc1="..." desc2="..." desc3="..." desc4="..."></item>
If ever you have something like that, you should think of using childs elements, even if attribute will work in this example, it is not the best way.

So you see, xml isn't that hard, you already know all the rules, and so, you can now call yourself a xml master!

But that not all, you want more to simply understand it, you want to be able to read and write it in php.
First step to do so, upgrade your php version. The function that we will be using is only working in php5, so if you are still in php4, it's about time you upgrade!

And PHP
Now that you have php5, do a phpinfo(); and take a look if you have SimpleXML installed.
If SimpleXML is not installed, do a quick google search, and you will learn how to install it. I'm not going to tell you how to install it since it depend on your os, apache version and how your php is installed, but normally it's quite simply.
Hopefully, SimpleXML is already installed, so let's digg in!

In a folder, create a file named test.xml and in it we will have this example:
<?xml version="1.0" encoding="UTF-8"?>
<menu>
  <breakfast>
    <item price="5.50">
      <name>Toast</name>
      <description>A description of toast</description>
    </item>
    <item price="2.10">
      <name>Egg</name>
      <description>
        Two egg to eat.
      </description>
    </item>
  </breakfast>

</menu>

In the same folder create an second file named index.php

In your php file, the first thing to do is to load the xml file:
<?php
$xml = simplexml_load_file('test.xml');
?>

And after that, it's only easy, if you'd like to read the name of the first item in breakfast you do it OO style
echo($xml->breakfast[0]->item[0]->name);
Like you can see, your php code will depend on the structure you decided for your xml.
breakfast and item have [0] after them, this is because, breakfast or item could have multiple instance. So if you want to echo the name of the second item, it is as much easy
echo($xml->breakfast[0]->item[1]->name);

But what if you'd like to echo every items name in the breakfast menu?
Once again, simpleXml is... well is really simple. If you look closely to the previous example, the way you get the index of the element look a lot how you get an element in a array.
And with that in mind, you will understand that you can do a foreach on a element
foreach($xml->breakfast[0] as $item) {
  echo($item->name . '<br />');
}

The only thing you can't read yet is the price of a item. The difference is because in this example I use attribute to set the price
But reading a attribute is as easy as the rest.
echo($xml->breakfast[0]->item[1]->attributes()->price);
You simply need to use the function attributes() and you will get your attribute.


So now, you can read all the file. And as you can see, it's quite easy.
Let's try if we can write a xml file.
Create a new php file with this in it
<?php
$xml = new SimpleXMLElement('<menu></menu>');
$breakfast = $xml->addChild('breakfast');

$item = $breakfast->addChild('item');
$item->addAttribute('price','5.50');
$item->addChild('name', htmlspecialchars(utf8_encode('Toast'));
$item->addChild('description', htmlspecialchars(utf8_encode('A description of toast'));

$item = $breakfast->addChild('item');
$item->addAttribute('price','2.10');
$item->addChild('name', htmlspecialchars(utf8_encode('Egg'));
$item->addChild('description', htmlspecialchars(utf8_encode('Two egg to eat.'));

echo($xml->asXML());
?>


And now execute this code with your favorite browser (hopefully not IE)
Once executed, you will see ... nothing, or maybe just some text.
But your code worked. It's simply because your browser think this is html code, and don't know how to execute it, so he simply don't.
But if you go take a look at the source, you will see your new xml code.

Let's read this example together to understand it more.
The first line, I create a new SimpleXMLElement with the name of my element.
This will become the root element, and an SimpleXmlElement is returned

After that, I create the element breakfast, with the method addChild. I only use one parameter since breakfast doesn't have content in it.
Than I create an item the same way I create the breakfast.
Than with the method addAttribute I add the price on this element.

And on the next line (very important line), I create the element name with the addChild method but with 2 parameter, the first one is for the name of the child, and the second one is for his data.
But I also use 2 other function htmlspecialchars & utf8_encode
In this example, it doesn't do anything.
But you won't always create xml file with constant, sometime it will be with variable like so
$item->addChild('name', htmlspecialchars(utf8_encode($value));
What is $value have some special char in it? for example $value = "hi</item>there";
In this example without htmlspecialchars & utf8_encode the xml you'll generate will throw an error, since the <item> element is closed twice.
So htmlspecialchars & utf8_encode will replace all special character, with "xml correct" character, and be sure to translate everything in utf8.




If you'd like to see it directly in your browser, and in a more user friendly way to see it add this code at the top of your php file (just after the <?php)
header ("Content-Type:text/xml");
This will tell your browser that the content of this page is a xml file.


One last thing:
The method $xml->asXML() can take one parameter.
And it's an file path. If you give a valid path to asXML(), insted of returning the value of the xml, he will write it to a file and return a boolean for the success of the writing.

With that in mind, you can read, write, and view a xml file.
Hope this can help some of you

Feel free to reply for any question, comments or errors (hopefully not too much of the last one)

See ya next time.
  • 0





Also tagged with one or more of these keywords: case sensitive, xml

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download