Bu yazımızda ağırlıklı olarak api kaynaklarından veri çekerken kullanılan xml dosyalarını php ile nasıl okuyacağımıza değineceğiz.
SimpleXml php ile xml verilerini okumamızı sağlayan basit ama yararlı bir uzantıdır.
SimpleXml tree yani ağaç tabanlı bir ayrıştırıcıdır.
SimpleXml ile eğer bir dosyanına yapısını ve içeriğinin düzenini biliyorsanız kolayca okuma yapabilirsiniz.
SimpleXml, dosyanızın içeriğini nesne veya dizi formatına çevirir.
simplexml_load_string() metodu ile bir değişkendeki xml metnini okuyabiliriz.
Temel kullanımı şu şekildedir.
<?php
$xml = '<kisi>
<isim>Zafer Yıldız</isim>
<meslek>Bilgisayar Mühendisi</meslek>
<yas>26</yas>
</kisi>';
$xml_oku=simplexml_load_string($xml) or die("Değişken yok");
print_r($xml_oku);
?>
Bir değişkenin içerisinde birden fazla değer tutulmuş olabilir. Bunun için kapsayıcı sınıf içerisine verileri yazarız.
<?php
$xml = '<?xml version="1.0"?>
<liste>
<kisi>
<isim>Zafer Yıldız</isim>
<meslek>Bilgisayar Mühendisi</meslek>
<yas>26</yas>
</kisi>
<kisi>
<isim>Hasan Yıldız</isim>
<meslek>Fındık Ağası</meslek>
<yas>26</yas>
</kisi>
</liste>
';
$xml_oku=simplexml_load_string($xml) or die("Değişken yok");
print_r($xml_oku);
?>
Yukarıdaki kodu çalıştırdığımızda ekranda şunlar yazacaktır.
SimpleXMLElement Object ( [kisi] => Array ( [0] => SimpleXMLElement Object ( [isim] => Zafer Yıldız [meslek] => Bilgisayar Mühendisi [yas] => 26 ) [1] => SimpleXMLElement Object ( [isim] => Hasan Yıldız [meslek] => Fındık Ağası [yas] => 26 ) ) )
Eğer Xml dosyamızda hata varsa bunu libxml_get_errors() metodu ile yakalayabiliriz.
<?php
libxml_use_internal_errors(true);
$xml_metni = '<?xml version="1.0"?>
<liste>
<kisi>
<isim>Zafer Yıldız</isim>
<meslek>Bilgisayar Mühendisi</meslek>
<yas>26</yas>
</kisi>
<kisi>
<isims>Hasan Yıldız</isim>
<meslek>Fındık Ağası</meslek>
<yas>26</yas>
</kisi>
</liste>
';
$xml = simplexml_load_string($xml_metni);
if ($xml === false) {
echo "Failed loading XML: ";
foreach(libxml_get_errors() as $error) {
echo "<br>", $error->message;
}
} else {
print_r($xml_metni);
}
?>
isim etiketini isims olarak değiştirdik dosya yanlış olduğu için şu hatayı verecektir:
Failed loading XML:
Opening and ending tag mismatch: isims line 9 and isim
simplexml_load_file() metodu ile dosyadan xml okuyabiliriz.
egitim.xml dosyamız:
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<book id="bk103">
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology
society in England, the young survivors lay the
foundation for a new society.</description>
</book>
<book id="bk104">
<author>Corets, Eva</author>
<title>Oberon's Legacy</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2001-03-10</publish_date>
<description>In post-apocalypse England, the mysterious
agent known only as Oberon helps to create a new life
for the inhabitants of London. Sequel to Maeve
Ascendant.</description>
</book>
</catalog>
index.php dosyamız
Yukarıdaki projeyi kaydedip çalıştırdığımızda ekranda şu şekil bir çıktı oluşturacaktır.
SimpleXMLElement Object ( [book] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => bk101 ) [author] => Gambardella, Matthew [title] => XML Developer's Guide [genre] => Computer [price] => 44.95 [publish_date] => 2000-10-01 [description] => An in-depth look at creating applications with XML. ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => bk102 ) [author] => Ralls, Kim [title] => Midnight Rain [genre] => Fantasy [price] => 5.95 [publish_date] => 2000-12-16 [description] => A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world. ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => bk103 ) [author] => Corets, Eva [title] => Maeve Ascendant [genre] => Fantasy [price] => 5.95 [publish_date] => 2000-11-17 [description] => After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society. ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => bk104 ) [author] => Corets, Eva [title] => Oberon's Legacy [genre] => Fantasy [price] => 5.95 [publish_date] => 2001-03-10 [description] => In post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for the inhabitants of London. Sequel to Maeve Ascendant. ) ) )
Hocam Merhaba, En son örnekte index.php dosyasının görüntüsü yok yardımcı olabilir misiniz?
Muhammed Fatih BAĞCIVAN
19-May-2021Ellerinize emeğinize sağlık hocam 😊. Gayet güzel açıklayıcı ve sade bir anlatım olmuş. Çok teşekkür ediyorum