0

i have some xml files as follows-

 <?xml version="1.0"?> <contacts> <contact> <mobile_no>9829344744</mobile_no> <name>Sample Name 1</name> <email>Some Email</email> </contact> <contact> <mobile_no>9829344799</mobile_no> <name>Sample Name 2</name> <email>Some Email</email> </contact> <contact> <mobile_no>9829345035</mobile_no> <name>Sample Name 3</name> <email>Some Email</email> </contact> </contacts> 

i have two xml files in above format. each file has distinct <mobile_no> but i want to extract distinct <mobile_no> from both files.

i have tried and use following code of php

$arr_filename[] = "file1.xml"; $arr_filename[] = "file2.xml"; $arr_contact = array(); foreach($arr_filename as $filename) { $xml = new simpleXMLElement($filename, null, true); $contact_array1 = $xml->xpath('contact/mobile_no'); for($i=0; $i<count($contact_array1); $i++) { $arr_contact[$contact_array1[$i]]=true; } } 

i found array $arr_contact with distinct contact no as its key its working fine but problem is that when files has large content(records) and use more files (approximately 10) it takes too much time to process. i want to get more efficient method to extract distinct mobile no from more than one xml files. just as we extract distinct from more than one table in mysql.

10
  • 1
    You can try to merge the xml files first and then read them. Should be faster. Commented Jan 6, 2014 at 9:10
  • 1
    Instead of your method please try it. It will help you "stackoverflow.com/questions/13341305/…" Commented Jan 6, 2014 at 9:29
  • @DainisAbols thanks bro i can merge but how distinct mobile_no in new merged file. Commented Jan 6, 2014 at 11:13
  • You can use array_unique after the final array is created. Commented Jan 6, 2014 at 11:18
  • @DainisAbols i don't want to use array because may be there a lacs of records. it use memory a lot. Commented Jan 6, 2014 at 11:22

1 Answer 1

1

Let's see, first merge:

$xml = simplexml_load_string($x1); // assume XML in $x1 and $x2 $xml2 = simplexml_load_string($x2); // merge $xml2 into $xml foreach ($xml2->contact as $contact) { $newcontact = $xml->addChild("contact"); foreach ($contact->children() as $name => $value) $newcontact->addChild($name, $value); } unset($xml2); // dispose $xml2 

then select:

// use xpath to select <contact> with distinct contact/mobile_no $results = $xml->xpath("/contacts/contact[not(mobile_no = following::mobile_no)]"); var_dump($results); 

see it working: https://eval.in/86352

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.