You are here:  » Extraction of var in multi nod record


Extraction of var in multi nod record

Submitted by jasonargo on Fri, 2009-08-21 10:57 in

Good day David,

I am trying to extract and define 2 vars from this xml record, how do I roceed :

...infocommande...
...sitecommande...Rue du Commerce.../sitecommande...
...refid>33846378.../refid>
...status>new.../status>
...montant devise="EUR">31.00.../montant>
...date>08/08/2009 11:25:30.../date>
...transport>
...nom>Standard.../nom>
...montant devise="EUR">6.00.../montant>
.../transport>
...list nbproduit="1">
...produit ref="2538494" nb="1" modid="MOD-D8D45M902029" status="new" ecotax="0" price="25" gender="Unisexe" merchantProductId="2644" merchantOfferId="2644" brand="1ER-PRIX" basePrice="25" discountAmount="0.0000" discountPercentage="0.0000" priceWithoutEcotax="25">tourisme 1ER-PRIX 175 80 14 88 Q hiver.../produit>
.../list>
.../infocommande>

the vars are :

$produits = merchantProductId
$libelle = tourisme 1ER-PRIX 175 80 14 88 Q hiver

I have tried several paths but no luck (i.e $refproduit = mysql_real_escape_string($record["INFOCOMMANDE/LIST/MERCHANTPRODUCTID"]);

Thank you in advance for the tip...

PS : the(<>) where delibrately replaced by (...) in order for you to get the xml code...

Submitted by support on Fri, 2009-08-21 12:13

Hello Jason,

It depends what Format String you are using - but assuming that INFOCOMMANDE is a child element of each of the main records you are interested in, then the variables would be:

$produits = $record["INFOCOMMANDE/LIST/PRODUIT-MERCHANTPRODUCTID"];
$libelle = $record["INFOCOMMANDE/LIST/PRODUIT"];

The "-" is used because MERCHANTPRODUCTID is an attribute of the PRODUITE element.

Hope this helps!
Cheers,
David.

Submitted by jasonargo on Fri, 2009-08-21 12:29

Excellent job as usual David, nothing to say...

Thank's again

Submitted by jasonargo on Fri, 2009-08-21 13:05

Now that I extracted the var of $produits=mysql_real_escape_string($record["INFOCOMMANDE/LIST/PRODUIT-MERCHANTPRODUCTID"]);, I have a probleme of reading multiple articles in one order, it only echo the 1st article (ref 983) and not the following one (ref 899), am I missing a loop somewhere ? :

...infocommande>
...sitecommande>Rue du Commerce.../sitecommande>
...refid>33846438.../refid>
...status>new.../status>
...montant devise="EUR">82.00.../montant>
...date>08/08/2009 11:32:55.../date>
...transport>
...nom>Standard.../nom>
...montant devise="EUR">0.00.../montant>
.../transport>
...list nbproduit="2">
...produit ref="2537961" nb="1" modid="MOD-23578M902051" status="new" ecotax="0" gender="Unisexe" merchantProductId="983" merchantOfferId="983" price="53" brand="Kleber" basePrice="53" discountAmount="0.0000" discountPercentage="0.0000" priceWithoutEcotax="53">Tourisme Kleber Krisalp Hp 175 65 14 82 T hiver.../produit>
...produit ref="2538018" nb="1" modid="MOD-23578M902053" status="new" ecotax="0" price="29" merchantProductId="899" merchantOfferId="899" gender="Unisexe" brand="1ER-PRIX" basePrice="29" discountAmount="0.0000" discountPercentage="0.0000" priceWithoutEcotax="29">tourisme 1ER-PRIX 155 80 13 79 T été.../produit>
.../list>
.../infocommande>

on the other hand is there a way to transform the string $datepaiement = mysql_real_escape_string($record["INFOCOMMANDE/DATE"]); which is ( dd-mm-yy hh:mm:ss 08/08/2009 11:32:55) into a shortdate format (yy-mm-dd hh:mm:ss i.e. 2009-08-08 14:35:42)

all the best David

Submitted by support on Fri, 2009-08-21 13:45

Hello Jason,

This would imply that your Format String is too high a level. What you need to do is parse the file at the PRODUIT level, but as I don't have a full sample of your XML i'm not sure what the exact format will be. But let's say you are currently using:

MagicParser_parse("filename.xml","myRecordHandler","xml|TOPLEVEL/");

...you should probably be using:

MagicParser_parse("filename.xml","myProduitRecordHandler","xml|TOPLEVEL/INFOCOMMANDE/LIST/PRODUIT/");

This will mean that myProduitRecordHandler is called once for each PRODUIT element. However, there would be a change to the variable names you need to access the parameters you require. Instead of those posted above, you would now use:

$produits = $record["PRODUIT-MERCHANTPRODUCTID"];
$libelle = $record["PRODUIT"];

However, you would not be able to access the DATE field from the INFOCOMMANDE level by doing the above, so the best solution is to parse the file again this time at the level you are currently using, but with a different record handler function:

MagicParser_parse("filename.xml","myInfocommandeRecordHandler","xml|TOPLEVEL/");

Regarding the time format translation; you should be able to make use of PHP's strtotime() and date() functions to do this. For example:

$time = strtotime($record["INFOCOMMANDE/DATE"]);
$date = date("y-m-d H:i:s",$time);

Assiming that you need to access the date when you are processing each order; the trick is to copy it into a global variable that you can access from your myProduitRecordHandler.

Finally, as you will be parsing twice, it is more efficient to load the XML into a string, and then use the "string://" feature of Magic Parser.

As this is quite a lot to take in, here as a complete example demonstrating all of the above:

<?php
  
function myInfocommandeRecordHandler($record)
  {
    global 
$date;
    
$time strtotime($record["INFOCOMMANDE/DATE"]);
    
$date date("y-m-d H:i:s",$time);
  }
  function 
myProduitRecordHandler($record)
  {
    global 
$date;
    
$produits $record["PRODUIT-MERCHANTPRODUCTID"];
    
$libelle $record["PRODUIT"];
    
// and you also have $date here from the global variable
  
}
  
$xml file_get_contents("filename.xml"); // or a URL of course!
  
MagicParser_parse("string://".$xml,"myInfocommandeRecordHandler","xml|TOPLEVEL/");
  
MagicParser_parse("string://".$xml,"myProduitRecordHandler","xml|TOPLEVEL/INFOCOMMANDE/LIST/PRODUIT/");
?>

Hope this helps!
Cheers,
David.

Submitted by jasonargo on Thu, 2009-10-08 11:04

Hello David,

I am calling again on you several weeks later to try to resolve this matter, about reading several products in one order, please find hereunder the XML file I am trying to parce :

{code saved}

A small reminder of what I am trying to do, I have a probleme of reading multiple articles in one order, it only echo the 1st article (ref 983) and not the following one (ref 899),

I need to create this var $panier which is composed from $refproduit and $nbrproduit, the final result have to be (for this example) $panier = 983;2|899;1

Below is the whole script I am using :

{code saved}

Submitted by support on Thu, 2009-10-08 19:10

Hello Jason,

Could you perhaps email me the original XML document and a copy of your PHP script so far and I'll get straight onto it for you...

Cheers!
David.

Submitted by jasonargo on Fri, 2009-10-09 07:53

Good day David,

An email was just sent to your attention with the requested files...

Cheers,

Jason

Submitted by jasonargo on Fri, 2009-10-09 09:15

Divid just for my record...

Hello Jason,

I've demonstrated the best way to go about this in the attached file;
and the structure will also allow you to extract all other unique
produit details also if you need to.

Look in the code for the mod which is lines 57 - 69. This loop
utilises the way Magic Parser resolves duplicate field names the @1,
@2... etc., just as you are using earlier in the script to extract the
multiple addresses.

However, instead of being fixed, it uses a counter $i to create @n for
each iteration of the loop, until there are no more produits (line
64).

Line 64 then adds to the $paniers array the structure required for
each produit (e.g. 899;1) and then once the loop as finished, they are
all joined together into the $panier value using implode() which
sticks them together with the | character in this case (line 68).

The only reason I didn't extract $refproduit and $nbrproduit into
variables of those names is so I didn't overwrite the variables you
already created with those names; but you can access any value in the
same way within that loop, for example

$refproduit = $record["INFOCOMMANDE/LIST/PRODUIT".$postfix."-MERCHANTPRODUCTID"];

Hope this helps!
All the best,
David.