Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
XML parsing with php : I'm lost...
Old 07-08-2005, 07:10 AM XML parsing with php : I'm lost...
Junior Talker

Posts: 4
Trades: 0
Hi,

I've read a lot of pages on parsing xml with php, but I really can't manage it!
Ca someone help me, I've just a simple file to parse :

Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<ListeArtiste>
	<nb_reponse>337</nb_reponse>
	<Artiste id='7732'>Abba</Artiste>
	<Artiste id='7509'>Ace Of Base</Artiste>
</ListeArtiste>
How can I extract "Artist id" and Artiste name?...
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Blue-directory is offline
Reply With Quote
View Public Profile Visit Blue-directory's homepage!
 
 
Register now for full access!
Old 07-08-2005, 09:18 AM
Novice Talker

Posts: 8
Trades: 0
Well, it's very easy

You can either do it the old fashion way or using xml functions.

The way I do it is I perforn a loop and detect when the string <Artiste begins and the </Artiste> finishes

This is very easy to do. There are pros and cons about this, the pros is that php doesn't have to have xml extensions installed and some hosts don't so even if hosts don't have xml extension it will work, the draw back is that it's more slow than if you use the xml functions

The other way is for you to use xml function

PHP Code:
<?php
  
class XMLParser {
    var 
$depth 0;
    var 
$parser;
    var 
$text;
    var 
$cdata = array();

    function 
XMLParser() {
      
$this->parser xml_parser_create();
      
xml_set_object($this->parser$this);
      
xml_parser_set_option($this->parserXML_OPTION_CASE_FOLDING0);
      
xml_set_element_handler($this->parser'startElement''endElement');
      
xml_set_character_data_handler($this->parser'characterData');
    }

    function 
parse($xml) {
      
$error xml_parse($this->parser,$xml,true);
      if (
$error === false) {
        die(
sprintf("XML error: %s at line %d",
        
xml_error_string(xml_get_error_code($this->parser)),
        
xml_get_current_line_number($this->parser)));
      }
      
$this->text '$arr = '.substr($this->text0,
    
strlen($this->text)-2).";\n";
      eval(
$this->text);
      return 
$arr;
    }

    function 
startElement($parser$name$attrs) {
      
$attrStr '';
      
$this->depth++;
      
$this->cdata[$this->depth] = '';
      foreach(
$attrs as $key => $value) {
        
$attrStr .= "'$key' => '$value', ";
      }
      
$this->text .= "array(\n'name' => '$name', 'attributes' => array($attrStr), ";
    }

    function 
endElement($parser$name) {
      
$this->text .= " 'cdata' => '".$this->cdata[$this->depth]."'),\n";
      
$this->depth--;
    }

    function 
characterData($parser$data) {
      
$this->cdata[$this->depth] .= $data;
    }
  }

  
$parser = new XMLParser();
  
$arr $parser->parse(file_get_contents('sample.xml'));
  
print_r($arr);
?>
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
andrade is offline
Reply With Quote
View Public Profile
 
Old 07-08-2005, 09:43 AM
Junior Talker

Posts: 4
Trades: 0
Thank you.
But I give up the project, there are too much problems in addition. Sorry and thank you for your answer...
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Blue-directory is offline
Reply With Quote
View Public Profile Visit Blue-directory's homepage!
 
Old 07-11-2005, 01:05 PM
Novice Talker

Posts: 8
Trades: 0
Not a problem
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
andrade is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to XML parsing with php : I'm lost...
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.38533 seconds with 12 queries