Jump to content

[SOLVED] XML PARSING.


atlanta

Recommended Posts

Hi, Not to good with xml parsing so needed a little help.

ok here we go..

i have this xml file

  <?xml version="1.0" encoding="iso-8859-1" ?> 
- <mixtape Title="Crack RNB Vol 3" Artist="Various Artists" Artwork="http://www.domain.com/images/" Tracks="21" Rating="4" Listens="6,422">
  <track id="238969" position="1" title="T.i.+ft.+tiny+-+lately" file="01-TI-ft-Tiny---Lately.mp3" length="235.55" /> 
  <track id="238970" position="2" title="Ntl+-+jump+jump" file="02-NTL---Jump-Jump.mp3" length="240.2" /> 
  <track id="238971" position="3" title="Jordin+sparks+ft.+chris+brown+-+no+air" file="03-Jordin-Sparks-ft-Chris-Brown---No-Air.mp3" length="248.53" /> 
  <track id="238972" position="4" title="Kyonte+-+99" file="04-Kyonte---99.mp3" length="215.64" /> 
  <track id="238973" position="5" title="Ne-yo+-+dream" file="05-Ne-Yo---Dream.mp3" length="189.26" /> 
  <track id="238974" position="6" title="One+chance+-+one+girl" file="06-One-Chance---One-Girl.mp3" length="231.97" /> 
  <track id="238975" position="7" title="Chris+brown+-+dying" file="07-Chris-Brown---Dying.mp3" length="110.42" /> 
  <track id="238976" position="8" title="Issa+ft.+trina+-+boss+lady" file="08-Issa-ft-Trina---Boss-Lady.mp3" length="244.85" /> 
  <track id="238977" position="9" title="Lyfe+jennings+-+never+never+land" file="09-Lyfe-Jennings---Never-Never-Land.mp3" length="232.91" /> 
  <track id="238978" position="10" title="Akon+ft.+40+glocc+-+borrow+u" file="10-Akon-ft-40-Glocc---Borrow-U.mp3" length="243.62" /> 
  <track id="238979" position="11" title="Lil+mama+ft.+t-pain+%26+chris+brown+-+shawty+get+loose+%28official+remix%29" file="11-Lil-Mama-ft-T-Pain-amp-Chris-Brown---Shawty-Get.mp3" length="201.95" /> 
  <track id="238980" position="12" title="Slim+%28of+112%29+ft.+pitbull+-+i+ain%27t+scared" file="12-Slim-of-112-ft-Pitbull---I-Aint-Scared.mp3" length="288.44" /> 
  <track id="238981" position="13" title="Lil+eddie+ft.+defy+-+easier+to+stay" file="13-Lil-Eddie-ft-Defy---Easier-To-Stay.mp3" length="201.72" /> 
  <track id="238982" position="14" title="Uness+-+taste+your+heartbeat" file="14-Uness---Taste-Your-Heartbeat.mp3" length="180.74" /> 
  <track id="238983" position="15" title="Razah+-+club+hobbing" file="15-Razah---Club-Hobbing.mp3" length="219.56" /> 
  <track id="238984" position="16" title="Cheri+dennis+-+flow+wit+me" file="16-Cheri-Dennis---Flow-Wit-Me.mp3" length="170.29" /> 
  <track id="238985" position="17" title="Lisha+ft.+scola+%28of+dru+hill%29+-+i+got+you" file="17-Lisha-ft-Scola-of-Dru-Hill---I-Got-You.mp3" length="197.15" /> 
  <track id="238986" position="18" title="Tiffany+evans+ft.+bow+wow+-+i%27m+grown" file="18-Tiffany-Evans-ft-Bow-Wow---Im-Grown.mp3" length="197.77" /> 
  <track id="238987" position="19" title="Ray+l+-+all+i+want+is+her" file="19-Ray-L---All-I-Want-Is-Her.mp3" length="189.68" /> 
  <track id="238988" position="20" title="Joelle+-+drop+her+off" file="20-Joelle---Drop-Her-Off.mp3" length="176.51" /> 
  <track id="238989" position="21" title="Deepside+ft.+dre+-+feelin+like+a+pimp" file="21-Deepside-ft-Dre---Feelin-Like-A-Pimp.mp3" length="209.95" /> 
  </mixtape>

 

Ok i want the xml to parse each track line and extract the data from tite= and file=..

 

if anyone code point me towards a good tut or help me out a little thanks

Link to comment
Share on other sites

If you go to :

http://www.criticaldevelopment.net/xml/doc.php

and download the php4 parser class, and name it parser.php

 

then you can save your xml file with the name tape.xml. I had to clean up your XML to get it to work:

 

tape.xml

