Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing XML file with Laravel

Ok, so I am pretty stuck on this. I have a semi-complex XML file which looks a bit like this:

<?xml version="1.0" encoding="UTF-8"?>

<community ID="1234" POSTDATE="20180329" xmlns="" COMPANY="SAMPLE CO">
   <data1>
      <datatype ID="0001">
         <utpricing LT="1" STARTDATE="20150102" ENDDATE="20160102" BASEAMOUNT="99" CONCESSION="0.00" EFFAMOUNT="99" CONTYPE="SAMPLE" CONVALUE="0.00"/>
      <utpricing LT="1" STARTDATE="20150102" ENDDATE="20160102" BASEAMOUNT="99" CONCESSION="0.00" EFFAMOUNT="99" CONTYPE="SAMPLE" CONVALUE="0.00"/>
      <utpricing LT="1" STARTDATE="20150102" ENDDATE="20160102" BASEAMOUNT="99" CONCESSION="0.00" EFFAMOUNT="99" CONTYPE="SAMPLE" CONVALUE="0.00"/>
      <utpricing LT="1" STARTDATE="20150102" ENDDATE="20160102" BASEAMOUNT="99" CONCESSION="0.00" EFFAMOUNT="99" CONTYPE="SAMPLE" CONVALUE="0.00"/>
</data1>
          <datatype ID="0002">
             <utpricing LT="2" STARTDATE="20160102" ENDDATE="20170102" BASEAMOUNT="99" CONCESSION="0.00" EFFAMOUNT="99" CONTYPE="SAMPLE" CONVALUE="0.00"/>
          <utpricing LT="1" STARTDATE="20150102" ENDDATE="20160102" BASEAMOUNT="99" CONCESSION="0.00" EFFAMOUNT="99" CONTYPE="SAMPLE" CONVALUE="0.00"/>
          <utpricing LT="1" STARTDATE="20150102" ENDDATE="20160102" BASEAMOUNT="99" CONCESSION="0.00" EFFAMOUNT="99" CONTYPE="SAMPLE" CONVALUE="0.00"/>
          <utpricing LT="1" STARTDATE="20150102" ENDDATE="20160102" BASEAMOUNT="99" CONCESSION="0.00" EFFAMOUNT="99" CONTYPE="SAMPLE" CONVALUE="0.00"/>
    </datatype>
</data1>
  <data2>
      <data2 ID="141" NAME="IAM | 1 | 1.00" SQFTMIN="111" SQFTMAX="222"/>
      <data2 ID="142" NAME="ASA | 1 | 1.00" SQFTMIN="111" SQFTMAX="222"/>
      <data2 ID="143" NAME="MPL | 1 | 1.00" SQFTMIN="111" SQFTMAX="222"/>
      <data2 ID="144" NAME="EBI | 1 | 1.00" SQFTMIN="111" SQFTMAX="222"/>
      <data2 ID="145" NAME="TOF | 2 | 2.00" SQFTMIN="111" SQFTMAX="222"/>
   </data2>

   <samples>
      <sample ID="001" AVAILDATE="20152901" STATUS="Unavailable" data1TYPE="001" UNITCAT="1X1" SOMEVALUE="50.00" data2ID="141">
         <item1 ID="59" item2="50.00" DESCRIPTION="Sample Description"/>
         <offeredterm LT="2" BASEAMOUNT="1120.00" TOTALCONCESSION="0.00" EFFECTIVESAMPLE="1120.00" CONTYPE="T" CONVALUE="0.00"/>
      </sample>
    <sample ID="002" AVAILDATE="20152901" STATUS="Unavailable" data1TYPE="001" UNITCAT="1X1" SOMEVALUE="50.00" data2ID="141">
         <item1 ID="59" item2="50.00" DESCRIPTION="Sample Description"/>
         <offeredterm LT="2" BASEAMOUNT="1120.00" TOTALCONCESSION="0.00" EFFECTIVESAMPLE="1120.00" CONTYPE="T" CONVALUE="0.00"/>
      </sample>

So I have tried to parse this a few ways, but what I want to end up with is a structured collection that I can then run operations on like selecting all items from data1 which have the same data1TYPE as the datatype ID etc.

Currently my code looks like this:

$XML = Storage::disk('local')->get('data\XML.xml');
$random = collect(json_decode(json_encode((array) simplexml_load_string($XML)), true));

I have been able to retrieve an object like this and then loop through by the indexes and build a separate collection for each xml tag. This doesn't feel that efficient though and I'm not sure how I would best run the comparison operations later on.

Any ideas?

like image 893
roo Avatar asked Mar 29 '18 14:03

roo


Video Answer


1 Answers

Welcome to the world of xml! The way you are doing it is the best way that I am currently aware of. A basic example of xml to json is the way you are doing it:

$xml = simplexml_load_string($xml_string);

$json = json_encode($xml);

$array = json_decode($json,TRUE);

I do see that you are type-hinting array before your simplexml_load_string call. Is there a reason for that?

Also when you state that you are not sure how to run the comparison, what exactly do you mean by that? Is your xml static, meaning never changing, or does it change in size and/or scope?

like image 132
TytonDon Avatar answered Oct 14 '22 08:10

TytonDon