[Help] XML Parsing with PHP

Started on the old forum in October 2011 · view the original in the archive · old links redirect here

9 replies · 856 views · started 15 years ago · 1 min read

5 participants
djdevil89 Senior · 287 posts #1

Hey again me with a problem lol,

i need to get an xml file with curl and parse it ... okay so far it all works my problem now is i have no idea on how to parse it properly atm it looks like this

Array
(
    [0] => Array
        (
            [name] => user
            [pass] => test
            [priority] => 4
        )

    [1] => Array
        (

            [name] => user
            [pass] => test
            [priority] => 4

        )

    [2] => Array
        (
    

            [name] => user
            [pass] => test
            [priority] => 4
        )

)

Now I dont know how to parse it so i can use the strings seperatly like

echo "$user"; 

etc

why do i need to use xml and curl? sc_trans 2 has an api which outputs results as xml funny thing is it needs to be a POST so i am using curl and theres no other way around it.

---Edit---

          $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://user:pass@domain.com:5555/api');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'op=getstatus&seq=45');
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$xml = curl_exec($ch);
function ExtractString($str, $start, $end)
{
$str_low = strtolower($str);
$pos_start = strpos($str_low, $start);
$pos_end = strpos($str_low, $end, ($pos_start + strlen($start)));
if ( ($pos_start !== false) && ($pos_end !== false) )
{
$pos1 = $pos_start + strlen($start);
$pos2 = $pos_end - $pos1;
return substr($str, $pos1, $pos2);
}
}
$match = ExtractString($xml, '<name>', '</name>');

Thats what i use at the moment to extract the xml and its working with one part of the XML but not multiple like in the array above

foreach ($_SERVER as $server => $value)
{
echo "$server is $value<br />";
}
djdevil89 Senior · 287 posts #3

thanks for the reply but im looking for a PHP solution not .NET

foreach ($_SERVER as $server => $value)
{
echo "$server is $value<br />";
}
djdevil89 Senior · 287 posts #5

i used the link above now my result is