<?xml version="1.0" encoding="iso-8859-1" ?>
  <mixtape Title="Crack RNB Vol 3" Artist="Various Artists" Artwork="http://www.domain.com/images/" Tracks="21" Rating="4" Listens="6,422">
  <track id="238969" position="1" title="T.i.+ft.+tiny+-+lately" file="01-TI-ft-Tiny---Lately.mp3" length="235.55" /> 
  <track id="238970" position="2" title="Ntl+-+jump+jump" file="02-NTL---Jump-Jump.mp3" length="240.2" /> 
  <track id="238971" position="3" title="Jordin+sparks+ft.+chris+brown+-+no+air" file="03-Jordin-Sparks-ft-Chris-Brown---No-Air.mp3" length="248.53" /> 
  <track id="238972" position="4" title="Kyonte+-+99" file="04-Kyonte---99.mp3" length="215.64" /> 
  <track id="238973" position="5" title="Ne-yo+-+dream" file="05-Ne-Yo---Dream.mp3" length="189.26" /> 
  <track id="238974" position="6" title="One+chance+-+one+girl" file="06-One-Chance---One-Girl.mp3" length="231.97" /> 
  <track id="238975" position="7" title="Chris+brown+-+dying" file="07-Chris-Brown---Dying.mp3" length="110.42" /> 
  <track id="238976" position="8" title="Issa+ft.+trina+-+boss+lady" file="08-Issa-ft-Trina---Boss-Lady.mp3" length="244.85" /> 
  <track id="238977" position="9" title="Lyfe+jennings+-+never+never+land" file="09-Lyfe-Jennings---Never-Never-Land.mp3" length="232.91" /> 
  <track id="238978" position="10" title="Akon+ft.+40+glocc+-+borrow+u" file="10-Akon-ft-40-Glocc---Borrow-U.mp3" length="243.62" /> 
  <track id="238979" position="11" title="Lil+mama+ft.+t-pain+%26+chris+brown+-+shawty+get+loose+%28official+remix%29" file="11-Lil-Mama-ft-T-Pain-amp-Chris-Brown---Shawty-Get.mp3" length="201.95" /> 
  <track id="238980" position="12" title="Slim+%28of+112%29+ft.+pitbull+-+i+ain%27t+scared" file="12-Slim-of-112-ft-Pitbull---I-Aint-Scared.mp3" length="288.44" /> 
  <track id="238981" position="13" title="Lil+eddie+ft.+defy+-+easier+to+stay" file="13-Lil-Eddie-ft-Defy---Easier-To-Stay.mp3" length="201.72" /> 
  <track id="238982" position="14" title="Uness+-+taste+your+heartbeat" file="14-Uness---Taste-Your-Heartbeat.mp3" length="180.74" /> 
  <track id="238983" position="15" title="Razah+-+club+hobbing" file="15-Razah---Club-Hobbing.mp3" length="219.56" /> 
  <track id="238984" position="16" title="Cheri+dennis+-+flow+wit+me" file="16-Cheri-Dennis---Flow-Wit-Me.mp3" length="170.29" /> 
  <track id="238985" position="17" title="Lisha+ft.+scola+%28of+dru+hill%29+-+i+got+you" file="17-Lisha-ft-Scola-of-Dru-Hill---I-Got-You.mp3" length="197.15" /> 
  <track id="238986" position="18" title="Tiffany+evans+ft.+bow+wow+-+i%27m+grown" file="18-Tiffany-Evans-ft-Bow-Wow---Im-Grown.mp3" length="197.77" /> 
  <track id="238987" position="19" title="Ray+l+-+all+i+want+is+her" file="19-Ray-L---All-I-Want-Is-Her.mp3" length="189.68" /> 
  <track id="238988" position="20" title="Joelle+-+drop+her+off" file="20-Joelle---Drop-Her-Off.mp3" length="176.51" /> 
  <track id="238989" position="21" title="Deepside+ft.+dre+-+feelin+like+a+pimp" file="21-Deepside-ft-Dre---Feelin-Like-A-Pimp.mp3" length="209.95" /> 
  </mixtape>

 

and then this was the php that parsed the titles like you wanted:

<?php
require "parser.php";
$xml = file_get_contents('tape.xml');
$parser = new XMLParser($xml);
$parser->Parse();
foreach($parser->document->tagChildren as $track){
$convert = get_object_vars($track);
echo $convert['tagAttrs']['title'];
echo "<br>";
}
?>

 

Its really pretty easy when you use a parser class. php5 has its own called simpleXML, but I did this up in case you have php4. It still works in php5.

Link to comment
Share on other sites

Thanks alot SkunkBad.. imma try your solution and see which one works faster i used this one that someone suggessted

 

<?php

$data = file_get_contents('data.xml');

$pattern = '#<track(?:.*?)title="(.*?)" file="(.*?)"(?:.*?)?/>#';

if(preg_match_all ($pattern, $data, $matches)){
  $count = count($matches[0])-1;
  for ($x = 0; $x <= $count; $x++) {
    echo '<b>Title:</b> '.$matches[1][$x].' - <b>File:</b> '.$matches[2][$x].'<br/>';
  }
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.