Element atrributes in case statement - php to xml

Hi,

I got a xml file. I need to write a php script to parse it and display in a webpage. I wrote a below code for the xml (below). I able to get the output as mentioned. but the element attribute under maesinfo, granperiod is not getting displayed. Can you please help me. .I am in learning phase of php and trying to do my best to have a php script for users.

<?php

echo "Details of the XML file";
//Initialize the XML parser
$parser=xml_parser_create();

//Function to use at the end of an element

//Function to use at the start of an element
function start($parser,$element_name,$element_attrs)
  {
  switch($element_name)
    {
    case "MEASINFO":
    echo "MEASINFO: <br>";
    break;
    case "GRANPERIOD":
    echo "GRANPERIOD:<br>";
    break;
    case "GRANPERIOD":
    echo "GRANPERIOD: <br>";
    break;
    case "GRANPERIOD-DURATION":
    echo "GRANPERIOD-DURATION: <br>";
    break;
    case "GRANPERIOD-ENDTIME":
    echo "GRANPERIOD-ENDTIME: <br> ";
	break;
	case "REPPERIOD":
	echo "REPPERIOD: <br>";
	break;
	case "MEASTYPES":
	echo "MEASTYPES: <br>";
	break;
	case "MEASVALUE":
	echo "MEASVALUE: ";
	break;
	case "MEASVALUE-MEASOBJLDN":
	echo "MEASVALUE-MEASOBJLDN: <br>";
	
	break;
	case "MEASVALUE/MEASRESULTS":
	echo "MEASVALUE/MEASRESULTS: <br>";
    }
  }


function stop($parser,$element_name)
  {
  echo "<br>";
  }

//Function to use when finding character data
function char($parser,$data)
  {
  echo $data;
  }

//Specify element handler
xml_set_element_handler($parser,"start","stop");

//Specify data handler
xml_set_character_data_handler($parser,"char");

//Open XML file
$fp=fopen("record1.xml","r");

//Read data
while ($data=fread($fp,4096))
  {
  xml_parse($parser,$data,feof($fp)) or
  die (sprintf("XML Error: %s at line %d",
  xml_error_string(xml_get_error_code($parser)),
  xml_get_current_line_number($parser)));
  }

