Php5 İle Xml Dosya Oluşturmak

İngilizce bir makaledir.
Php5 İle Xml Dosya OluşturmakXML Review
As a (very) quick reminder about what XML is, you work with tags much like you do in HTML, but with two very important distinctions:
1.(restriction) You must close every tag
2.(benefit) You get to define the name of these tags in your XML document
In other words, what you are doing is creating a “tree” of XML entities where you are defining the data structure for the objects you want to manage (or allow others to manage).
By way of example, you might have the following XML file represent the teams in Major League Baseball.
<?xml version=”1.0″ encoding=”iso-8859-1″?>
<teams>
<team>
<name>Atlanta Braves</name>
<stadium>Turner Field</stadium>
<league>National</league>
</team>
<team>
<name>Chicago Cubs</name>
<stadium>Wrigley Field</stadium>
<league>National</league>
</team>

<team>
<name>Baltimore Orioles</name>
<stadium>Camden Yards</stadium>
<league>American</league>
</team>

</teams>
Creating XML in DOM
The general workflow for creating an XML document using DOM is (where Steps 2 and 3 are repeated to complete the XML):
1.Create a new DOM document
2.Create elements
3.Append children
In our example (considering only the first two teams), the code would be:
<?php
// xml_teams_dom.php
// create the new XML document
$dom = new DOMDocument(‘1.0’, ‘iso-8859-1’);
// create the root element
$list_of_teams = $dom->createElement(‘teams’);
$dom->appendChild($list_of_teams);
// create the first team element
$team = $dom->createElement(‘team’);
$list_of_teams->appendChild($team);
// now create all the subelements for the team
$name = $team->appendChild($dom->createElement(‘name’));
$name->appendChild($dom->createTextNode(‘Atlanta Braves’));
$stadium = $team->appendChild($dom->createElement(‘stadium’));
$stadium->appendChild($dom->createTextNode(‘Turner Field’));
$league = $team->appendChild($dom->createElement(‘league’));
$league->appendChild($dom->createTextNode(‘National’));
// create the second team element
$team = $dom->createElement(‘team’);
$list_of_teams->appendChild($team);
// now create all the subelements for the second team
$name = $team->appendChild($dom->createElement(‘name’));
$name->appendChild($dom->createTextNode(‘Chicago Cubs’));
$stadium = $team->appendChild($dom->createElement(‘stadium’));
$stadium->appendChild($dom->createTextNode(‘Wrigley Field’));
$league = $team->appendChild($dom->createElement(‘league’));
$league->appendChild($dom->createTextNode(‘National’));
$xml_result = $dom->saveXML();
// simple mechanism to see the XML
print <<<XML_SHOW
<textarea cols=’80’ rows=’40’ name=’xml_result’>$xml_result</textarea>
XML_SHOW;
?>
XML Parsing
Now that we have an XML structure, how exactly do we interrogate it for information?
The general workflow for parsing an XML document for specific elements is:
1.Create a new DOM document
2.Load an XML tree into the DOM document
3.Query the new XML tree and loop through the desired elements
In our case, we might add the following code to our script:
<?php
// pull in the work we’ve just done
$team_xml = new DOMDocument();
$team_xml->loadXML($xml_result);
// statement block to get at the elements of the xml document
$team_list = $team_xml->getElementsByTagName(‘team’);
foreach ($team_list AS $team) {
foreach ($team->childNodes AS $item) {
print $item->nodeName . ” = ” . $item->nodeValue . “<br />”;
}
}
?>
This obviously only scratches the surface of using XML in PHP 5. XML is an extremely important tool in PHP (and everywhere else), particularly in the development of web services.
If you need to create XML files in PHP, you can do it without having to create the tags yourself with strings. This code shows you how to use the new_child functions to create an XML file.
<?
$doc = new_xmldoc(‘1.0’);
$root = $doc->add_root(‘members’);
$member = $root->new_child(‘member’,”);
$member->new_child(‘lastName’,’John’);
$member->new_child(‘firstName’,’Adams’);
$member->new_child(‘contribution’,’3400′);
$member = $root->new_child(‘member’,”);
$member->new_child(‘lastName’,’Debra’);
$member->new_child(‘firstName’,’Hones’);
$member->new_child(‘contribution’,’2400′);
$member = $root->new_child(‘member’,”);
$member->new_child(‘lastName’,’Jake’);
$member->new_child(‘firstName’,’Tudor’);
$member->new_child(‘contribution’,’1200′);
$fp = @fopen(‘members.xml’,’w’);
if(!$fp) {
die(‘Error cannot create XML file’);
}
fwrite($fp, $doc->dumpmem());
fclose($fp);
?>
would create the following XML file.
<members>
<member>
<lastName>John</lastName>
<firstName>Adams</firstName>
<contribution>3400</contribution>
</member>
<member>
<lastName>Debra</lastName>
<firstName>Hones</firstName>
<contribution>2400</contribution>
</member>
<member>
<lastName>Jake</lastName>
<firstName>Tudor</firstName>
<contribution>1200</contribution>
</member>
</members>
Standart olmayan başka bir yöntem
<?php
$dosya_adi = “deneme.xml”; // Oluşturulacak Dosya Adı
$host = “localhost”; //mysql adresiniz
$user = “kullaniciadi”; //veritabanı kullanıcı adı
$pass = “sifre”; //veritabanı kullanıcı şifresi
$database = “veritabani_adi”; //veritabanı adı
$linkID = mysql_connect($host, $user, $pass) or die(“Veritabanına bağlanılamadı.”); //bağlantı kuruluyor
mysql_select_db($database, $linkID) or die(“Veritabanı bulunamadı.”);
$resultID = mysql_query($query, $linkID) or die(“Veri bulunamadı.”);
$xml_output = “<?xml version=”1.0″ encoding=”iso-8859-9″?>n”;
//xml dil kodlaması belirleniyor veritabanı karakter setiyle aynı olmalı,aksi halde web sayfasında görüntülerken hata alırsınız.
$xml_output .= “<Ogrenciler>n”; //ilk xml etiketiyle başlığı açıyoruz
for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
$row = mysql_fetch_assoc($resultID); //Veritabanında kaç satır olduğunu öğrenerek tüm satırlar için işlem yapmasını istediğimizi belirtiyoruz.
$xml_output .= “tt<isim>”. $row[‘isim’] .”</isim>n”; //isim etiketinin içine veritabanındaki isim alanının verilerini doldurtuyoruz.
$xml_output .= “tt<soyisim>”. $row[‘soy_isim’] .”</soyisim>n”; //aynı şekilde soy_isim alanı için
$xml_output .= “tt<okulno>”. $row[‘okul_no’] .”</okulno>n”; //aynı şekilde okul_no alanı için
}
$xml_output .= “</Ogrenciler>”; // başta açtığımız etiketi kapatıyoruz.
$yaz=fopen($dosya_adi, “w”); //en başta belirlediğimiz isimde bir dosya ortaya çıkartıyoruz.
fwrite($yaz,$xml_output); //veritabanından gelen bilgileri açtığımız dosyaya yazdırıyoruz.
fclose($yaz); //yazma işlemini kapatıyoruz.
?>
Bu yapıyı geliştirerek mysql veritabanınızdaki datalarınızı xml standartlarına dönüştürebilirsiniz.
Oluşması Gereken Sonuç :
<?xml version=&#8221;1.0&#8243; encoding=&#8221;iso-8859-9&#8243;?>
<Ogrenciler>
<isim>Suphi Özgür</isim>
<soyisim>CANKURT</soyisim>
<okulno>2405010033</okulno>
</Ogrenciler>
Kaynak weblog.bignerdranch.com/ , www.developerfusion.co.uk/show/3944/ , www.seoyarismasi.net/php-mysql-to-xml/