1

I have a String and it gives a XML output , now I want to capture that value. But the problem is there are same variables and that needs to run in a loop.

It's a Shipping method activity tracking, means shiftment step by step process and outputing that

Here is the XML I am getting:

<ArrayOfConsignmentTrack xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
    <ConsignmentTrack>
        <ERROR/>
        <DOCKNO>AB000000002</DOCKNO>
        <TRANSIT_LOCATION>ANDHERI BRANCH OFFICE, MUMBAI</TRANSIT_LOCATION>
        <ACTIVITY>In Transit to</ACTIVITY>
        <EVENTDATE>13 Apr 2015</EVENTDATE>
        <EVENTTIME>18:27:40</EVENTTIME>
        <NEXT_LOCATION>ANDHERI BRANCH OFFICE</NEXT_LOCATION>
        <TRACKING_CODE>T</TRACKING_CODE>
    </ConsignmentTrack>
    <ConsignmentTrack>
        <ERROR/>
        <DOCKNO>AB000000002</DOCKNO>
        <TRANSIT_LOCATION>OKHLA BRANCH, OKHLA</TRANSIT_LOCATION>
        <ACTIVITY>Picked up and Booking processed</ACTIVITY>
        <EVENTDATE>13 Apr 2015</EVENTDATE>
        <EVENTTIME>17:27:53</EVENTTIME>
        <NEXT_LOCATION/>
        <TRACKING_CODE>T</TRACKING_CODE>
    </ConsignmentTrack>
</ArrayOfConsignmentTrack>

Now I want a output like this:

enter image description here

I am using this code to get the value

$myXMLData = file_get_contents($URL);

$xml = (array)simplexml_load_string($myXMLData);
if($xml) {
    $dataArray = (array)$xml['ConsignmentTrack'];
    echo $DOCKNO = $dataArray['DOCKNO'];
    echo $TRANSIT_LOCATION =  $dataArray['TRANSIT_LOCATION'];
    echo $ACTIVITY =  $dataArray['ACTIVITY'];
    echo $EVENTDATE =  $dataArray['EVENTDATE'];
    echo $EVENTTIME =  $dataArray['EVENTTIME'];
    echo $NEXT_LOCATION =  $dataArray['NEXT_LOCATION'];
} else{
    echo "  -  Invalid Docket No.";
}

But it's giving only one value. What loop structure do I have to use?

Rizier123
  • 58,877
  • 16
  • 101
  • 156
Lorry.B
  • 45
  • 1
  • 5
  • Thank You Sajan for Suggested Edit :) – Lorry.B May 01 '15 at 06:23
  • Just in case you still haven't found this article in the PHP manual yet (as it seems), here is a link of a recommended reading: [Basic SimpleXML usage](http://php.net/manual/en/simplexml.examples-basic.php). And please take the resources and information into account given in the reference question I close this question of yours against to spare you the next X questions on how to parse and process XML in PHP (or at least that you don't get so far off like here with all the superfluous array castings you do which likely will cause you further problems). – hakre May 02 '15 at 10:20

1 Answers1

2

This should work for you:

Just load your string with simplexml_load_string(), then you can do: echo $xml->asXML(); to see the structure of the xml, to then see where you have to loop through.

<?php

    $xml = simplexml_load_string($myXMLData);  

    echo "<table border='1'>";
        echo "<tr>";
            echo "<td>Transit Location</td>";
            echo "<td>Activity</td>";
            echo "<td>Event Date</td>";
            echo "<td>Event Time</td>";
            echo "<td>next Location</td>";
        echo "</tr>";

    foreach($xml->ConsignmentTrack as $v) {
        echo "<tr>";
            echo "<td>" . $v->TRANSIT_LOCATION . "</td>";
            echo "<td>" . $v->ACTIVITY . "</td>";
            echo "<td>" . $v->EVENTDATE . "</td>";
            echo "<td>" . $v->EVENTTIME . "</td>";
            echo "<td>" . $v->NEXT_LOCATION . "</td>";
        echo "</tr>";
    }   

    echo "</table>";

?>
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • Hi Rizier Thanks for your code . Is there anyway i could capture the value of $xml without using a file.xml. Actually i don't want to 1st posh the data in a xml file and then run that code. ? Please let me know . Thanks a Lot for your reply – Lorry.B May 01 '15 at 06:36
  • @Lorry.B See my updated code. I changed it to load your xml string variable. Just reload my answer and try my code again – Rizier123 May 01 '15 at 06:37
  • Okay let me try , Thanks a lot for your immediate reply. Much appreciated .. – Lorry.B May 01 '15 at 06:42
  • @Lorry.B So were are we with the question ? Did it worked for you? – Rizier123 May 01 '15 at 07:04
  • it worked , Thanks a lot . I am currently running it . After finish i will get back to you and give accepted ans. – Lorry.B May 01 '15 at 07:47