Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> plik xml wyciągnięcie odpowiednich informacji
skine
post 26.05.2014, 16:56:56
Post #1





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 26.05.2014

Ostrzeżenie: (0%)
-----


Witam, mam takie zadanie aby z pliku xml wyciągnąć odpowiednio id produktu oraz jego nazwę niemiecką(czyli 1 i 4 linia), a następnie zapisać to w pliku.
Za bardzo nie wiem jak się za to wziąć, może ktoś coś doradzi ? Z xml jeszcze nie miałem do czynienia także prosiłbym o jakieś najprostsze rozwiązania.
Tutaj fragment kodu





Kod
<product product_id = "15409">
            <name><![CDATA[EMAMODA SUKIENKA - CZARNY - BIAŁY 5705-8]]></name>
            <name_en><![CDATA[EMAMODA DRESS - BLACK - WHITE 5705-8]]></name_en>
            <name_de><![CDATA[EMAMODA DRESS - SCHWARZ - WEISS 5705-8]]></name_de>
                        <name_ru><![CDATA[EMAMODA ПЛАТЬЕ - черный - белый 5705-8]]></name_ru>
            <description><![CDATA[Szyfonowa sukienka z koronką<br /><p>
    <img alt=""" /></p><br /><iframe width="400" frameborder="no" src=""></iframe>]]></description>
            <description_en><![CDATA[Chiffon dress with lace<br /><p>
    <img alt="" src="" /></p><br /><iframe width="400" frameborder="no" src=""></iframe>]]></description_en>
            <description_de><![CDATA[Chiffon-Kleid mit Spitze<br /><p>
    <img alt="" src="" /></p><br /><iframe width="400" frameborder="no" src=""></iframe>]]></description_de>
                        <description_ru><![CDATA[Шифон платье с кружевом<br /><p>
    <img alt="" src="" /></p><br /><iframe width="400" frameborder="no" src=""></iframe>]]></description_ru>
            <code>5705-8</code>
            <producer_code>Y593</producer_code>
            <type>f</type>
            <producer><![CDATA[Emamoda]]></producer>
            <photos>
                            <photo></photo>
                <photo></photo>
                
                <photo></photo>
                
                <photo></photo>
                
            </photos>
            <category_id>15</category_id>
            <category_id2>0</category_id2>
            <add_date>2014-05-05 08:39:42</add_date>
            <price_netto>61.40</price_netto>
            <sale>0</sale>
            <sale_price_netto>0.00</sale_price_netto>
            <fillup>0</fillup>
            <sizes>
                <size name="S" count="1" />
                <size name="M" count="4" />
                <size name="L" count="3" />
            </sizes>
                        <color><![CDATA[CZARNY - BIAŁY]]></color>
                        <color_en><![CDATA[BLACK - WHITE]]></color_en>
            <color_de><![CDATA[SCHWARZ - WEISS]]></color_de>
                        <color_ru><![CDATA[BLACK - WHITE]]></color_ru>
                        <cloth><![CDATA[100% poliester]]></cloth>
                        <cloth_en><![CDATA[100% poliester]]></cloth_en>
            <cloth_de><![CDATA[100% poliester]]></cloth_de>
                        <cloth_ru><![CDATA[100% poliester]]></cloth_ru>
                        <price_netto_eur>15.24</price_netto_eur>


Ten post edytował skine 26.05.2014, 17:06:45
Go to the top of the page
+Quote Post
Turson
post 26.05.2014, 17:42:13
Post #2





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

Ostrzeżenie: (0%)
-----


http://www.php.net/manual/en/simplexml.examples-basic.php
Go to the top of the page
+Quote Post
skine
post 27.05.2014, 11:10:24
Post #3





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 26.05.2014

Ostrzeżenie: (0%)
-----


Ciągle mi to nie wychodzi, nawet wzorując się na przykładach od Turson. Nie potrafię uzyskać nawet samego id.

Ten post edytował skine 27.05.2014, 11:12:40
Go to the top of the page
+Quote Post
Turson
post 27.05.2014, 11:19:25
Post #4





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

Ostrzeżenie: (0%)
-----


Pokaż próby
Go to the top of the page
+Quote Post
skine
post 27.05.2014, 11:31:25
Post #5





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 26.05.2014

Ostrzeżenie: (0%)
-----


No właśnie w przykładach są wyciągane elementy z pomiędzy tagów jak np <plot> xxx </plot> u mnie id znajduje się <product product_id = "15409">
Go to the top of the page
+Quote Post
Turson
post 27.05.2014, 11:34:36
Post #6





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

Ostrzeżenie: (0%)
-----


Jak już masz otwarty plik xml np. w $plik i chcesz się dostać do product_id to
  1. echo $plik->product['product_id'];
Go to the top of the page
+Quote Post
nospor
post 27.05.2014, 11:35:54
Post #7





Grupa: Moderatorzy
Postów: 36 440
Pomógł: 6290
Dołączył: 27.12.2004




W przykladzie Example #5 Using attributes masz pokazane jak pobierac wartosci z atrybutów...


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
skine
post 27.05.2014, 11:51:53
Post #8





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 26.05.2014

Ostrzeżenie: (0%)
-----


Cytat(Turson @ 27.05.2014, 12:34:36 ) *
Jak już masz otwarty plik xml np. w $plik i chcesz się dostać do product_id to
  1. echo $plik->product['product_id'];


Tak wygląda od początku xml


Kod
<?xml version="1.0" encoding="utf-8"?>
<root targetNamespace="http://yournewstyle.pl">
    <date>2014-05-26 03:02:30</date>
    <categories>
        <category category_id = "1">
            <parent>0</parent>
            <name><![CDATA[Damskie]]></name>
        </category>
        <category category_id = "2">
            <parent>0</parent>
            <name><![CDATA[Męskie]]></name>
        </category>
        <category category_id = "3">
            <parent>0</parent>
            <name><![CDATA[Dodatki]]></name>
        </category>
        <category category_id = "33">
            <parent>2</parent>
            <name><![CDATA[Koszule]]></name>
        </category>
        <category category_id = "12">
            <parent>1</parent>
            <name><![CDATA[Kurtki i Płaszcze]]></name>
        </category>
        <category category_id = "34">
            <parent>2</parent>
            <name><![CDATA[Bluzki]]></name>
        </category>
        <category category_id = "13">
            <parent>1</parent>
            <name><![CDATA[Spodnie i spodenki]]></name>
        </category>
        <category category_id = "51">
            <parent>2</parent>
            <name><![CDATA[Długi rękaw]]></name>
        </category>
        <category category_id = "15">
            <parent>1</parent>
            <name><![CDATA[Sukienki ]]></name>
        </category>
        <category category_id = "52">
            <parent>2</parent>
            <name><![CDATA[Spodnie, spodenki]]></name>
        </category>
        <category category_id = "16">
            <parent>1</parent>
            <name><![CDATA[Swetry]]></name>
        </category>
        <category category_id = "55">
            <parent>2</parent>
            <name><![CDATA[Swetry]]></name>
        </category>
        <category category_id = "17">
            <parent>1</parent>
            <name><![CDATA[Topy]]></name>
        </category>
        <category category_id = "50">
            <parent>3</parent>
            <name><![CDATA[Szale]]></name>
        </category>
        <category category_id = "18">
            <parent>1</parent>
            <name><![CDATA[Tuniki]]></name>
        </category>
        <category category_id = "19">
            <parent>1</parent>
            <name><![CDATA[Obuwie]]></name>
        </category>
        <category category_id = "20">
            <parent>1</parent>
            <name><![CDATA[Komplety]]></name>
        </category>
        <category category_id = "23">
            <parent>1</parent>
            <name><![CDATA[Spódniczki]]></name>
        </category>
        <category category_id = "24">
            <parent>1</parent>
            <name><![CDATA[Koszule]]></name>
        </category>
        <category category_id = "27">
            <parent>1</parent>
            <name><![CDATA[Bluzki]]></name>
        </category>
        <category category_id = "28">
            <parent>1</parent>
            <name><![CDATA[Dodatki]]></name>
        </category>
        <category category_id = "30">
            <parent>1</parent>
            <name><![CDATA[Stroje kąpielowe]]></name>
        </category>
        <category category_id = "35">
            <parent>1</parent>
            <name><![CDATA[Marynarki i żakiety]]></name>
        </category>
    </categories>
    <products>
        <product product_id = "15409">
            <name><![CDATA[EMAMODA SUKIENKA - CZARNY - BIAŁY 5705-8]]></name>
            <name_en><![CDATA[EMAMODA DRESS - BLACK - WHITE 5705-8]]></name_en>
            <name_de><![CDATA[EMAMODA DRESS - SCHWARZ - WEISS 5705-8]]></name_de>
                        <name_ru><![CDATA[EMAMODA ПЛАТЬЕ - черный - белый 5705-8]]></name_ru>
            <description><![CDATA[Szyfonowa sukienka z koronką<br /><p>
    <img alt="" src="http://www.yournewstyle.pl/grafika/wymiary/emamoda2.gif" /></p><br /><iframe width="400" frameborder="no" src="http://www.youtube.com/embed/yH4UPXFsu-Y?rel=1&amp;autoplay=1"></iframe>]]></description>
            <description_en><![CDATA[Chiffon dress with lace<br /><p>
    <img alt="" src="http://www.yournewstyle.pl/grafika/wymiary/emamoda2.gif" /></p><br /><iframe width="400" frameborder="no" src="http://www.youtube.com/embed/yH4UPXFsu-Y?rel=1&amp;autoplay=1"></iframe>]]></description_en>
            <description_de><![CDATA[Chiffon-Kleid mit Spitze<br /><p>
    <img alt="" src="http://www.yournewstyle.pl/grafika/wymiary/emamoda2.gif" /></p><br /><iframe width="400" frameborder="no" src="http://www.youtube.com/embed/yH4UPXFsu-Y?rel=1&amp;autoplay=1"></iframe>]]></description_de>
                        <description_ru><![CDATA[Шифон платье с кружевом<br /><p>
    <img alt="" src="http://www.yournewstyle.pl/grafika/wymiary/emamoda2.gif" /></p><br /><iframe width="400" frameborder="no" src="http://www.youtube.com/embed/yH4UPXFsu-Y?rel=1&amp;autoplay=1"></iframe>]]></description_ru>
            <code>5705-8</code>
            <producer_code>Y593</producer_code>
            <type>f</type>
            <producer><![CDATA[Emamoda]]></producer>
            <photos>
                            <photo>http://yournewstyle.pl/files/clothes/big/b93c898bbb07f29b19eca41ea7e0fbf1.jpg</photo>
                <photo>http://yournewstyle.pl/files/clothes/big/cf77f3e0c0595f243e52cb1838c5769f.jpg</photo>
                
                <photo>http://yournewstyle.pl/files/clothes/big/64c2c591dc85e668f64dd5a34cf1d6ab.jpg</photo>
                
                <photo>http://yournewstyle.pl/files/clothes/big/d3e5a098e1e3fcc2e2cf2c3f5bb02c2c.jpg</photo>
                
            </photos>
            <category_id>15</category_id>
            <category_id2>0</category_id2>
            <add_date>2014-05-05 08:39:42</add_date>
            <price_netto>61.40</price_netto>
            <sale>0</sale>
            <sale_price_netto>0.00</sale_price_netto>
            <fillup>0</fillup>
            <sizes>
                <size name="S" count="1" />
                <size name="M" count="4" />
                <size name="L" count="3" />
            </sizes>
                        <color><![CDATA[CZARNY - BIAŁY]]></color>
                        <color_en><![CDATA[BLACK - WHITE]]></color_en>
            <color_de><![CDATA[SCHWARZ - WEISS]]></color_de>
                        <color_ru><![CDATA[BLACK - WHITE]]></color_ru>
                        <cloth><![CDATA[100% poliester]]></cloth>
                        <cloth_en><![CDATA[100% poliester]]></cloth_en>
            <cloth_de><![CDATA[100% poliester]]></cloth_de>
                        <cloth_ru><![CDATA[100% poliester]]></cloth_ru>
                        <price_netto_eur>15.24</price_netto_eur>
                        

        </product>


Kod
<?php

$xml = simplexml_load_file("yournewstyle.xml");

echo $xml->products->product['product_id'];

?>

To tak powinno wyglądać ?

Ten post edytował skine 27.05.2014, 11:55:45
Go to the top of the page
+Quote Post
Turson
post 27.05.2014, 11:57:05
Post #9





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

Ostrzeżenie: (0%)
-----


raczej tak
  1. $xml = simplexml_load_file("yournewstyle.xml");
  2. foreach($xml->root->products->product as $v){
  3. echo $v['product_id'];
  4. }
Go to the top of the page
+Quote Post
skine
post 27.05.2014, 12:08:00
Post #10





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 26.05.2014

Ostrzeżenie: (0%)
-----


Coś muszę jeszcze robić chyba źle albo czegoś wgl nie robić, nic nie wyskakuje.
Go to the top of the page
+Quote Post
Pyton_000
post 27.05.2014, 20:54:27
Post #11





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


  1. $xml = simplexml_load_file("yournewstyle.xml");
  2. foreach($xml->root->products->product as $v){
  3. echo (string)$v['product_id'];
  4. }
Go to the top of the page
+Quote Post
skine
post 28.05.2014, 16:24:00
Post #12





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 26.05.2014

Ostrzeżenie: (0%)
-----


Dzięki wielkie za pomoc, osiągnąłem to co chciałem.

Teraz próbuję wyciągnąć zawartość z pomiędzy tagów za pomocą samego simplexmla jak jest pokazane tutaj.
tutaj

Mój xml ma taką zawartość

  1. <?xml version='1.0' standalone='yes'?>
  2. <movies>
  3. <movie>
  4. <title>PHP: Behind the Parser</title>
  5. <characters>
  6. <character>
  7. <name>Ms. Coder</name>
  8. <actor>Onlivia Actora</actor>
  9. </character>
  10. <character>
  11. <name>Mr. Coder</name>
  12. <actor>El ActÓr</actor>
  13. </character>
  14. </characters>
  15. <plot>
  16. So, this language. It's like, a programming language. Or is it a
  17. scripting language? All is revealed in this thrilling horror spoof
  18. of a documentary.
  19. </plot>
  20. <great-lines>
  21. <line>PHP solves all my web problems</line>
  22. </great-lines>
  23. <rating type="thumbs">7</rating>
  24. <rating type="stars">5</rating>
  25. </movie>
  26. </movies>



Mój kod to
  1. <?php
  2.  
  3. $xml = simplexml_load_file('test.xml');
  4.  
  5.  
  6. $movies = new SimpleXMLElement($xml);
  7.  
  8.  
  9. echo $movies->movie->plot;
  10.  
  11.  
  12. ?>


Napisze mi ktoś co robię źle ? Nic się nie pokazuje.

Ten post edytował skine 28.05.2014, 16:24:35
Go to the top of the page
+Quote Post
Pyton_000
post 28.05.2014, 17:46:07
Post #13





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


Jak już robisz kopiuj/wklej to rób to dokładnie. Porównaj swój kod z tym z przykładu
Go to the top of the page
+Quote Post
skine
post 28.05.2014, 22:00:33
Post #14





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 26.05.2014

Ostrzeżenie: (0%)
-----


ok mam

Ten post edytował skine 28.05.2014, 22:11:26
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 28.03.2024 - 11:56