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.

michi
  • 6,565
  • 4
  • 33
  • 56
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51
  • 1
    You can try to [merge the xml](http://stackoverflow.com/questions/10163675/merge-xml-files-in-php) files first and then read them. Should be faster. – Peon Jan 06 '14 at 09:10
  • 1
    Instead of your method please try it. It will help you "http://stackoverflow.com/questions/13341305/how-get-distinct-value-node-xml" – Janty Jan 06 '14 at 09:29
  • @DainisAbols thanks bro i can merge but how distinct mobile_no in new merged file. – Satish Sharma Jan 06 '14 at 11:13
  • You can use [array_unique](http://php.net/array_unique) after the final array is created. – Peon Jan 06 '14 at 11:18
  • @DainisAbols i don't want to use array because may be there a lacs of records. it use memory a lot. – Satish Sharma Jan 06 '14 at 11:22
  • `$arr_contact[$contact_array1[$i]]=true;` you are using an array already... – Peon Jan 06 '14 at 11:32
  • @DainisAbols yes thats why i put question here i can't use array. ok one more thing can we read xml file node by node without using array. – Satish Sharma Jan 06 '14 at 11:35
  • How do you want to process the numbers? Where do you want to store them? What is your desired end result? – Peon Jan 06 '14 at 11:37
  • @DainisAbols i am storing contacts in xml files and get distinct. i am using mysql db already but there is a large no of records and found slow process when operating on some conditions. so the main thing is that reduce processing time to extract mobile no with some condtions. so i decided to use xml files to store contacts. its ok. now only thing is remaining is thant extract distinct mobile no from more than one file. – Satish Sharma Jan 06 '14 at 11:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44612/discussion-between-dainis-abols-and-satish-sharma) – Peon Jan 06 '14 at 11:42

1 Answers1

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

michi
  • 6,565
  • 4
  • 33
  • 56