<?php try { /*** a new dom object ***/ $dom = new domDocument; /*** make the output tidy ***/ $dom->formatOutput = true; /*** create the root element ***/ $root = $dom->appendChild($dom->createElement( "user" )); /*** create the simple xml element ***/ $sxe = simplexml_import_dom( $dom ); /*** add a firstname element ***/ $sxe->addChild("firstname", "John"); /*** add a surname element ***/ $sxe->addChild("surname", "Brady"); /*** echo the xml ***/ echo $sxe->asXML(); } catch( Exception $e ) { echo $e->getMessage(); } ?>上式所產生的XML樹如下
<?xml version="1.0"?> <user> <firstname>John</firstname> <surname>Brady</surname> </user>上例完成後再來思考一個問題,如果我們再子樹下還要建立子樹該怎麼做呢,請看下例。
<?php try { /*** a new dom object ***/ $dom = new domDocument; /*** make the output tidy ***/ $dom->formatOutput = true; /*** create the root element ***/ $root = $dom->appendChild($dom->createElement( "users" )); /*** create the simple xml element ***/ $sxe = simplexml_import_dom( $dom ); /*** add a user node ***/ $user = $sxe->addchild("user"); /*** add a firstname element ***/ $user->addChild("firstname", "John"); /*** add a surname element ***/ $user->addChild("surname", "Brady"); /*** add address element ***/ $user->addChild("address", "1 Bunch St"); /*** add the city element ***/ $user->addChild("city", "Downtown"); /*** add the country ***/ $user->addChild("country", "America"); $contact = $user->addChild("contact"); $contact->addChild("phone", "4444 4444"); $contact->addChild("url", "http://phpro.org"); $contact->addChild("email", "brady@bunch.example.com"); echo $sxe->asXML(); } catch( Exception $e ) { echo $e->getMessage(); } ?>上式所得之結果為
<?xml version="1.0"?> <users> <user> <firstname>John</firstname> <surname>Brady</surname> <address>1 Bunch St</address> <city>Downtown</city> <country>America</country> <contact> <phone>4444 4444</phone> <url>http://phpro.org</url> <email>brady@bunch.example.com</email> </contact> </user> </users>再來一個範例說明如何為NODE增加屬性
<?php try { /*** a new dom object ***/ $dom = new domDocument; /*** make the output tidy ***/ $dom->formatOutput = true; /*** create the root element ***/ $root = $dom->appendChild($dom->createElement( "users" )); /*** create the simple xml element ***/ $sxe = simplexml_import_dom( $dom ); /*** add a user node ***/ $user = $sxe->addchild("user"); /*** add a firstname element ***/ $user->addChild("firstname", "John"); /*** add a surname element ***/ $user->addChild("surname", "Brady"); /*** add address element ***/ $user->addChild("address", "1 Bunch St"); /*** add the city element ***/ $user->addChild("city", "Downtown"); /*** add the country ***/ $user->addChild("country", "America"); /*** add the contact element ***/ $contact = $user->addChild("contact"); /*** add children to the contact element ***/ $phone = $contact->addChild("phone", "4444 4444"); /*** add an attribute to the phone element ***/ $phone->addAttribute("type", "mobile"); /*** more children for the contact element ***/ $contact->addChild("url", "http://phpro.org"); $contact->addChild("email", "brady@bunch.example.com"); /*** show the xml ***/ echo $sxe->asXML(); } catch( Exception $e ) { echo $e->getMessage(); } ?>上式所得之結果為
<?xml version="1.0"?> <users> <user> <firstname>John</firstname> <surname>Brady</surname> <address>1 Bunch St</address> <city>Downtown</city> <country>America</country> <contact> <phone type="mobile">4444 4444</phone> <url>http://phpro.org</url> <email>brady@bunch.example.com</email> </contact> </user> </users>上面我們已經做過了取單一NODE的方法,下面用FOREACH來取得所有NODE PHONE部分之屬性
<?xml version="1.0" encoding="utf-8"?> <users> <user> <firstname>Sheila</firstname> <surname>Green</surname> <address>2 Good St</address> <city>Campbelltown</city> <country>Australia</country> <contact> <phone type="mobile">1234 1234</phone> <url>http://example.com</url> <email>pamela@example.com</email> </contact> </user> <user> <firstname>Bruce</firstname> <surname>Smith</surname> <address>1 Yakka St</address> <city>Meekatharra</city> <country>Australia</country> <contact> <phone type="landline">4444 4444</phone> <url>http://yakka.example.com</url> <email>bruce@yakka.example.com</email> </contact> </user> <user> <firstname>Davo</firstname> <surname>White</surname> <address>The Only Way</address> <city>Whitefall</city> <country>Australia</country> <contact> <phone type="mobile">8888 8888</phone> <url>http://white.example.com</url> <email>davo@white.example.com</email> </contact> </user> <user> <firstname>Shazza</firstname> <surname>Green</surname> <address>222 Great Western Hwy</address> <city>Bathurst</city> <country>Australia</country> <contact> <phone type="fax">6666 6666</phone> <url>http://shazza.green.example.com</url> <email>shazza@green.example.com</email> </contact> </user> </users>
<?php /*** create a SimpleXML object ***/ if( ! $xml = simplexml_load_file("address.xml") ) { echo "Unable to load XML file"; } else { $i = 0; /*** loop over the elements ***/ foreach($xml as $node) { echo $xml->user[$i]->contact->phone->attributes().'<br />'; $i++; } } ?>得到之結果為
mobilel
andline
mobile
fax
下一頁
update : 2010/3/25
沒有留言:
張貼留言