Jump to content

Need Help!


total noob

Recommended Posts

Hello all

 

I have here a code for parsing an XML string

<?php
function xml2array($contents, $get_attributes=1, $priority = 'tag') {
    if(!$contents) return array();

    if(!function_exists('xml_parser_create')) {

        return array();
    }

    $parser = xml_parser_create('');
    xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); 
    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, trim($contents), $xml_values);
    xml_parser_free($parser);

    if(!$xml_values) return;


    $xml_array = array();
    $parents = array();
    $opened_tags = array();
    $arr = array();

    $current = &$xml_array;

    $repeated_tag_index = array();
    foreach($xml_values as $data) {
        unset($attributes,$value);

        
        extract($data);

        $result = array();
        $attributes_data = array();
        
        if(isset($value)) {
            if($priority == 'tag') $result = $value;
            else $result['value'] = $value; 
        }

        if(isset($attributes) and $get_attributes) {
            foreach($attributes as $attr => $val) {
                if($priority == 'tag') $attributes_data[$attr] = $val;
                else $result['attr'][$attr] = $val; 
            }
        }

        if($type == "open") {
            $parent[$level-1] = &$current;
            if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
                $current[$tag] = $result;
                if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
                $repeated_tag_index[$tag.'_'.$level] = 1;

                $current = &$current[$tag];

            } else {

                if(isset($current[$tag][0])) {
                    $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
                    $repeated_tag_index[$tag.'_'.$level]++;
                } else {
                    $current[$tag] = array($current[$tag],$result);
                    $repeated_tag_index[$tag.'_'.$level] = 2;
                    
                    if(isset($current[$tag.'_attr'])) {
                        $current[$tag]['0_attr'] = $current[$tag.'_attr'];
                        unset($current[$tag.'_attr']);
                    }

                }
                $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
                $current = &$current[$tag][$last_item_index];
            }

        } elseif($type == "complete") {
            if(!isset($current[$tag])) { 
                $current[$tag] = $result;
                $repeated_tag_index[$tag.'_'.$level] = 1;
                if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;

            } else {
                if(isset($current[$tag][0]) and is_array($current[$tag])) {

                    $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
                    
                    if($priority == 'tag' and $get_attributes and $attributes_data) {
                        $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
                    }
                    $repeated_tag_index[$tag.'_'.$level]++;

                } else {
                    $current[$tag] = array($current[$tag],$result); 
                    $repeated_tag_index[$tag.'_'.$level] = 1;
                    if($priority == 'tag' and $get_attributes) {
                        if(isset($current[$tag.'_attr'])) {
                            
                            $current[$tag]['0_attr'] = $current[$tag.'_attr'];
                            unset($current[$tag.'_attr']);
                        }
                        
                        if($attributes_data) {
                            $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
                        }
                    }
                    $repeated_tag_index[$tag.'_'.$level]++; 
                }
            }

        } elseif($type == 'close') {
            $current = &$parent[$level-1];
        }
    }
    
    return($xml_array);
}  

/*
$XMLS = '<?xml  version="1.0"  encoding="UTF-8"?><GetSMSInboundResponse><Transaction><Code>1</Code><Description>Transaction  OK</Description></Transaction><SMSInbounds><InboundSMS><ID>3135765</ID><Originator>+447537404702</Originator><Destination>+447537404702</Destination><Keyword>UCB2</Keyword><Date>2012-01-25</Date><Time>12:09:08</Time><Body>UCB2  this  is  a  test  "sms",  with  commas..  Test  onlt  for  UCB  Function</Body></InboundSMS></SMSInbounds></GetSMSInboundResponse>';
*/

$XMLS = '	
<?xml  version="1.0"  encoding="UTF-8"?><GetSMSInboundResponse><Transaction><Code>1</Code><Description>Transaction  OK</Description></Transaction><SMSInbounds><InboundSMS><ID>3135899</ID><Originator>+447537404702</Originator><Destination>+447537404702</Destination><Keyword>UCB2</Keyword><Date>2012-01-25</Date><Time>12:20:24</Time><Body>UCB2  test  "SMS",  with  comma  and  qoutes  for  UCB..</Body></InboundSMS><InboundSMS><ID>3135903</ID><Originator>+447537404702</Originator><Destination>+447537404702</Destination><Keyword>UCB2</Keyword><Date>2012-01-25</Date><Time>12:20:24</Time><Body>UCB2  test  "SMS",  with  comma  and  qoutes  for  UCB..  the  second  message...</Body></InboundSMS></SMSInbounds></GetSMSInboundResponse> ';

$myAray = xml2array($XMLS, $get_attributes=1, $priority = 'tag');
$innerArray = $myAray['GetSMSInboundResponse']['SMSInbounds']['InboundSMS'];
foreach ($innerArray as $item) {
echo $item['ID'] . '<br/>' . $item['Originator'] . '<br/>' . $item['Destination'] . '<br/>' . $item['Keyword'] . '<br/>' . $item['Date']. '<br/>' . $item['Time']. '<br/>' . $item['Body'] . '<hr/>';//. PHP_EOL;
}
?>

 

 

And i have to kinds of XML

 

First XML

$XMLS = '<?xml  version="1.0"  encoding="UTF-8"?><GetSMSInboundResponse><Transaction><Code>1</Code><Description>Transaction  OK</Description></Transaction><SMSInbounds><InboundSMS><ID>3135765</ID><Originator>+447537404702</Originator><Destination>+447537404702</Destination><Keyword>UCB2</Keyword><Date>2012-01-25</Date><Time>12:09:08</Time><Body>UCB2  this  is  a  test  "sms",  with  commas..  Test  onlt  for  UCB  Function</Body></InboundSMS></SMSInbounds></GetSMSInboundResponse>';

Result of the first XML

3

3

3

3

3

3

3+

+

+

+

+

+

++

+

+

+

+

+

+U

U

U

U

U

U

U2

2

2

2

2

2

21

1

1

1

1

1

1U

U

U

U

U

U

U

 

Second XML

$XMLS = '

<?xml  version="1.0"  encoding="UTF-8"?><GetSMSInboundResponse><Transaction><Code>1</Code><Description>Transaction  OK</Description></Transaction><SMSInbounds><InboundSMS><ID>3135899</ID><Originator>+447537404702</Originator><Destination>+447537404702</Destination><Keyword>UCB2</Keyword><Date>2012-01-25</Date><Time>12:20:24</Time><Body>UCB2  test  "SMS",  with  comma  and  qoutes  for  UCB..</Body></InboundSMS><InboundSMS><ID>3135903</ID><Originator>+447537404702</Originator><Destination>+447537404702</Destination><Keyword>UCB2</Keyword><Date>2012-01-25</Date><Time>12:20:24</Time><Body>UCB2  test  "SMS",  with  comma  and  qoutes  for  UCB..  the  second  message...</Body></InboundSMS></SMSInbounds></GetSMSInboundResponse> ';

 

Result for the second XML

3135899

+447537404702

+447537404702

UCB2

2012-01-25

12:20:24

UCB2 test "SMS", with comma and qoutes for UCB..3135903

+447537404702

+447537404702

UCB2

2012-01-25

12:20:24

UCB2 test "SMS", with comma and qoutes for UCB.. the second message...

 

Now my problem is that when i use the second XML string it would echo the results just fine but when i use the first XML string i would echo weird results... Can someone help me with my problem? i think the problem is on how i echo the results but still i can't fix this problem.. Please help me guys..

 

thanks

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.