SimpleXML PHP Parsing XML method

Source: Internet
Author: User

SimpleXML is an easy-to-use XML toolset provided after PHP5 that transforms XML into an easy-to-handle object or organizes XML data to be generated. However, it does not apply to XML that contains namespace, and is guaranteed to be complete in XML format (well-formed). It provides three methods: Simplexml_import_dom, Simplexml_load_file, simplexml_load_string, and the function name is very intuitive to illustrate the function. All three functions return the SimpleXMLElement object, and the read/add of the data is done through the simplexmlelement operation.

$string = <<<xml<?xml version= ' 1.0 '?><document>  <cmd>login</cmd>  < login>imdonkey</login></document>xml; $xml = simplexml_load_string ($string);p rint_r ($xml); $login = $ Xml->login;//returned here is still a SimpleXMLElement object Print_r ($login); $login = (string) $xml->login;//when doing a data comparison, Note that the Print_r ($login) must be cast first;

The advantage of SimpleXML is that it is easy to develop, and the downside is that it will load the entire XML into memory before processing it, so it may be too weak to parse an XML document that is hyper-content. If you are reading small files, and the XML does not contain namespace, then SimpleXML is a good choice.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.