1

I am trying to get information from external large xml files; from file 1 (vehicleList.xml) and file 2 (CheckVehicles.xml) into a PHP file. All values in XML file 2 are in XML file 1. I would like to display only values in file 1 that are in XML file 2.

My foreach loop code can bring results for up to 130 items (that is if I reduce the items in XML file 2 to 130 items/nodes). However if I remove the if statement, I am able to get all the 3340 items/vehicles from XML file 1.

Where am I going wrong? I tried arrays but failed.

Here is my code:

//XML FILE 1 with 1300 items
$myXML = new SimpleXMLElement('CheckVehicles.xml', NULL, TRUE);//
foreach($myXML->root->item as $item){
        $listArrayNew[(int)$item->value] = (int)$item->value;
}

//XML FILE 2 with 3340 vehicles
$parser = new SimpleXMLElement('vehicleList.xml', NULL, TRUE);
foreach ($parser->GetVehiclesListResponse->GetVehiclesListResult->Vehicle as $Vehicle) {
    if($listArrayNew[(int)$Vehicle->ID] == (int)$Vehicle->ID){
        $vehicle    = $Vehicle->Description;    
        $regNumber  = $Vehicle->RegistrationNumber; 
        $siteID     = $Vehicle->SiteID;     
        $row       .= "<tr>
                            <td>".$vehicle."</td>
                            <td>".$regNumber."</td>
                            <td>".$siteId."</td>
                        </tr>"; 

    }
}

Here are the XML files:

XML file 1: vehicleList.xml

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope>
  <GetVehiclesListResponse>
    <GetVehiclesListResult>
      <Vehicle>
        <ID>153</ID>
        <SiteID>11</SiteID>
        <GroupID>3</GroupID>
        <Description>A.O Basid KAR 459 E</Description>
        <RegistrationNumber>KAR 459 E</RegistrationNumber>
      </Vehicle>

      ..............................


      <Vehicle>
        <ID>3340</ID>
        <SiteID>25</SiteID>
        <GroupID>4</GroupID>
        <Description>UAR 712B White Nissan Tiida (Deus Mubangizi)</Description>
        <RegistrationNumber>UAR 712B</RegistrationNumber>
      </Vehicle>
    </GetVehiclesListResult>
  </GetVehiclesListResponse>
</soap:Envelope>

XML file 2: CheckVehicles.xml

<?xml version="1.0" encoding="utf-8"?>
<Result>
  <root>
    <item>
      <index>0</index>
      <value>153</value>
    </item>

   ...................

    <item>
      <index>1300</index>
      <value>128</value>
    </item>
  </root>
</Result>
doubleDown
  • 8,048
  • 1
  • 32
  • 48
mukamaivan
  • 1,435
  • 6
  • 16
  • 24
  • Even if they seem large, I won't say those files are large. How many megabytes do they have? more or less than 10 MB? - Also check, you've mixed the order/numbering of the files. First is second, second is first. Not that you look into the wrong elements. – hakre Jan 10 '13 at 11:41
  • Actually they are less that 5 MB. – mukamaivan Jan 10 '13 at 11:45
  • that's not really large. Anyway I suggest you take a look into xpath, I've added an answer, hopefully it helps you aligning those two files more easily. – hakre Jan 10 '13 at 12:36

1 Answers1

1

I don't know where you go wrong in your case. However if you want to select elements from the second file based on a criteria (e.g. an ID / unique Number) from the first file I suggest you make use of xpath in your case:

  • Obtain the numbers from the first file that are the criteria (e.g. /*/root/item/value)
  • Select all elements from the second file that match the criteria (e.g. ID in /*/GetVehiclesListResponse/GetVehiclesListResult/Vehicle).

The later point can best be achieved by using the technique outlined in Is there anything for XPATH like SQL “IN”? which is creating a comma separated list of the numbers to select and then compare this against each elements number.

Example:

Consider there 2 500 out of 10 000 elements in a first file and in a second file there are 10 000 elements. Each element can be uniquely identified by it's ID.

The first file has this layout:

<?xml version="1.0"?>
<root>
    <item>
        <index>0</index>
        <id>604</id>
    </item>
    <item>
        <index>1</index>
        <id>2753</id>
    </item>
    ...
</root>

And the second file has this layout.

<?xml version="1.0"?>
<list>
    <item>
        <id>1</id>
        <some>Number: 33</some>
    </item>
    <item>
        <id>2</id>
        <some>Number: 35</some>
    </item>
    ...
</list>

The xpath query to get all IDs from the first file therefore is:

//item/id

And the query for the second file can be expressed with SimpleXML in PHP as:

$ids   = implode(',', $file1->xpath('//item/id'));
$query = '//item[contains(",' . $ids . ',", concat(",", id, ","))]';

You can find example code of that example here: http://eval.in/6370

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836