Bonjour
J'ai une data json, ci-dessous qui contient un article avec les taxes :
et je récupère les données sans problème en mettant dans mon fichier XML ci dessous :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 { "date": "2015-05-05 12:41", "shop": "Toto", "products": [ { "price": "2.00", "quantity": "1", "description": "tototati", "tax": [ { "price": "1.00", "quantity": "1", "description": "tax1" }, { "price": "2.00", "quantity": "1", "description": "tax2" } ] } ] }
Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 <?xml version="1.0" encoding="UTF-8"?> <Biling> <date>2015-05-05 12:41</date> <shop>Toto</shop> <amount>5</amount> <products> <item><price/>2.00<quantity/>1<description/>Chocolat</item> <item><price/>1.00<quantity/>1<description/>tax1</item> <item><price/>2.00<quantity/>1<description/>tax2</item> </products> </Biling>
et voici une partie de mon code :
Jusqu'ici, tout va bien.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 dateBiling = $dataDecode->date; $shopName = $dataDecode->shop; foreach ($dataDecode->products as $obj){ $ItemPrice = $obj->price; $ItemQuantity = $obj->quantity; $ItemDescription = $obj->description; $taxes = (array)$obj->tax; $taxesPrice1 = !empty($taxes[0]) ? $taxes[0]->price : 0.00; $taxesPrice2 = !empty($taxes[1]) ? $taxes[1]->price : 0.00; $taxesQuantity1 = !empty($taxes[0]) ? $taxes[0]->quantity : 0; $taxesQuantity2 = !empty($taxes[1]) ? $taxes[1]->quantity : 0; $taxesDescription1 = !empty($taxes[0]) ? $taxes[0]->description : '1er tax null'; $taxesDescription2 = !empty($taxes[1]) ? $taxes[1]->description : '2e tax null'; $amountAll = $ItemPrice * $ItemQuantity + $taxesPrice1 + $taxesPrice2; } header('Content-Type: text/xml'); header('Content-Type: application/xml'); $doc= new DOMDocument('1.0', 'UTF-8'); $doc->preserveWhiteSpace = true; $doc->formatOutput = true; // Biling element $bilingInfo = $doc->createElement("Biling"); $doc->appendChild($bilingInfo); // child element $date = $doc->createElement("date"); $bilingInfo->appendChild($date); // text node $text_date = $doc->createTextNode($dateBiling); $date->appendChild($text_date); // child element $shop = $doc->createElement("shop"); $bilingInfo->appendChild($shop); // text node $text_shop = $doc->createTextNode($shopName); $shop->appendChild($text_shop); // child element $amount = $doc->createElement("amount"); $bilingInfo->appendChild($amount); // text node $text_amount = $doc->createTextNode($amountAll); $amount->appendChild($text_amount); // products element with sub element $products = $doc->createElement("products"); $bilingInfo->appendChild($products); // item child element with sub element $item = $products->appendChild($doc->createElement('item')); //$item = $doc->createElement("item"); // $bilingInfo->appendChild($item); // child element with sub element | $price = $item->appendChild($doc->createElement('price')); // text node $price = $item->appendChild($doc->createTextNode($ItemPrice)); // child element with sub element | $quantity = $item->appendChild($doc->createElement('quantity')); // text node $quantity = $item->appendChild($doc->createTextNode($ItemQuantity)); // create child element with sub element | $description = $item->appendChild($doc->createElement('description')); // create text node $description = $item->appendChild($doc->createTextNode($ItemDescription)); // taxes 1 // item child element with sub element $item = $products->appendChild($doc->createElement('item')); //$item = $doc->createElement("item"); // $bilingInfo->appendChild($item); // child element with sub element | $price = $item->appendChild($doc->createElement('price')); // text node $price = $item->appendChild($doc->createTextNode($taxesPrice1)); // child element with sub element | $quantity = $item->appendChild($doc->createElement('quantity')); // text node $quantity = $item->appendChild($doc->createTextNode($taxesQuantity1)); // create child element with sub element | $description = $item->appendChild($doc->createElement('description')); // create text node $description = $item->appendChild($doc->createTextNode($taxesDescription1)); // taxes 2 // item child element with sub element $item = $products->appendChild($doc->createElement('item')); //$item = $doc->createElement("item"); // $bilingInfo->appendChild($item); // child element with sub element | $price = $item->appendChild($doc->createElement('price')); // text node $price = $item->appendChild($doc->createTextNode($taxesPrice2)); // child element with sub element | $quantity = $item->appendChild($doc->createElement('quantity')); // text node $quantity = $item->appendChild($doc->createTextNode($taxesQuantity2)); // create child element with sub element | $description = $item->appendChild($doc->createElement('description')); // create text node $description = $item->appendChild($doc->createTextNode($taxesDescription2)); echo $doc->saveXML(); $doc->save("dom_xmlSauvegarder.xml");
Par contre, lorsqu'il y a 2 articles ou bien 3 ou 4 etc. Je ne sais plus comment je peux construire mon XML dynamiquement ?
Voici mon json avec 3 articles :
et j'aimerais bien obtenir un XML comme cela :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 { "date": "2015-05-05 12:41", "shop": "Toto", "products": [ { "price": "2.00", "quantity": "1", "description": "chocolat", "tax": [ { "price": "1.00", "quantity": "1", "description": "tax1" }, { "price": "2.00", "quantity": "1", "description": "tax2" } ] }, { "price": "3.00", "quantity": "1", "description": "bonbon", "tax": [ { "price": "2.00", "quantity": "1", "description": "tax1" }, { "price": "3.00", "quantity": "1", "description": "tax2" } ] }, { "price": "2.00", "quantity": "1", "description": "gateaux", "tax": [ { "price": "1.00", "quantity": "1", "description": "tax1" }, { "price": "2.00", "quantity": "1", "description": "tax2" } ] } ] }
Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 <?xml version="1.0" encoding="UTF-8"?> <Biling> <date>2015-05-05 12:41</date> <shop>Toto</shop> <amount>18.00</amount> <products> <item><price/>2.00<quantity/>1<description/>Chocolat</item> <item><price/>3.00<quantity/>1<description/>bonbon</item> <item><price/>2.00<quantity/>1<description/>gateaux</item> <item><price/>4.00<quantity/>1<description/>tax1</item> <item><price/>7.00<quantity/>1<description/>tax2</item> </products> </Biling>
Mais je ne sais plus comment je peux.
En fait, j'ai la difficulté de concevoir en 2 niveaux :
1er comment je peux ajouter des articles dans mon XML selon le nombre ?
2e comment je peux additionner mes taxes selon le nombre d'articles et trouver la bon total (amount)?
Merci
Partager