Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP][XML] Dwa tagi root
Forum PHP.pl > Forum > Przedszkole
Puchatek
Witajcie, natknąłem się na ciekawy problem.
Mam do przygotowania plik XML o następującej strukturze:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <import>
  3. <offers>
  4. <offer>
  5. <sku>123</sku>
  6. <product-id>0000000000000</product-id>
  7. <product-id-type>EAN</product-id-type>
  8. <price>170.1</price>
  9. <quantity>2</quantity>
  10. <state>11</state>
  11. <logistic-class>1</logistic-class>
  12. <leadtime-to-ship>2</leadtime-to-ship>
  13. </offer>
  14. <offer>
  15. <sku>123</sku>
  16. <product-id>0000000000000</product-id>
  17. <product-id-type>EAN</product-id-type>
  18. <price>170.1</price>
  19. <quantity>2</quantity>
  20. <state>11</state>
  21. <logistic-class>1</logistic-class>
  22. <leadtime-to-ship>2</leadtime-to-ship>
  23. </offer>
  24. </offers>
  25. </import>


Do utworzenia pliku używam SimpleXML. Problemem jest stworzenie jednocześnie tagu <import> oraz <offers>.
Gdy próbuję zrobić to w ten sposób:
  1. $xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><import></import>');
  2. $xml = $xml->addChild('offers');
  3. $shopitem = $xml->addChild('offer');
  4. /*Tutaj dodaję pozostałe tagi*/

Otrzymuję XML w postaci:
  1. <offers>
  2. <offer>
  3. <sku>123</sku>
  4. <product-id>0000000000000</product-id>
  5. <product-id-type>EAN</product-id-type>
  6. <price>170.1</price>
  7. <quantity>2</quantity>
  8. <state>11</state>
  9. <logistic-class>1</logistic-class>
  10. <leadtime-to-ship>2</leadtime-to-ship>
  11. </offer>
  12. <offer>
  13. <sku>123</sku>
  14. <product-id>0000000000000</product-id>
  15. <product-id-type>EAN</product-id-type>
  16. <price>170.1</price>
  17. <quantity>2</quantity>
  18. <state>11</state>
  19. <logistic-class>1</logistic-class>
  20. <leadtime-to-ship>2</leadtime-to-ship>
  21. </offer>
  22. </offers>


Ginie mi w tym przypadku zewnętrzny tag <import>
Próbowałem również użyć tego sposobu:
  1. $xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><import><offers></offers></import>');

Bez skutku, otrzymuję strukturę w tej postaci:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <import>
  3. <offers/>
  4. <offer>
  5. <sku>123</sku>
  6. <product-id>0000000000000</product-id>
  7. <product-id-type>EAN</product-id-type>
  8. <price>170.1</price>
  9. <quantity>2</quantity>
  10. <state>11</state>
  11. <logistic-class>1</logistic-class>
  12. <leadtime-to-ship>2</leadtime-to-ship>
  13. </offer>
  14. </import>



Jakieś pomysły?
trueblue
Tag <offer> należy dodać do <offers>, a nie do root.
Puchatek
Głupie, kuriozalne niedopatrzenie.
  1. $xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><import></import>');
  2. $xml = $xml->addChild('offers');

oraz
  1. $xml->asXML('plik.xml');

ze startu eliminowało root.

Przepraszam za zamieszanie smile.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.