View Full Version : Parsing XML with php

04-15-2004, 10:23 PM

I'm fairly nw to php and have read few tutorials about XML parsing and dont understand it so thought best place to ask would be here.

I currently run a website with 4 bit torrent trackers which export there data to a xml file. I would like to create a php script that would retrieve the xml files frm the remote locations and parse them into a web page or if possible save results into a database.

The xml files look like:

<?xml version="1.0" encoding="ISO-8859-1" ?>
- <torrents>
- <torrent hash="08763f08912260d294a8a1a621e86216bf27f76f" filename="Longshot limited series 1-6.torrent" name="Longshot limited series 1-6" added="2004 03/25 08:33:44" size="61383147" files="6" completed="48" tag="Comics" uploader="jedispyder">
- <peers>
*<peer id="2d415a323038342d363776346446476847765774" ip="65.93.89.*" uploaded="9066979" downloaded="4800512" left="37560811" connected="49482" hop="0" />
*<peer id="4d332d342d322d2d643764376431363539356261" ip="65.161.114.*" uploaded="98304" downloaded="589824" left="37560811" connected="56712" hop="0" />
*<peer id="533538362d2d2d2d2d3274702d346c6e704b3635" ip="24.100.218.*" uploaded="0" downloaded="163840" left="37560811" connected="22781" hop="0" />
- <torrent hash="090a35536d3adf86ec8724c392e8cae422bf9dec" filename="Bone Issues #13 - 51 by Jeff Smith.torrent" name="Bone" added="2004 04/12 03:11:08" size="331196873" files="39" completed="78" tag="Comics" uploadname="Bone Issues" uploader="Psychocandy">
- <peers>
*<peer id="0000000000000000000000003ae6514604d89979" ip="140.247.126.*" uploaded="335112649" downloaded="331196873" left="0" connected="1646" hop="0" />
*<peer id="0000000000000000000000005d138269074bf3ac" ip="83.117.9.*" uploaded="49004544" downloaded="73662464" left="0" connected="5805" hop="0" />
*<peer id="00000000000000000000000070c0c45ec2209fcd" ip="217.42.19.*" uploaded="125812736" downloaded="331196873" left="0" connected="21428" hop="0" />
*<peer id="000000000000000000000000752f2742fc3cd15c" ip="24.157.12.*" uploaded="32522240" downloaded="100171776" left="210610633" connected="11010" hop="0" />

Can anyone help me out here and any advice?

Thanks in advance, Serj

04-16-2004, 12:02 AM

04-16-2004, 01:21 AM
I've been to that page, read it and just got so confused.

I see the sample code they give but i have no idea how it would relate the to xml file i have.

If no one else replies i guess im going to have to figure this out on my own or give up.

Thanks for reply, Serj

04-16-2004, 01:29 AM
I've never done anything like this, but maybe this will help:

Good luck,

04-16-2004, 01:55 AM
Thanks for that link, ill try to follow it and see what happens.

If anyone has anymore advice/help then let me know. Ill post back if i sorted it or if im still stuck when i had go at that link

Thanks, Serj

04-16-2004, 02:31 AM
Read url and it mentions Sablotron, i have no idea about this program/script and its C/C++ anyway so i wouldnt be able to run it server side like i would want to, well i dont think so from what i read but most of the Sablotron website stuff went right over my head as i didnt understand much of it.

I was hoping there was a way to just use XML/PHP and get the job done.

So im back ont he search and going to read up alot on xml and php now and see if any of it will make sense.

Keep helping coming if anyone has anything to offer

Thanks, Serj

04-16-2004, 09:10 AM
Where'd you see the C/C++ stuff?


04-16-2004, 12:39 PM
Ahem, for a beginner I wouldn't recommend to start XML parsing with XSLT processors. They require quite a bit upfront knowledge. Additionally, not every server has Sablotron installed, so YMMV.

Serj, your best bet is using the standard expat parser built into PHP by default. How it works is described in the XML section of the manual, as someone linked already to. Here's an introductory tutorial about XML parsing with PHP: http://www.zend.com/zend/art/parsing.php

Expat is a so-called SAX parser. SAX stands for "Simple API for XML". Nice, isn't it? ;) Basically the process of SAX parsing is pretty simple: The parser starts at the top of the document, and each time it encounters a new element or a CDATA section a callback function is called that does something with the data passed. For instance, if the desired elements are reached, their text values might be inserted into a SQL query.

Needless to say that you have to get familiar with some XML terms, what a node, element, CDATA section is. But then it's really quite straightforward working with the expat parser. If you have any specific questions, feel free to post them into this thread.