作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我如何在 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/
我是一名优秀的程序员,十分优秀!