gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 00:56:06 25 4
gpt4 key购买 nike

我如何在 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];

关于php - 我如何从 xml 中获取第一个子元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42993678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com