Coder Profile - Show off your skills, get a coder profile.
 
 
 
[PHP] XML: HTML DOM Basics
Web Development
PHP has built-in modules that you can use to load and parse XML files. You can also use the HTML DOM (depends on your browser) or external libraries that you have to go through extra setup for. Just to simplify the process, I'm going to be using the DOM. If you're not comfortable with it, you can look it up at http://www.w3schools.com.

One of the things that you should not do when loading XML files is to use the PHP filesystem library. Such as fopen and fread. The problem with these is that you have to do your own means of parsing the file, which could take you a lot of time and a lot of space. Instead we just want to use a library that's already done all that. It would be impractical otherwise.

To create an instance of the DOM, do variable = new DOMDocument(). We can now call its methods and do some actual execution. We can call load to load the file, and then asXML() to return the data read (tags are ignored). Note that the file is not automatically loaded. You can research this.
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. $_dom = new DOMDocument();
  2. $_dom->load("louis.xml");
  3. echo $_dom->asXML();
Since the tags are ignored, it looks pretty ugly. We can format it by running a foreach loop through the file's nodes. If you're not familiar with the XML DOM, you should look it up as to avoid confusion. I will not explain it here. When we're referring to the DOM instance, it will refer to the first node unless specified otherwise. $_dom[0] is redundant. We can obtain its children by calling children().
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. $_children = $_dom->children();
  2. foreach ($_children as $_child)
  3.     echo $_child . "<br />";
That's about it. You can mess around with it to see the different results you get.
CODE: Copy / Restore  ::  Remove Scroll Bars
  1. children() - returns node's children (ie, <boy /><sister />)
  2. nextSibling() - returns next node under same parent (ie, <boy /><sister /> returns sister)
  3. prevSibling()
  4. attributes() - returns node's attributes (ie, attr="ibute")


Posted By remy
Please login to rate coding articles.

Click here to register a free account with us.
Comments
Please login to post comments.
 
Friso     Posted 3.72 Years Ago
 
 
I also wanted to write a article about XML & PHP but i guess you covered it all
:D
 
remy     Posted 3.89 Years Ago
 
 
I'll write a more extensive article supporting it later.
 
Relish     Posted 3.89 Years Ago
 
 
Nice article :D
 
VBAssassin     Posted 3.90 Years Ago
 
 
Have you thought about adding the contents of: louis.xml

Nice article anyway - xml i think is very under documented around the net for php.

Kind regards,
Scott
Page 1 of 1
More Articles By This Author
[PHP] XML
[PHP] XML: HTML DOM Basics
Recently Posted "Web Development" Articles
Website Optimization
How to use Memcache on Windows
PHP Sessions = Member Database Cache
Speed Up Delivery of your JavaScript Libraries
A cool tool ^^
AJAX - When to use it?
Introduction To PHP
[PHP] XML
HTML vs XHTML
[PHP] XML: HTML DOM Basics
Recently Rated "Web Development" Articles
Embed flash into your website
PHP Sessions = Member Database Cache
What makes a good PHP programmer?
Website Optimization
Preventing raw PHP leaks on Apache
How to use Memcache on Windows
AJAX - When to use it?
Speed Up Delivery of your JavaScript Libraries
[PHP] XML
A cool tool ^^
source codes Categories articles
Browse All
Business & E-Commerce (1)
Databases (1)
Design & Creativity (1)
Hardware (1)
Internet & Web Sites (3)
Life In General (2)
Networking (1)
Operating Systems (4)
Other (2)
Programming (46)
Security (8)
Software Development (5)
Web Development (14)
search Search Inside
Web Development
 
 
Latest News About Coder Profile
Coder Profile Poll
If you made money from keeping your profile up to date, say $30 and up, per month. What extra time would you spend on your profile?

No extra time
A few hours at weekends
A whole day each week
Every minute i can get free


please login to cast your vote
and see the results of this poll
Latest Coder Profile Changes
Coder Profile was last updated
3.20 Years Ago
Official Blog :: Make A Donation :: Credits :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents :: Wallpapers
Version 1.46.00
Copyright © 2007 - 2012, Scott Thompson, All Rights Reserved