Jump to content

How to parse XML elements using php?


ballhogjoni

Recommended Posts

I have searched yahoo and google for about a week straight and cannot figure out how to parse specific elements in my xml file. I know how to start a parser, but when I parse my file it prints all the data of all the tags. Can someone show me how to print certain tag elements from a large xml file?

 

EX.

<cardlist>

<card>

  <cid>111</cid>

  <category>Low Interest</category>

</card>

<card>

  <cid>112</cid>

  <category>0% APR</category>

</card>

</cardlist>

 

From the example above I want to only print the <category> tag data for both <card> tags and not the <cid> tags. In my code below it prints everything to the browser.

 

My code

$file = "http://www.ccccccccccccccccc.com/partners/access/xml/xml.asp?uid=cccccccccccccc";

function contents($parser, $data){ 
    echo $data; 
} 

function startTag($parser, $data){ 
    echo "<b>"; 
} 

function endTag($parser, $data){ 
    echo "</b><br />"; 
} 

$xml_parser = xml_parser_create(); 

xml_set_element_handler($xml_parser, "startTag", "endTag"); 

xml_set_character_data_handler($xml_parser, "contents");

if (!($fp = fopen($file, "r"))) {
    die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
    if (!xml_parse($xml_parser, $data, feof($fp))) {
        die(sprintf("XML error: %s at line %d",
                    xml_error_string(xml_get_error_code($xml_parser)),
                    xml_get_current_line_number($xml_parser)));
    }
}
xml_parser_free($xml_parser);

fclose($fp);

Link to comment
https://forums.phpfreaks.com/topic/72190-how-to-parse-xml-elements-using-php/
Share on other sites

i have an easier time with xml if i put it into an array:

 

function xml2ary(&$string) {
    $parser = xml_parser_create();
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parse_into_struct($parser, $string, $vals, $index);
    xml_parser_free($parser);

    $mnary=array();
    $ary=&$mnary;
    foreach ($vals as $r) {
        $t=$r['tag'];
        if ($r['type']=='open') {
            if (isset($ary[$t])) {
                if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array());
                $cv=&$ary[$t][count($ary[$t])-1];
            } else $cv=&$ary[$t];
            if (isset($r['attributes'])) {foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;}
            $cv['_c']=array();
            $cv['_c']['_p']=&$ary;
            $ary=&$cv['_c'];

        } elseif ($r['type']=='complete') {
            if (isset($ary[$t])) { // same as open
                if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array());
                $cv=&$ary[$t][count($ary[$t])-1];
            } else $cv=&$ary[$t];
            if (isset($r['attributes'])) {foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;}
            $cv['_v']=(isset($r['value']) ? $r['value'] : '');

        } elseif ($r['type']=='close') {
            $ary=&$ary['_p'];
        }
    }    
    
    _del_p($mnary);
    return $mnary;
}

// _Internal: Remove recursion in result array
function _del_p(&$ary) {
    foreach ($ary as $k=>$v) {
        if ($k==='_p') unset($ary[$k]);
        elseif (is_array($ary[$k])) _del_p($ary[$k]);
    }
}

function stripHeader($source) {
list($header,$content) = explode("\r\n\r\n",$source,2);
return $content;
}

 

use:

$xmlarray = xml2ary($your_xml_here);

 

if you have php5

<?php
$str = '<cardlist>
<card>
  <cid>111</cid>
  <category>Low Interest</category>
</card>
<card>
  <cid>112</cid>
  <category>0% APR</category>
</card>
</cardlist>';

$xml = simplexml_load_string($str);

foreach ($xml->card as $c)
{
    echo $c->category,  '<br/>';
}
?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.