SimpleXML, simple, but limited

A look at how to slightly adapt SimpleXML to be able to read the root element's name.

After playing with it for a while, I actually had some work this week which would be a good project for using PHP5's simpleXML functionality. I had to work with an existing standard which I had no control over, and which proved to become a bane of my existence though. The main problem of this standard was that the root element of the XML strings contained the type of the XML call that was being done. When you load an XML string into SimpleXML, though, you can not easily access the element name of the root element. Any attributes are easily accessed, but the root element itself is harder. So I decided to write a little SimpleXML extension. Or really, a simple class that would instantiate and return a SimpleXML document, but with a new dummy root element.
', '', $strXml);
        $strXml = '' . $strXml . '';
        return simplexml_load_string($strXml);
    }
}
?>
This is still version 0.1, there are definitely things that can be improved (such as introducing regular expressions in the xml tag to allow for other versions of xml and other encoding types) but for now this does the job.
Add comment

Comments

gravatar Marko: heh ,nice solution m8
October 27, 2005
gravatar Jonathan Stanley: Errm… can’t you just XPath for the root element?

http://www.zvon.org/xxl/XPathTutorial/General/examples.html
October 29, 2005
gravatar left: well, obviously, the root element is available in the first place, but can XPath also fetch the name of the element? if so, how? ;)
October 29, 2005
gravatar Steve: The solution to this is the getName() function.

echo "Root Element -> " . $strXml->getName() . "\r\n";

February 18, 2011
gravatar Time Warner Cable Packages: It is very effective article. It is well written and has information.
February 24, 2012
gravatar tilting patio umbrella: I'm still learning from you, however I'm improving myself. I definitely love studying every little thing that is written on your blog.Preserve the stories coming. I cherished it!
March 8, 2012
gravatar beats by dre: Ce site internet est sérieusement bonne, souhaitent déterminer beaucoup plus si merveilleux écriture jusqu'à

March 9, 2012
gravatar mississauga real estate agent: Dynamic web pages usually consist of a static part and a dynamic part, which is code that generates HTML. The code that generates the HTML can do this based on variables in a template, or on code. The text to be generated can come from a database, thereby making it possible to dramatically reduce the number of pages in a site. Thanks.
April 19, 2012

Php5_zce_logo

Upcoming events

I will be speaking 05-06-2012: Forum PHP
I will be attending 06-06-2012: Symfony Live Paris 2012
I will be attending 07-06-2012: Symfony Live Paris 2012
I will be attending 08-06-2012: Symfony Live Paris 2012
I will be attending 09-06-2012: Symfony Live Paris 2012
I will be attending 23-06-2012: PHP TestFest NL

Tags

1337 2008 2010 2011 4developers access modifiers accessibility AdaLovelaceDay09 advent agavi agile alfred amsterdam apache api apple article articles atk atkMetaNode audioscrobbler autoloading automation azure backwards compatibility barcelona barcodes bash bbc bbq beatstad belgium best practices bittorrent blogging blogs boards of canada book books bughuntday bundle caching cake cal evans calendar career cat cerf certificate cfp cilex clear cms cologne common sense communities community components composer conference conferences contest continuous integration contribute contribution crisis css curl custom d-day datetime DbFinderPlugin decorator decorators deployment deps devdays development directoryindex directoryiterator docblox doctrine documentation download dpc dpc09 dpc10 dpc11 DPC2008 dreamhost drupal dv7 eclipse ed editors efficiency enterprise errors event events expertise ezcomponents facebook filter-branch filteriterator finland flickr fork framework frameworks freelance freeze frontend fun game games geoip germany getting real git github globiterator gnome-do google google calendar googletalk graceful degradation hack hackers hidden gem hiphop howto hp HR html http i386 ibuildings icann ide ideasofmarch idm imovie inclusivity indy ingewikkeld integration international php conference internet interview ipad IPC ipc ipc08 ipc10 ipc11se iterators iterm2 javascript jenkins jenkins-php job job openings jobeet john peel joomla joomladays kiva kubuntu launcher launchy left on the web libcurl libraries library lighttpd lime linktuesday linux live london loudblog m2ts mac magazines malware mambo marjolein mediterra meeting meme meta methodology micro-financing microframework microsoft migration movie music mysql namespace namespaces netbeans netherlands newsfire nllgg northeastphp nos odmarco open source opinion ORM osx paradiso paris partnership pavilion pear pecl performance personal pfc10 pfc11 pfcongres pfcongrez pfz photo php PHP php5.3 phpabstract phpazure phpBB phpbb phpbelgium phpbenelux phpbnl10 phpday phpdoc phpdocumentor phpgg phpitalia phpnw phpnw08 phpnw11 phpstorm phptek phptek09 phpuk2009 phpUnderControl phpunit php|architect php|tek podcast politics portability postcrossing presentation presentations private projects protected prototype PSR-0 public python qa qr codes re2c recruiting refactoring review rewrite ruby on rails san francisco schedule scifi script security sensio seven things sfdaycgn sflive2011 shell scripting silex simplexml slides smfony software sogeti solar sound speakers spl ssh standard standards star trek static steer strings stylesheets subversion symfony symfony live Symfony2 symfonycamp symfonyday symfonylive symfonyUnderControlPlugin talk talks tech techademy technology techportal tek09 telecommuting terratec terrorism testfest testing textmate textpattern the right tool timeout tips tld todo tomas tools training twig uncon unet usability usergroup validation vhost video vim vinyl virus warp webinar weblogging webservices wiki windows winphp women wordpress work workshop world world of warcraft wpi writing wunderlist xml xpath xsd yara year youtube zc11 ZCE zemanta zend zend framework zend server zend studio zendcon Zend_Form zite
© 2004 - 2012 Stefan Koopmanschap + Powered by Symfony, photos powered by Flickr, links powered by Delicious, Shanghai smilies by Iconbuffet. Feeds: rss / atom. Left on the Web v4.4.0.1