//Free the XML parser
xml_parser_free($parser);
?>
<?xml version="1.0" encoding="UTF-8"?>
<measCollecFile xmlns="http://latest/nmc-omc/cmNrm.doc#measCollec" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://latest/nmc-omc/cmNrm.doc#measCollec schema\\pmResultSchedule.xsd">
    <fileHeader fileFormatVersion="32.435 V7.2" vendorName="Huawei">
        <fileSender elementType="USN"/>
        <measCollec beginTime="2013-12-01T00:00:00+04:00"/>
    </fileHeader>
    <measData>
        <managedElement userLabel="USN_AR"/>
 <measInfo measInfoId="11854767">
            <granPeriod duration="PT3600S" endTime="2013-12-01T01:00:00+04:00"/>
            <repPeriod duration="PT3600S"/>
            <measTypes>MME-init PDN disconnection request Times;MME-init PDN disconnection success Times;PDN disconnect fail Times(#35 PTI already in use);PDN disconnect fail Times(#43 Unknown EPS bearer ctx);PDN disconnect fail Times(#49 Last PDN disconn not allowed);PDN disconnect fail Times(#95-111 Protocol err, unspec);PDN disconnect fail Times(#96 Invalid mandatory information);PDN disconnect request Times;PDN disconnect success Times;</measTypes>
            <measValue measObjLdn="USN_AR/Whole System:USN_AR">
                <measResults>0 0 0 0 0 0 0 0 0 </measResults>
            </measValue>
        </measInfo>
        <measInfo measInfoId="117440618">
            <granPeriod duration="PT3600S" endTime="2013-12-01T01:00:00+04:00"/>
            <repPeriod duration="PT3600S"/>
            <measTypes>Components sent by the node;Componets received by the node;Reject component received by the node;Reject component sent by the node;Return error component received by the node;Return error component sent by the node;Return result received ultilizations(not last);Return result ultilizations received by the node;Return result ultilizations sent by the node;Return result ultilizations sent(not last);</measTypes>
            <measValue measObjLdn="USN_AR/Whole System:USN_AR">
                <measResults>23839 2067 0 0 3235 1 0 19163 2065 0 </measResults>
            </measValue>
        </measInfo>
        <measInfo measInfoId="117440600">
            <granPeriod duration="PT3600S" endTime="2013-12-01T01:00:00+04:00"/>
            <repPeriod duration="PT3600S"/>
            <measTypes>Iu mode Foreign subscribers act PDP context accept;Iu mode Foreign subscribers act PDP context reject;Iu mode Foreign subscribers init PDP context act;Iu mode MS init IPv4 PDP ctx act;Iu mode MS init IPv4 PDP ctx act reject;Iu mode MS init IPv4 PDP ctx act success;Iu mode MS init IPv6 PDP ctx act;Iu mode MS init IPv6 PDP ctx act reject;Iu mode MS init IPv6 PDP ctx act success;Iu mode MS init PDP context Max act time delay;Iu mode MS init PDP context Mean act time delay;Iu mode MS init PDP context act;Iu mode MS init PDP context act reject;Iu mode MS init PDP context act reject with dynamic addr;Iu mode MS init PDP context act success;Iu mode MS init PDP context act success rate;Iu mode MS init PDP context act success with dynamic addr;Iu mode MS init PDP context act with dynamic addr;Iu mode MS init PDP context sec act success;Iu mode Ms PDP Context Total act time delay;Iu mode Ms PDP Context act complete Total times;Iu mode NET PDP context act fail;Iu mode NET PDP context act fail ((insufficient net resources));Iu mode NET PDP context act fail (DNS resolution fail);Iu mode NET PDP context act fail (Operator Determined Barring);Iu mode NET PDP context act fail (QoS negotiation fail);Iu mode NET PDP context act fail (missing or unknown APN);Iu mode NET PDP context act fail (rejected by GGSN);Iu mode NET PDP context act fail (service not supported);Iu mode NET PDP context act fail (service unsubscribed);Iu mode NET PDP context act fail (unknown PDP addr or type);Iu mode NET PDP context act fail (user authentication fail);Iu mode NET PDP context act fail(APN Restriction);Iu mode NET PDP context act fail(Ge fail);Iu mode NET PDP context act fail(RAB assignment fail);Iu mode NET PDP context act fail(Unspecified Reason);Iu mode Network init IPv4 PDP ctx act;Iu mode Network init IPv4 PDP ctx act success;Iu mode Network init IPv6 PDP ctx act;Iu mode Network init IPv6 PDP ctx act success;Iu mode Network init PDP context act;Iu mode Network init PDP context act success;Iu mode Network init sec PDP context act;Iu mode Network init sec PDP context act success;Iu mode PDP context act fail (DNS resolution fail);Iu mode PDP context act fail (QoS negotiation fail);Iu mode PDP context act fail (RAB assignment fail);Iu mode PDP context act fail (rejected by GGSN);Iu mode PDP context act fail(APN Restriction);Iu mode PDP context act fail(Ge fail);Iu mode PDP context act fail(insufficient net resources);Iu mode PDP context act fail(missing or unknown APN);Iu mode PDP context act fail(service not supported);Iu mode PDP context act fail(service unsubscribed);Iu mode PDP context act fail(unknown PDP addr or type);Iu mode PDP context act fail(unspecific reason);Iu mode PDP context act fail(user authentication fail);Iu mode PDP context act request times per subscriber @BH;Iu mode PDP context sec act;Iu mode PDP context sec act fail(Ge fail);Iu mode PDP context sec act fail(Unspecified Reason);Iu mode PDP context sec act fail(net resource insufficient);Iu mode PDP context sec act fail(rejected by GGSN);Iu mode PDP context sec act fail(unknown PDP context);Iu mode PDP context sec act reject;Iu mode PDP ctx act fail (Operator Determined Barring);Iu mode PDP ctx sec act fail (Operator Determined Barring);Iu mode active One Tunnel PDP Success;Iu mode active One Tunnel PDP fail;Iu mode active One Tunnel PDP reject(CAMEL);Iu mode active One Tunnel PDP reject(GTP Versions);Iu mode active Two Tunnel PDP Success;Iu mode active Two Tunnel PDP fail;Iu mode maximum MS init PDP context act;Iu mode secondary IPv4 PDP ctx act;Iu mode secondary IPv4 PDP ctx act success;Iu mode secondary IPv6 PDP ctx act;Iu mode secondary IPv6 PDP ctx act success;</measTypes>
            <measValue measObjLdn="USN_AR/Whole System:USN_AR">
                <measResults>21 72 93 13476 5492 7984 5 0 5 9540 783 13596 5568 5567 8028 59.047 8027 13594 0 6288830 8028 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 3 0 3427 41 0 0 162 0 0 452 0 1 1482 0.472 0 0 0 0 0 0 0 0 0 0 0 0 0 8028 4935 59 0 0 0 0 </measResults>
            </measValue>
        </measInfo>
    </measData>
    <fileFooter>
        <measCollec endTime="2013-12-01T01:00:00+04:00"/>
    </fileFooter>
</measCollecFile>

Output of the above code
[COLOR=“#008000”]
Details of the XML file

MEASINFO:
GRANPERIOD:

REPPERIOD:

MEASTYPES:
MME-init PDN disconnection request Times;MME-init PDN disconnection success Times;PDN disconnect fail Times(#35 PTI already in use);PDN disconnect fail Times(#43 Unknown EPS bearer ctx);PDN disconnect fail Times(#49 Last PDN disconn not allowed);PDN disconnect fail Times(#95-111 Protocol err, unspec);PDN disconnect fail Times(#96 Invalid mandatory information);PDN disconnect request Times;PDN disconnect success Times;
MEASVALUE: 0 0 0 0 0 0 0 0 0

MEASINFO:
GRANPERIOD:

REPPERIOD:

MEASTYPES:
Components sent by the node;Componets received by the node;Reject component received by the node;Reject component sent by the node;Return error component received by the node;Return error component sent by the node;Return result received ultilizations(not last);Return result ultilizations received by the node;Return result ultilizations sent by the node;Return result ultilizations sent(not last);
MEASVALUE: 23839 2067 0 0 3235 1 0 19163 2065 0

MEASINFO:
GRANPERIOD:

REPPERIOD:

MEASTYPES:
Iu mode Foreign subscribers act PDP context accept;Iu mode Foreign subscribers act PDP context reject;Iu mode Foreign subscribers init PDP context act;Iu mode MS init IPv4 PDP ctx act;Iu mode MS init IPv4 PDP ctx act reject;Iu mode MS init IPv4 PDP ctx act success;Iu mode MS init IPv6 PDP ctx act;Iu mode MS init IPv6 PDP ctx act reject;Iu mode MS init IPv6 PDP ctx act success;Iu mode MS init PDP context Max act time delay;Iu mode MS init PDP context Mean act time delay;Iu mode MS init PDP context act;Iu mode MS init PDP context act reject;Iu mode MS init PDP context act reject with dynamic addr;Iu mode MS init PDP context act success;Iu mode MS init PDP context act success rate;Iu mode MS init PDP context act success with dynamic addr;Iu mode MS init PDP context act with dynamic addr;Iu mode MS init PDP context sec act success;Iu mode Ms PDP Context Total act time delay;Iu mode Ms PDP Context act complete Total times;Iu mode NET PDP context act fail;Iu mode NET PDP context act fail ((insufficient net resources));Iu mode NET PDP context act fail (DNS resolution fail);Iu mode NET PDP context act fail (Operator Determined Barring);Iu mode NET PDP context act fail (QoS negotiation fail);Iu mode NET PDP context act fail (missing or unknown APN);Iu mode NET PDP context act fail (rejected by GGSN);Iu mode NET PDP context act fail (service not supported);Iu mode NET PDP context act fail (service unsubscribed);Iu mode NET PDP context act fail (unknown PDP addr or type);Iu mode NET PDP context act fail (user authentication fail);Iu mode NET PDP context act fail(APN Restriction);Iu mode NET PDP context act fail(Ge fail);Iu mode NET PDP context act fail(RAB assignment fail);Iu mode NET PDP context act fail(Unspecified Reason);Iu mode Network init IPv4 PDP ctx act;Iu mode Network init IPv4 PDP ctx act success;Iu mode Network init IPv6 PDP ctx act;Iu mode Network init IPv6 PDP ctx act success;Iu mode Network init PDP context act;Iu mode Network init PDP context act success;Iu mode Network init sec PDP context act;Iu mode Network init sec PDP context act success;Iu mode PDP context act fail (DNS resolution fail);Iu mode PDP context act fail (QoS negotiation fail);Iu mode PDP context act fail (RAB assignment fail);Iu mode PDP context act fail (rejected by GGSN);Iu mode PDP context act fail(APN Restriction);Iu mode PDP context act fail(Ge fail);Iu mode PDP context act fail(insufficient net resources);Iu mode PDP context act fail(missing or unknown APN);Iu mode PDP context act fail(service not supported);Iu mode PDP context act fail(service unsubscribed);Iu mode PDP context act fail(unknown PDP addr or type);Iu mode PDP context act fail(unspecific reason);Iu mode PDP context act fail(user authentication fail);Iu mode PDP context act request times per subscriber @BH;Iu mode PDP context sec act;Iu mode PDP context sec act fail(Ge fail);Iu mode PDP context sec act fail(Unspecified Reason);Iu mode PDP context sec act fail(net resource insufficient);Iu mode PDP context sec act fail(rejected by GGSN);Iu mode PDP context sec act fail(unknown PDP context);Iu mode PDP context sec act reject;Iu mode PDP ctx act fail (Operator Determined Barring);Iu mode PDP ctx sec act fail (Operator Determined Barring);Iu mode active One Tunnel PDP Success;Iu mode active One Tunnel PDP fail;Iu mode active One Tunnel PDP reject(CAMEL);Iu mode active One Tunnel PDP reject(GTP Versions);Iu mode active Two Tunnel PDP Success;Iu mode active Two Tunnel PDP fail;Iu mode maximum MS init PDP context act;Iu mode secondary IPv4 PDP ctx act;Iu mode secondary IPv4 PDP ctx act success;Iu mode secondary IPv6 PDP ctx act;Iu mode secondary IPv6 PDP ctx act success;
MEASVALUE: 21 72 93 13476 5492 7984 5 0 5 9540 783 13596 5568 5567 8028 59.047 8027 13594 0 6288830 8028 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 3 0 3427 41 0 0 162 0 0 452 0 1 1482 0.472 0 0 0 0 0 0 0 0 0 0 0 0 0 8028 4935 59 0 0 0 0

[/COLOR]

I think the issue is that the code doesn’t do anything at all with the element attributes, only the data, and the elements in question have no data, only attributes. So to extract that information, you need to look in the $element_attrs array in your start() function - $element_attrs[DURATION] and $element_attrs[ENDTIME] contain the data you are looking for. To see what I mean, add


print_r($element_attrs);

just before the switch() statement in your start routine. No doubt there’s a neater way to do this.

I prefer using a simpleXML parse myself…


$xml = new SimpleXMLElement($xmlstring);

foreach($xml->measData->children() AS $info) {
 rabbithole($info);
}

function rabbithole($child) {
  echo $child->getName().": <br />";
  foreach($child->attributes() AS $key => $value) {
   echo $key." => ".$value."<br />";
  }
  $var = (string) $child;
  if(!empty($var)) {
    echo 'Contents: '.$var.'<br />';
  }
  echo "<br />";
  foreach($child->children() AS $subchild) {
    rabbithole($subchild);
  }
  echo "<br />";
}

(Yes, i know, cardinal sin to echo inside a function. But mreh.)