Senior Trainer-Instructor- Developer C++ / C UNIX & Web-JAVA / J2EE,PhD or Masters
WSI Nationwide, Inc.
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

XML How-To
Recently, at work I was given the job of learning XML. OK, so it wasn't technically XML, it was RDF, but I found that PHP's XML parsing functions worked just the same. At work I parsed out DMOZ (http://www.dmoz.org) but for simplicity I will stick with the basics of XML and then leave you free to parse DMOZ in your extra time ;o)
To begin with you will want to make sure that you have a PHP binary compiled with the '--with-xml' option enabled. Once that is complete you are ready to start parsing XML. Next grab Slashdot's XML file from their homepage (www.slashdot.org/slashdot.xml). Slashdot has a fairly simplistic file that is extremely easy to parse.
Remember that when you are working with XML it is a lot like working with a table in a database. You have a result index in the xml parser and a psuedo table in the XML document. Once you get over the differences you will be parsing in no time.
PHP's XML functions allow you to specify three functions that will handle the data in the XML file. One handles opening tags, one hands the data between tags, and the third handles the ending tags. Based on the name of the tags it gets passed you can then manipulate the data however you please. To begin with you need to look at your XML document and find out what tags are in the file. In our slashdot file we have STORY, TITLE, URL, TIME, AUTHOR, DEPARTMENT, TOPIC, COMMENTS, SECTION, and IMAGE. In some cases you would have attributes, and example is HREF is an attribute to A in HTML. PHP has an extremely cool way of handling attributes automagically. Next we need to define those tags in our script.

<?php

$open_tags
= array(
    
'STORY' => '<STORY>',
    
'TITLE' => '<TITLE>',
    
'URL' => '<URL>');

$close_tags = array(
    
'STORY' => '</STORY>',
    
'TITLE' => '</TITLE>',
    
'URL' => '</URL>');

?>
I only want to parse out the above data because I just want to make one of those cool Slashboxes. Next on our list is to make the functions that will extract this data. On the following page are the functions that I created to do so.
[ Next Page ]


Comments:
the best sex siteDmitry11/17/05 08:23
RE: XML declaration error : ( help me out.....crashpoint_zero03/02/05 01:12
XML declaration error : ( help me out.....crashpoint_zero01/25/05 02:06
RE: XML db guestbook?MevelTribe01/28/03 03:54
RE: XML db guestbook?Pir801/08/03 04:39
RE: how to deal with <P />?Hector Vergara10/26/02 22:40
RE: How to POST XML data?Tweaktijn10/24/02 03:05
RE: How to POST XML data?Richard Hundt09/08/02 07:42
RE: How to POST XML data?Richard Hundt09/08/02 07:26
how to deal with <P />?Craig Nakamoto08/28/02 19:16
POST XML documentTamer El-Nasser 08/21/02 09:07
creating a new file?Stevz08/13/02 07:03
Trouble with Expat in IISwyswilson08/12/02 04:14
URL Variables with XMLGabriel08/06/02 15:10
RE: why caps?Tucker McLean07/31/02 13:57
How to POST XML data?Ivor Brands07/31/02 06:28
RE: Programming Advantages ... WHAT?!!!hobbit07/28/02 20:34
A question about the exampleArne Schwabe07/28/02 05:18
XML db guestbook?Igro06/28/02 06:58
call to undefined function xmldoc()Kavitha06/11/02 02:15
how to exit parsing once I get what I want.eric pan06/07/02 12:59
RE: How to read xml from POST?Todd Anderson05/14/02 16:08
RE: Prob:strings longer 1024 chars are truncatedIsh05/02/02 22:36
RE: How to read xml from POST?john04/23/02 03:49
dynamic $xml_filearman04/22/02 19:59
How to compile PHP binary fto enable xml Yogesh Randhawa04/22/02 01:35
parse to variable?Jeremy04/03/02 21:31
RE: dmoz with php and mysqldavid d03/27/02 07:54
RE: XML Tag attributes - Joe Stump Help!sculptor02/23/02 13:31
How do I build a Image in a Tag?Sofia02/22/02 11:39
RE: Help error on first lineJay Hawkins02/18/02 13:58
RE: dmoz with php and mysqlTobsn02/14/02 12:43
Help error on first linePaul 02/11/02 07:09
Tag Names PrintingSteven Chalker02/02/02 20:52
XML Tag attributes - Joe Stump Help!Sandeep02/01/02 07:14
call resultsrens01/15/02 10:24
RE: Some HTML not supported?Mike Hall01/09/02 18:53
why caps?kremb12/20/01 11:38
Some HTML not supported?Thomas Brune12/14/01 04:53
RE: system,exec, functions in xml?Ranjith11/28/01 23:13
Array Parse errorNewbie11/14/01 17:50
RE: How to produce that XML input fileDario Nuevo11/12/01 09:04
Error running the script - what's wrong? HelpBecky10/08/01 10:01
Searching an XML FileRuss Jones10/01/01 01:28
RE: HELP!!! Redirecting to a URL from XMLGreg M09/25/01 21:07
RE: HELP!!! Redirecting to a URL from XMLGreg M09/25/01 20:10
Prob:strings longer 1024 chars are truncatedmatthias09/18/01 11:08
system,exec, functions in xml?JOHN09/14/01 13:08
Accounting ERPBharat Pondugula07/26/01 23:25
RE: The treatment of "&amp;" in contentAllan Lykke07/24/01 05:23
How to read xml from POST?Stanko07/03/01 18:02
How do I install xml support?Luis07/02/01 16:47
How do i search an xml filerobert collyer06/20/01 02:37
XML & Calendarmoses05/04/01 16:45
RE: How to solve this problem - child elementTiefeng Jia04/26/01 05:39
php for xmlhack.com?James Cummings04/24/01 07:06
RE: How to solve this problem - child elementAli Driver04/23/01 10:44
Read XML source questionTiefeng Jia04/19/01 03:18
Parse error: parse error, expecting `'&'' ortassilo04/18/01 05:27
RE: XML Tag attributes - Joe Stump Help!Cong04/04/01 19:46
RE: Parse LinkWatcher - ElementsCong04/04/01 16:09
HELP!!! Redirecting to a URL from XMLPedro Costa04/02/01 13:56
How to solve this problemHongrun Yu03/29/01 14:54
RE: dmoz with php and mysqlIrina03/12/01 08:16
xml-->bd relationnelouahab02/27/01 12:55
MSXML - MSXSL on a IIS running PHP4Jack02/01/01 15:01
parsing more than one fileManish01/18/01 04:33
RE: array questionManish01/18/01 04:25
RE: Parse LinkWatcher - ElementsManish01/17/01 08:15
Copy CatJon Herron01/15/01 20:24
array questionjeremy01/15/01 17:54
Find XML files on NewsIsFree.comPino01/11/01 04:31
Parse LinkWatcher - ElementsSalted Wound01/05/01 16:49
XSLT , Parse error: HELP !!!Kevinjohn12/28/00 13:15
dmoz with php and mysqldennis12/21/00 07:03
RE: How to produce that XML input fileOzgur Unat12/06/00 08:12
RE: Where to find XML files?Gary Lawrence Murphy11/17/00 09:43
How to produce that XML input fileRob Chan11/14/00 21:50
Ensuring that special chars eg &amp; display.Ed Gale11/03/00 05:56
XML Tag attributes - Joe Stump Help!Sammy Lusiola10/26/00 13:24
RE: Programming AdvantagesBryan Brunton10/23/00 16:41
RE: Programming AdvantagesJimmie10/15/00 13:22
XML and FlashKirk Jones10/12/00 16:55
RE: Programming Advantages/Hey JoeT.R. Cox10/07/00 06:55
RE: Programming Advantagesjoe10/06/00 17:12
Programming AdvantagesRyan10/06/00 00:20
RE: Where to find XML files?jon eyre09/24/00 10:53
RE: Where to find XML files?mocha09/22/00 02:56
RE: Parsing Nasdaqmocha09/22/00 02:49
Where to find XML files?Justin Ryan09/21/00 21:38
RE: Parsing NasdaqBrian Vicente09/21/00 14:56
RE: Parsing Nasdaqmocha09/20/00 19:55
RE: Parsing NasdaqLawry09/20/00 12:08
RE: Parsing Nasdaqmocha09/20/00 03:21
RE: Parsing Nasdaqmocha09/20/00 00:25
Parsing NasdaqLawry09/19/00 12:37
RE: The treatment of "&amp;" in contentDan Barnes09/19/00 11:28
RE: More precise: reatment of & amp ; in contentJoe Stump09/15/00 20:00
More precise: reatment of & amp ; in contentTom Henry09/15/00 16:27
The treatment of "&amp;" in contentTom Henry09/15/00 16:22
RE: expected output?Vincent Stoessel09/14/00 15:25
RE: expected output?mocha09/14/00 15:11
expected output?Vincent Stoessel09/14/00 14:06
RE: A couple of queriesmocha09/14/00 08:26
RE: A couple of queriesJoe Stump09/12/00 22:06
A couple of queriesDan Barnes09/12/00 12:34
backend archive herephilip olson09/11/00 16:47
RE: dmoz rdfJoe Stump09/11/00 00:37
RE: Why use a cron job ?Joe Stump09/11/00 00:33
RE: DTDs & SchemasJoe Stump09/11/00 00:32
DTDs & SchemasEverton de Brito Dias09/10/00 16:46
RE: Why use a cron job ?Jeroen Latour09/09/00 19:25
dmoz rdfmocha09/09/00 18:00
Why use a cron job ?Justin Grant09/08/00 17:22
Quick cron exampleBrian Sugrue09/08/00 15:45
OutstandingBrian Sugrue09/08/00 15:13
Great!Vassil Peytchev09/08/00 14:36
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.