Array
(
    [0] => Array
        (
            [name] => RESPONSE
            [attrs] => Array
                (
                    [SEQ] => 45
                )

            [children] => Array
                (
                    [0] => Array
                        (
                            [name] => DATA
                            [attrs] => Array
                                (
                                )

                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => DJLIST
                                            [attrs] => Array
                                                (
                                                )

                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [name] => DJ
                                                            [attrs] => Array
                                                                (
                                                                )

                                                            [children] => Array
                                                                (
                                                                    [0] => Array
                                                                        (
                                                                            [name] => NAME
                                                                            [attrs] => Array
                                                                                (
                                                                                )

                                                                            [tagData] => matrix
                                                                        )

                                                                    [1] => Array
                                                                        (
                                                                            [name] => PASSWORD
                                                                            [attrs] => Array
                                                                                (
                                                                                )

                                                                            [tagData] => xxxxxx
                                                                        )

                                                                    [2] => Array
                                                                        (
                                                                            [name] => PRIORITY
                                                                            [attrs] => Array
                                                                                (
                                                                                )

                                                                            [tagData] => 4
                                                                        )

                                                                    [3] => Array
                                                                        (
                                                                            [name] => ENABLED
                                                                            [attrs] => Array
                                                                                (
                                                                                )

                                                                            [tagData] => 1
                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

the parts i am interested in are

[tagData] => matrix

and

[tagData] => xxxxx

i cant get it to to list properly heres my code (yes i know it says print_r thats because right now its the only way to give me any data at all)

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://xxxx:xxxx@domain.com:5555/api');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'op=listdjs&seq=45');
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$xml = curl_exec($ch);
class xml2Array {
     
     var $arrOutput = array();
     var $resParser;
     var $strXmlData;
     
     function parse($strInputXML) {
     
             $this->resParser = xml_parser_create ();
             xml_set_object($this->resParser,$this);
             xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
             
             xml_set_character_data_handler($this->resParser, "tagData");
         
             $this->strXmlData = xml_parse($this->resParser,$strInputXML );
             if(!$this->strXmlData) {
                die(sprintf("XML error: %s at line %d",
             xml_error_string(xml_get_error_code($this->resParser)),
             xml_get_current_line_number($this->resParser)));
             }
                             
             xml_parser_free($this->resParser);
             
             return $this->arrOutput;
     }
     function tagOpen($parser, $name, $attrs) {
        $tag=array("name"=>$name,"attrs"=>$attrs); 
        array_push($this->arrOutput,$tag);
     }
     
     function tagData($parser, $tagData) {
        if(trim($tagData)) {
             if(isset($this->arrOutput[count($this->arrOutput)-1]['tagData'])) {
                 $this->arrOutput[count($this->arrOutput)-1]['tagData'] .= $tagData;
             } 
             else {
                 $this->arrOutput[count($this->arrOutput)-1]['tagData'] = $tagData;
             }
        }
     }
     
     function tagClosed($parser, $name) {
        $this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1];
        array_pop($this->arrOutput);
     }
 }
  $objXML = new xml2Array();
  $arrOutput = $objXML->parse($xml);
 ?>
 <pre><?
  print_r($arrOutput); //print it out, or do whatever!

 ?>
foreach ($_SERVER as $server => $value)
{
echo "$server is $value<br />";
}
StRiNg_MaStEr Developer · 93 posts #6

why u cant use SimplXML

$xml = simplexml_load_file("test.xml");

echo $xml->getName() . " ";

foreach($xml->children() as $child) { echo $child->getName() . ": " . $child . " "; }

GumSlone wrote: try this examples: PHP: xml_parse - Manual

will you please check and reply to my pm

djdevil89 Senior · 287 posts #7

simple xml aint working now when i do a echo $xml; it lists me all required data but since i need to put some entry before it i need to

$name = ExtractString($xmlres, '<name>', '</name>'); 
$password = ExtractString($xmlres, '<password>', '</password>');

this is my code now

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://xxxx:xxxx@domain.com:5555/api');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'op=listdjs&seq=45');
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$xmlres = curl_exec($ch);
?>
<pre>
<?
function ExtractString($str, $start, $end)
{
$str_low = strtolower($str);
$pos_start = strpos($str_low, $start);
$pos_end = strpos($str_low, $end, ($pos_start + strlen($start)));
if ( ($pos_start !== false) && ($pos_end !== false) )
{                                                                                                     
$pos1 = $pos_start + strlen($start);                                                                   
$pos2 = $pos_end - $pos1;
return substr($str, $pos1, $pos2);
}
}
$name = ExtractString($xmlres, '<name>', '</name>'); 
$password = ExtractString($xmlres, '<password>', '</password>'); 
?>
<table style="border-collapse:collapse;width:100%;">
  <tbody>
    <tr>
      <td style="border:#000000 1px solid">DJ Name<br />
        </td>
      <td style="border:#000000 1px solid">DJ Pass<br />
        </td>
    </tr>
  </tbody>
</table>
<table style="border-collapse:collapse;width:100%;">
  <tbody>
    <tr>
      <td style="border:#000000 1px solid"><?
      print_r($name);?><br />
        </td>
<td style="border:#000000 1px solid"><?
      print_r($password);?><br />
        </td>
    </tr>
  </tbody>
</table>
<? 

But it only gives me one result while i know there are 2 -.-

foreach ($_SERVER as $server => $value)
{
echo "$server is $value<br />";
}
Mysterio Lead · 630 posts #8

Lets say your array is stored in a variable called $array

Now just do:

foreach($array as $a) { echo 'name: '.$a['name'].' '; echo 'pass: '.$a['pass'].' '; echo 'priority: '.$a['priority'].' '; }

mysterio.al - programming is a functional art

djdevil89 Senior · 287 posts #9

Thanks mysterio but that aint working either bro, you got the script maybe youll find a solution i keep trying and trying but no result -.-

foreach ($_SERVER as $server => $value)
{
echo "$server is $value<br />";
}
arnage Lead · 811 posts #10

So u only want the content between <name></name> and <password></password>? Have u take into consideration to preg_match them and take matched values? Something like:

function get_it($from) {
$match = preg_match_all('/(<name|password>)(\w.*)(<.*>)/ismU', $from, $matched);
return $matched[2]; // return $matched[1] or $matched[3] for tags
}

Just separate tag in the pattern if necessary i just wrote both of for example in one. 🙂

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close