0
votes

I have an xml file and using simplexml :

   $xml = simplexml_load_file('feed.xml', 'SimpleXMLElement', LIBXML_NOCDATA);

   foreach($xml->item as $products)
     {
     $name =  (string)trim($products->name) ;
     $weight = (string)$products->weight;

     .....and so on...

The xml feed is as follows:

   <?xml version="1.0"?>
    <root>
    <update_time>2014-06-09</update_time>

    <item>
    <name>Product 1</name>
    <weight>0.3000</weight>
    <Price>31.4400</Price>
    </item>

   <item>
    <name>Product 2</name>
    <weight>0.2000</weight>
    <Price>32.4400</Price>
    </item>

  <item> //duplicate
    <name>Product 1</name>
   <weight>0.1000</weight>
   <Price>22.4400</Price>
   </item>

   </root>      

The name value had duplicates and i need only values without duplicates. I need to eliminate the whole item node (all children) with duplicate name child.

I have read that this can be done with xpath. How can i do it in the above code?? Can xpath be used inside foreach loop? pretty confused on how to incorporate this in my code above.

Help requested..

1
can you show your xml - Rakesh Sharma
@Rakesh Sharma XML code updated - user3737431
distinct-values in xpath-functions may do the job..I dunno how to do it in my code above. sorry, i am a newbie. - user3737431
is the end result in array format? or xml? - user1978142
@ kevinabelita i am using the foreach loop to insert the values in mysql db, after removing duplicate values for items with duplicate name - user3737431

1 Answers

0
votes

If you want to select only unique values and prepare it for insertion in database, a simple foreach loop should suffice. Consider this example: Sample Output

$raw_xml = '<?xml version="1.0"?><root><update_time>2014-06-09</update_time><item><name>Product 1</name><weight>0.3000</weight><Price>31.4400</Price></item><item><name>Product 2</name><weight>0.2000</weight><Price>32.4400</Price></item><item><name>Product 1</name><weight>0.1000</weight><Price>22.4400</Price></item></root>';
$xml = simplexml_load_string($raw_xml);
$items = json_decode(json_encode($xml), true);
$update_time = $items['update_time'];
$items = $items['item'];
$new_items = array();
foreach($items as $value) {
    if(!isset($new_items[$value['name']])) {
        $new_items[$value['name']] = $value;
    }
}

$new_items = array_values($new_items);
// $new_items should have unique values

$new_items should yield:

Array
(
    [0] => Array
        (
            [name] => Product 1
            [weight] => 0.3000
            [Price] => 31.4400
        )

    [1] => Array
        (
            [name] => Product 2
            [weight] => 0.2000
            [Price] => 32.4400
        )

)

Insert into database:

// insert to database
$dbh = new PDO('mysql:host=localhost;dbname=test', 'test', 'test');
foreach($new_items as $key => $value) {
    $stmt = $dbh->prepare("INSERT INTO products (name, weight, price) VALUES (:name, :weight, :price)");
    $stmt->execute(array(':name' => $value['name'], ':weight' => $value['weight'], ':price' => $value['Price']));
}