Page 1 of 1

Please help having trouble parsing xml

Posted: Fri May 16, 2008 9:13 am
by evin1207
Im having some issues parsing some xml data i know my xml parsing script works when i run it on its own but when i combine it with my script that reads the data nothing gets parsed it just spits out the entire xml data to a file

i just want one the data thats in between the message feild

Code: Select all

 
<?php
$xml = "";
$putdata = fopen( "php://input" , "rb" );
$dest_xml='/var/www/webupload/scribe'.date("H:i:s").".xml";
$file = fopen($dest_xml, "a", 0);
while(!feof( $putdata )) {
$_data = fread($putdata, 4096);
$_data = urldecode($_data);
$xml = $xml.$_data;
}
$xml = str_replace("response=","",$xml);
$current = "";
function start_tag($parser, $name, $attribs) {
        global $current;
        $current = $name;
        if ($name == "MESSAGE") { }
        if (is_array($attribs)) {
                while(list($key,$val) = each($attribs)) {
                        echo strtolower($key)."=\"".$val."\"";
                        }
                }
}
 
function end_tag($parser, $name) {
        if ($name == "message") { }
} 
function tag_contents($parser, $xml) {
        global $current;
        if ($current == "message") { echo $xml; }
} 
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "start_tag", "end_tag");
xml_set_character_data_handler($xml_parser, "tag_contents");
if(!(xml_parse($xml_parser, $xml, TRUE))){
        die("Error on line " . xml_get_current_line_number($xml_parser));
        }
xml_parser_free($xml_parser);
fwrite($file, $xml);
fclose($putdata);
fclose($file);
?>
 

my outupt is this the only thing that is working properly is the str_replace at this point

Code: Select all

 
 
<response><external-xml></external-xml><external-id>789456123</external-id><audio-duration>15</audio-duration><trans-xml><message><![CDATA[This is a (<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>?). Test 1, 2. Test 1, 2, 3.  1, 2, 3, 4, 5, 6, 7, 8, 9, 10. Code hear communications.]]></message><result>transcribed</result></trans-xml><metadata/><truncated>false</truncated><error-code>0</error-code><ss-id>260141855</ss-id></response>
 
 
If anybody can help me id really appreciate it ive been on this for about a week with no avail. :banghead:

Re: Please help having trouble parsing xml

Posted: Fri May 16, 2008 11:54 am
by JacobT
hey evin, I'm not even going to try to fix your parser, but here's the xml2Array script that I use (I didn't write it) and it works wonderfully. Run your XML through it and dump the results to see how the output format is setup. Let me know if this helps!

Code: Select all

    function xml2arrayNew($contents, $get_attributes=1) {
        if(!$contents) return array();
    
        if(!function_exists('xml_parser_create')) {
            //print "'xml_parser_create()' function not found!";
            return array();
        }
        //Get the XML parser of PHP - PHP must have this module for the parser to work
        $parser = xml_parser_create();
        xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
        xml_parser_set_option( $parser, XML_OPTION_SKIP_WHITE, 1 );
        xml_parse_into_struct( $parser, $contents, $xml_values );
        xml_parser_free( $parser );
    
        if(!$xml_values) return;//Hmm...
    
        //Initializations
        $xml_array = array();
        $parents = array();
        $opened_tags = array();
        $arr = array();
    
        $current = &$xml_array;
    
        //Go through the tags.
        foreach($xml_values as $data) {
            unset($attributes,$value);//Remove existing values, or there will be trouble
            extract($data);//We could use the array by itself, but this cooler.
    
            $result = '';
            if($get_attributes) {//The second argument of the function decides this.
                $result = array();
                if(isset($value)) $result['value'] = $value;
    
                //Set the attributes too.
                if(isset($attributes)) {
                    foreach($attributes as $attr => $val) {
                        if($get_attributes == 1) $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
                        /**  :TODO: should we change the key name to '_attr'? Someone may use the tagname 'attr'. Same goes for 'value' too */
                    }
                }
            } elseif(isset($value)) {
                $result = $value;
            }
    
            //See tag status and do the needed.
            if($type == "open") {//The starting of the tag '<tag>'
                $parent[$level-1] = &$current;
    
                if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
                    $current[$tag] = $result;
                    $current = &$current[$tag];
    
                } else { //There was another element with the same tag name
                    if(isset($current[$tag][0])) {
                        array_push($current[$tag], $result);
                    } else {
                        $current[$tag] = array($current[$tag],$result);
                    }
                    $last = count($current[$tag]) - 1;
                    $current = &$current[$tag][$last];
                }
    
            } elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
                //See if the key is already taken.
                if(!isset($current[$tag])) { //New Key
                    $current[$tag] = $result;
    
                } else { //If taken, put all things inside a list(array)
                    if((is_array($current[$tag]) and $get_attributes == 0)//If it is already an array...
                            or (isset($current[$tag][0]) and is_array($current[$tag][0]) and $get_attributes == 1)) {
                        array_push($current[$tag],$result); // ...push the new element into that array.
                    } else { //If it is not an array...
                        $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
                    }
                }
    
            } elseif($type == 'close') { //End of tag '</tag>'
                $current = &$parent[$level-1];
            }
        }
    
        return($xml_array);
    }

Re: Please help having trouble parsing xml

Posted: Sun May 18, 2008 10:36 am
by sim-and-sim
someone might probably kill me for suggesting this.. but.. why not use preg_match_all..? I used it on the old YouTube API.... worked like a charm..