Mahmoud Ismail
Mahmoud Ismail

Reputation: 1617

How i can get first child element from xml?

How i can get first child element from xml in PHP my code :

    $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 Code :

<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>

So i want to get only first element how i can do that , any suggestion ?

Upvotes: 4

Views: 10745

Answers (3)

ThW
ThW

Reputation: 19502

In SimpleXML:

$root = new SimpleXMLElement($xml);

$firstElementChild = $root->children()[0];

Or with Xpath:

$root = new SimpleXMLElement($xml);

$firstElementChild = $root->xpath('./*[1]')[0];

In DOM Level 2 (That is implemented in PHP ext/dom.)

$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);
$context = $document->documentElement;

$firstElementChild = $xpath->evaluate('./*[1]', $context)[0];

The first approach uses that SimpleXML sees the DOM as a tree of nested elements. The other two use Xpath to fetch all element children and limit the result list to 1, then fetch the first element (index 0) from the result list.

In DOM Level 3 nodes with children have a firstElementChild property.

Upvotes: 3

IMSoP
IMSoP

Reputation: 97718

Unless I'm misunderstanding the question (which is possible, you weren't very clear), you have a loop over all child elements:

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

To change this to just access the first child element is really trivial:

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

Upvotes: 4

Marcel
Marcel

Reputation: 5119

It 's simple with PHP 's own DomDocument.

$oXML = (new DomDocument())->load('path/to/your/file.xml');
$oElement = $oXML->getElementsByTagName('urun');

After that $oElement is a DOMNodeList object over which you can iterate.

To get the content:

$oElement->item(0)->nodeValue;

Obviously there is no content in the urun node. Only child nodes. In the DomNodeList $oElement includes all the content of the urun node. You can iterate over it. You can search for other nodes in it. It contains all the childnodes urun got.

Upvotes: -1

Related Questions