Bonjour,
Je suis vraiment perdu depuis quelques jours.
J'ai un fichier en format json et je veux donner ses informations dans un fichier XML.
J'arrive calculer le prix total et mettre dans un fichier XML (jusqu'ai ici c'est simple).
voici mon json :
et voici mon fichier XML que je veux obtenirCode:
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 { "ville": "VERDUN", "articles": [ { "prix": "2.00", "quantite": "1", "nom_item": "tomates", "tax": [ { "prix": "0.25", "quantite": "1", "nom_item": "taxe1" }, { "prix": "0.50", "quantite": "1", "nom_item": "taxe2" } ] }, { "prix": "1.00", "quantite": "1", "nom_item": "patates", "tax": [ { "prix": "0.12", "quantite": "1", "nom_item": "taxe1" }, { "prix": "0.75", "quantite": "1", "nom_item": "taxe3" } ] } ] }
Code:
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 <?xml version="1.0" encoding="UTF-8"?> <resume> <ville>VERDUN</ville> <total_item_resume>4.12</total_item_resume> <articles> <chaque_item> <prix>2.00</prix> <quantite>1</quantite> <nom_item>tomates</nom_item> </chaque_item> <chaque_item> <prix>1.00</prix> <quantite>1</quantite> <nom_item>patates</nom_item> </chaque_item> <chaque_item> <prix>0.37</prix> <quantite>1</quantite> <nom_item>taxe1</nom_item> </chaque_item> <chaque_item> <prix>0.50</prix> <quantite>1</quantite> <nom_item>taxe2</nom_item> </chaque_item> <chaque_item> <prix>0.25</prix> <quantite>1</quantite> <nom_item>taxe2</nom_item> </chaque_item> </articles> </resume>
Je fais les premiers 5 lignes de mon fichier XML avec le code suivant :
Code:
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 header('Content-Type: text/xml'); header('Content-Type: application/xml'); $doc= new DOMDocument('1.0', 'UTF-8'); //formatage $doc->preserveWhiteSpace = false; $doc->formatOutput = true; //creating an xslt adding processing line /*$xslt = $doc->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="Vigieresume.xslt"'); //adding it to the xml $doc->appendChild($xslt);*/ $datajs = "fruit.json"; // 3article (1 fruit + 2 taxe) $datajs = file_get_contents($datajs); $data = json_decode($datajs); $city= $data->ville; $total_items_avec_taxe = '0.0000'; // create resume element $lavel1_resume = $doc->createElement("resume"); $doc->appendChild($lavel1_resume); // create child element -------------------| $ville = $doc->createElement("ville"); $lavel1_resume->appendChild($ville); // create text node =====================================|| $text_ville = $doc->createTextNode($city); $ville->appendChild($text_ville); //Calcule de prix total avec taxe //$data = json_decode($datajs); $data_decode_array = json_decode($datajs, 1); // transforme en array $total_items_avec_taxe = 0; foreach ($data_decode_array['articles'] as $product) { $total = $product['prix']; //prix hors taxe foreach ($product['tax'] as $tax) { $total += $tax['prix']; //ajoute tax au prix hors taxe } $total_items_avec_taxe += $product['quantite'] * $total; } $total_items_avec_taxe = number_format($total_items_avec_taxe, 2); // create child element -------------------| $total_item_resume = $doc->createElement("total_item_resume"); $lavel1_resume->appendChild($total_item_resume); // create text node =====================================|| $text_total_item_resume = $doc->createTextNode($total_items_avec_taxe); $total_item_resume->appendChild($text_total_item_resume); echo $doc->saveXML(); $doc->save("dom_xmltest.xml");
Par contre je ne sais pas comment je peux inserer les lignes suivants pour faire la reste :
Alors j'ai essayé avec cela :Code:
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 // create child element with sub element-------------------| -------------------| $chaque_item = $articles->appendChild($doc->createElement('chaque_item')); //$chaque_item = $doc->createElement("chaque_item"); // $lavel1_resume->appendChild($chaque_item); // create child element with sub element-------------------| -------------------| | -------------------| $prix = $chaque_item->appendChild($doc->createElement('prix')); //$prix = $doc->createElement("prix"); // $lavel1_resume->appendChild($product['prix']); // ou bien selon le taxe $lavel1_resume->appendChild($tax['prix']); // create text node $prix = $chaque_item->appendChild($doc->createTextNode('0.25')); // $text_prix = $doc->createTextNode("11.00"); // $lavel1_resume->appendChild($text_prix); // create child element with sub element-------------------| -------------------| | -------------------| $quantite = $chaque_item->appendChild($doc->createElement('quantite')); //$quantite = $doc->createElement("quantite"); // $lavel1_resume->appendChild($quantite); // create text node $quantite = $chaque_item->appendChild($doc->createTextNode('1')); // $text_quantite = $doc->createTextNode("un"); // $lavel1_resume->appendChild($text_quantite); // create child element with sub element-------------------| -------------------| | -------------------| $nom_item = $chaque_item->appendChild($doc->createElement('nom_item')); // create text node $nom_item = $chaque_item->appendChild($doc->createTextNode('taxe1')); // create child element with sub element-------------------| -------------------| | -------------------|
Code:
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 // create child element with sub element-------------------| //$articles = $lavel1_resume->appendChild($doc->createElement('articles')); $articles = $doc->createElement("articles"); $lavel1_resume->appendChild($articles); // create child element with sub element-------------------| -------------------| $chaque_item = $articles->appendChild($doc->createElement('chaque_item')); //$chaque_item = $doc->createElement("chaque_item"); // $lavel1_resume->appendChild($chaque_item); // create child element with sub element-------------------| -------------------| | -------------------| $prix = $chaque_item->appendChild($doc->createElement('prix')); //$prix = $doc->createElement("prix"); // $lavel1_resume->appendChild($prix); foreach ($data_decode_array['articles'] as $product){ $prix = $chaque_item->appendChild($doc->createTextNode($product['prix'])); // prix d'un article $quantite = $chaque_item->appendChild($doc->createElement('quantite')); $quantite = $chaque_item->appendChild($doc->createTextNode($product['quantite'])); $nom_item = $chaque_item->appendChild($doc->createElement('nom_item')); $nom_item = $chaque_item->appendChild($doc->createTextNode($product['nom_item'])); foreach ($product['tax'] as $tax){ $prix = $chaque_item->appendChild($doc->createTextNode($tax['prix'])); //// prix d'une taxe comme un article $quantite = $chaque_item->appendChild($doc->createElement('quantite')); $quantite = $chaque_item->appendChild($doc->createTextNode($tax['quantite'])); $nom_item = $chaque_item->appendChild($doc->createElement('nom_item')); $nom_item = $chaque_item->appendChild($doc->createTextNode($tax['nom_item'])); } } // $text_prix = $doc->createTextNode("11.00"); // $lavel1_resume->appendChild($text_prix); // create child element with sub element-------------------| -------------------| | -------------------| // create child element -------------------| echo $doc->saveXML(); $doc->save("mon_dom_xml.xml");
Comment je peux faire ? vous avez une idée ?
Merci et bonne journée