其他分享
首页 > 其他分享> > 我如何从xml获取第一个子元素?

我如何从xml获取第一个子元素?

作者:互联网

我如何从PHP中的xml中获取第一个子元素:

    $xmlPath = 'www.example.com/file.xml';
    $fileContents= file_get_contents($xmlPath);

    $fileContents = str_replace(array("\n", "\r", "\t", "<br>"), '', $fileContents);
      $fileContents = trim(str_replace('"', "'", $fileContents));
      $simpleXml = simplexml_load_string($fileContents);
      $xmlArray = array();
      foreach($simpleXml->children() as $child) {
        $xmlArray [] = array(
          'parent' => $child->getName(),
        );
        foreach($child->children() as $subchild) {
          $xmlArray[] = array(
            'child' => $subchild->getName()
          );
        }
      }

XML代码:

<root>
<urun>
<UrunID>9728</UrunID>
<Kod>3697-844 BORDO</Kod>
<Baslik>İRONİ KRUVAZE BORDO EKOSELİ ŞİFON BLUZ</Baslik>
<Renk>BORDO</Renk>
<Aciklama>Kumaş Bilgisi: %100 POLYESTER<br><br>Manken Ölçüleri: Göğüs : 85, Bel : 61, Kalça : 89, Boy:177<br><br>Mankenin Giydiği Beden: S/36</Aciklama>
<Durum>1</Durum>
<mainCategory>Kadın Giyim</mainCategory>
<subCategory>Bluz</subCategory>
<Marka>İroni</Marka>
<kdvHaricFiyat>9.17</kdvHaricFiyat>
<kdvDahilFiyat>9.90</kdvDahilFiyat>
<kdvDahilAlisFiyat>6.44</kdvDahilAlisFiyat>
<fiyatparabirimi>TL</fiyatparabirimi>
<KDV>8</KDV>
<link>
http://www.ironitekstil.com/urun/ironi-kruvaze-bordo-ekoseli-sifon-bluz_9728.aspx
</link>
<ImageName1>
http://www.ironitekstil.com/uploads/productsimages/3697-844-ironi-bordo-gomlek-1.jpg
</ImageName1>
<ImageName2>
http://www.ironitekstil.com/uploads/productsimages/3697-844-ironi-bordo-gomlek-3.jpg
</ImageName2>
<ImageName3>
http://www.ironitekstil.com/uploads/productsimages/3697-844-ironi-bordo-gomlek-2.jpg
</ImageName3>
<Kur>TL</Kur>
<StokAdedi>2</StokAdedi>
<Stoklar>
<stok>
<isim>BORDO</isim>
<deger>S</deger>
<barcode>1000000007077</barcode>
<miktar>0</miktar>
</stok>
<stok>
<isim>BORDO</isim>
<deger>XS</deger>
<barcode>1000000007619</barcode>
<miktar>2</miktar>
</stok>
<stok>
<isim>BORDO</isim>
<deger>M</deger>
<barcode>1000000007626</barcode>
<miktar>0</miktar>
</stok>
<stok>
<isim>BORDO</isim>
<deger>L</deger>
<barcode>1000000007633</barcode>
<miktar>0</miktar>
</stok>
<stok>
<isim>BORDO</isim>
<deger>XL</deger>
<barcode>3543000015973</barcode>
<miktar>0</miktar>
</stok>
</Stoklar>
<yeniSezon/>
</urun>
<urun>
<UrunID>9728</UrunID>
<Kod>3697-844 BORDO</Kod>
<Baslik>İRONİ KRUVAZE BORDO EKOSELİ ŞİFON BLUZ</Baslik>
<Renk>BORDO</Renk>
<Aciklama>Kumaş Bilgisi: %100 POLYESTER<br><br>Manken Ölçüleri: Göğüs : 85, Bel : 61, Kalça : 89, Boy:177<br><br>Mankenin Giydiği Beden: S/36</Aciklama>
<Durum>1</Durum>
<mainCategory>Kadın Giyim</mainCategory>
<subCategory>Bluz</subCategory>
<Marka>İroni</Marka>
<kdvHaricFiyat>9.17</kdvHaricFiyat>
<kdvDahilFiyat>9.90</kdvDahilFiyat>
<kdvDahilAlisFiyat>6.44</kdvDahilAlisFiyat>
<fiyatparabirimi>TL</fiyatparabirimi>
<KDV>8</KDV>
<link>
http://www.ironitekstil.com/urun/ironi-kruvaze-bordo-ekoseli-sifon-bluz_9728.aspx
</link>
<ImageName1>
http://www.ironitekstil.com/uploads/productsimages/3697-844-ironi-bordo-gomlek-1.jpg
</ImageName1>
<ImageName2>
http://www.ironitekstil.com/uploads/productsimages/3697-844-ironi-bordo-gomlek-3.jpg
</ImageName2>
<ImageName3>
http://www.ironitekstil.com/uploads/productsimages/3697-844-ironi-bordo-gomlek-2.jpg
</ImageName3>
<Kur>TL</Kur>
<StokAdedi>2</StokAdedi>
<Stoklar>
<stok>
<isim>BORDO</isim>
<deger>S</deger>
<barcode>1000000007077</barcode>
<miktar>0</miktar>
</stok>
<stok>
<isim>BORDO</isim>
<deger>XS</deger>
<barcode>1000000007619</barcode>
<miktar>2</miktar>
</stok>
<stok>
<isim>BORDO</isim>
<deger>M</deger>
<barcode>1000000007626</barcode>
<miktar>0</miktar>
</stok>
<stok>
<isim>BORDO</isim>
<deger>L</deger>
<barcode>1000000007633</barcode>
<miktar>0</miktar>
</stok>
<stok>
<isim>BORDO</isim>
<deger>XL</deger>
<barcode>3543000015973</barcode>
<miktar>0</miktar>
</stok>
</Stoklar>
<yeniSezon/>
</urun>
</root>

所以,我只想获得第一个要素,我有什么建议吗?

解决方法:

除非我对这个问题有误解(可能是,您不太清楚),否则所有子元素都有一个循环:

foreach($simpleXml->children() as $child) {

将其更改为仅访问第一个子元素确实很简单:

$children = $simpleXml->children();
$first_child = $children[0];

标签:children,simplexml,simplexml-load-string,xml,php
来源: https://codeday.me/bug/20191111/2020746.html