Posted by: phillipnb | May 3, 2011

PHP and XML – Part 1


Yesterday, when I talked to few of my colleagues I came to know how little they knew about php and xml. Either they were ignorant about many xml built-in functions and libraries that php had or they were afraid to use it. Either way I thought I should bring out a series of articles on my blog to help these folks to get started.

The word ‘XML’ stands for extensible markup language, a subset of the mother of all markups called SGML. If you ask me what SGML is I am not going to say that here as that is my aim for writing this blog. All I can say is that in the world of computer science, XML is the format of choice for communication between disparate system(the disparity could be in terms of hardware or software or both). We can talk about the word ‘XML’ for days but that is not what I intend to do here. As usual this write-up about xml is also going to be a brief introduction to the series starting with the definition of xml, some fundamentals and then we will move on to SimpleXML library and finally to XPath.

All xml documents can have a xml declaration statement with a version attribute, e.g.. If an xml document has a xml declaration then it has to be first statement of that document. Every xml document can contain entities, elements and attributes. An entity can be anything – it can be a section for external data, it can be used for declaration of elements or it can be a placeholder for repeatable characters. Elements are used to classify data in a xml document and elements can also be called as tags. A xml element includes everything from the start tag to the end tag.i.e,it can contain other elements, text, attributes or a combination of all these.Elements are surrounded by angular brackets and their names usually describes the type of content they are holding. Each element may or may not contain attributes and while elements contain data, attributes contain meta-data.

There are two more terms associated with an xml that we need to know. They are

  • well-formed xml
  • valid xml

.
A xml document is well-formed if it obeys the following rules:

  • The xml declaration if used in a xml document must have to be the first line in that document
  • There has to be a root element or root tag for every xml document
  • Tags used in a xml document must be properly nested
  • Other than empty tags,all open tags must be closed
  • Attribute values must be surrounded by a single or double quote

If a xml is well-formed and follows the rules of the referenced
DTD(internal or external) then it is a valid xml.

So, that was just a brief introduction to xml. In the next edition of this series, we will walk through the SimpleXML library and its ability to support our code in php.

Till next time, Happy PHPing.

About these ads

Responses

  1. Thought I would comment and say neat theme, did you make it for yourself? It’s really awesome!

    • No, it is a wordpress theme


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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.

Join 25 other followers

%d bloggers like this: