Cheap Web Hosting for Developers

PHP, MySQL, Java, Unix Cheap Web Hosting

CHAPTER 7 SIMPLEXML 257 When an attribute

Filed under: PHP and XML — webmaster @ 21:30

CHAPTER 7 SIMPLEXML 257 When an attribute is being written to and does not exist on the element, the attribute is automatically created with the content specified by the string to which it is being set. In this instance, the firstname element originally contained no attributes. Writing the string Mr. to an attribute named prefix, the prefix attribute was created on the firstname element and its value set to the value of the supplied string. Removing Attributes Again, you can use the function unset() to remove attributes from elements, just as you remove elements from the tree: $book = simplexml_load_file(’sxml.xml’); $book->bookinfo->author->firstname[”prefix”] = “Mr.”; print $book->bookinfo->author->firstname->asXML().”nn”; unset($book->bookinfo->author->firstname[”prefix”]); print $book->bookinfo->author->firstname->asXML(); Rob Rob After adding the prefix attribute and printing the XML data from the firstname element, the code continues, removes this newly added attribute, and again prints the updated XML data from the firstnameelement. Extending the SimpleXMLElement Class You can extend the SimpleXMLElement class just as you would any other class: class mySXE extends SimpleXMLElement { function appendChild($name, $content) { $dom = dom_import_simplexml($this); $dom->appendChild($dom->ownerDocument->createElement($name, $content)); } } A big difference with extended classes in SimpleXML from those in the DOM extension is that once an object using the extended class has been instantiated, all objects returned from the SimpleXML methods will use the extended class type. Looking at the class definition, you can see that the method appendChild() has been added. This allows for an easy way to append child nodes in SimpleXML: $sxe = new mySXE(”“); $sxe->node1->appendChild(”node2″, “content”); print $sxe->asXML(); content

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Clan Web Hosting services

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Sorry, the comment form is closed at this time.

Powered by Cheap Web Hosting