我如何在xml
我的代码中获取PHP
中的第一个子元素:
$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>
所以我想只获得第一个元素,我可以做什么,任何建议?
答案 0 :(得分:2)
除非我误解了这个问题(这是可能的,你不是很清楚),否则你对所有子元素都有一个循环:
foreach($simpleXml->children() as $child) {
要将此更改为只访问第一个子元素,实际上是微不足道的:
$children = $simpleXml->children();
$first_child = $children[0];
答案 1 :(得分:1)
在SimpleXML中:
ID EffectiveDate Enddate DATEGROUP
----------- ----------------------- ----------------------- -----------
1 2017-01-01 00:00:00.000 2017-01-11 00:00:00.000 1
1 2017-01-02 00:00:00.000 2017-01-05 00:00:00.000 1
1 2017-01-03 00:00:00.000 2017-01-12 00:00:00.000 1
1 2017-01-06 00:00:00.000 2017-01-09 00:00:00.000 1
1 2017-01-13 00:00:00.000 2017-01-19 00:00:00.000 2
2 2017-02-01 00:00:00.000 2017-02-11 00:00:00.000 3
2 2017-02-06 00:00:00.000 2017-02-16 00:00:00.000 3
或使用Xpath:
$root = new SimpleXMLElement($xml);
$firstElementChild = $root->children()[0];
在DOM Level 2中(在PHP ext / dom中实现。)
$root = new SimpleXMLElement($xml);
$firstElementChild = $root->xpath('./*[1]')[0];
第一种方法使用SimpleXML将DOM视为嵌套元素树。另外两个使用Xpath来获取所有元素子节点并将结果列表限制为1,然后从结果列表中获取第一个元素(索引0)。
在DOM Level 3中,有子项的节点具有$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);
$context = $document->documentElement;
$firstElementChild = $xpath->evaluate('./*[1]', $context)[0];
属性。
答案 2 :(得分:0)
使用PHP自己的DomDocument很简单。
$oXML = (new DomDocument())->load('path/to/your/file.xml');
$oElement = $oXML->getElementsByTagName('urun');
之后,$ oElement是一个DOMNodeList对象,您可以在其上进行迭代。
获取内容:
$oElement->item(0)->nodeValue;
显然,urun节点中没有内容。只有子节点。在DomNodeList中,$ oElement包含urun节点的所有内容。你可以迭代它。您可以搜索其中的其他节点。它包含了urun得到的所有子节点。