Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of impleXML for XML Operation extended by PHP

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is to share with you the content of a sample analysis of impleXML for XML operations of PHP extensions. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Introduction and installation

The SimpleXML extension provides a very simple and easy-to-use toolset that converts XML into an object with a generic property selector and array iterator.

This extension requires a libxml PHP extension. This means that you need to use-- enable-libxml, although this will be done implicitly because libxml is enabled by default.

PHP5 and above are required.

This extension is enabled by default and can be disabled at compile time with the following options:-- disable-simplexml

Second, the basic use of the example

Example # 1 Include file example.php with XML string

PHP: Behind the ParserMs. CoderOnlivia ActoraMr. CoderEl Act rSo, this language. It's like, a programming language. Or is it ascripting language? All is revealed in this thrilling horror spoofof a documentary.PHP solves all my web problems75XML;? >

The simplicity of SimpleXML appears most clearly when one extracts a string or number from a basic XML document.

Example # 2 Getting

The above routine outputs:

So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary.

Accessing elements within an XML document that contain characters not permitted under PHP's naming convention (e.g. The hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.

Example # 3 Getting

The above routine outputs:

PHP solves all my web problems

Example # 4 Accessing non-unique elements in SimpleXML

When multiple instances of an element exist as children of a single parent element, normal iteration techniques apply.

The above routine outputs:

Ms. Coder played by Onlivia ActoraMr. Coder played by El Act interface r

Note:

Properties ($movies- > movie in previous example) are not arrays. They are iterable and accessible objects.

Example # 5 Using attributes

So far, we have only covered the work of reading element names and their values. SimpleXML can also access element attributes. Access attributes of an element just as you would elements of an array.

The above routine outputs:

7 thumbs up5 stars

Example # 6 Comparing Elements and Attributes with Text

To compare an element or attribute with a string or pass it into a function that requires a string, you must cast it to a string using (string). Otherwise, PHP treats the element as an object.

The above routine outputs:

My favorite movie.PHP: Behind the Parser

Example # 7 Comparing Two Elements

Two SimpleXMLElements are considered different even if they point to the same element since PHP 5.2.0.

The above routine outputs:

Bool (false)

Example # 8 Using XPath

SimpleXML includes built-in support. To find all elements:

'/ /' serves as a wildcard. To specify absolute paths, omit one of the slashes.

The above routine outputs:

Ms. Coder played by Onlivia ActoraMr. Coder played by El Act interface r

Example # 9 Setting values

Data in SimpleXML doesn't have to be constant. The object allows for manipulation of all of its elements.

The above routine outputs:

PHP: Behind the Parser Miss Coder Onlivia Actora Mr. Coder El Act interface r So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. PHP solves all my web problems 7 5

Example # 10 Adding elements and attributes

Since PHP 5.1.3, SimpleXML has had the ability to easily add children and attributes.

The above routine outputs:

PHP: Behind the Parser Ms. Coder Onlivia Actora Mr. Coder El Act interface r Mr. ParserJohn Doe So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. PHP solves all my web problems 7 5 PG

Example # 11 DOM Interoperability

PHP has a mechanism to convert XML nodes between SimpleXML and DOM formats. This example shows how one might change a DOM element to SimpleXML.

The above routine outputs:

Blah handles XML error

Example # 1 Loading broken XML string

The above routine outputs:

Failed loading XML Blank needed here parsing XML declaration:'? > 'expected Opening and ending tag mismatch: xml line 1 and broken Premature end of data in tag broken line 1 III. Related functions

Simplexml_import_dom-Get a SimpleXMLElement object from a DOM node.

Simplexml_load_file-Interprets an XML file into an object

Simplexml_load_string-Interprets a string of XML into an object

4. Related classes and member functions

SimpleXMLElement class

SimpleXMLElement::addAttribute-Adds an attribute to the SimpleXML element

SimpleXMLElement::addChild-Adds a child element to the XML node

SimpleXMLElement::asXML-Return a well-formed XML string based on SimpleXML element

SimpleXMLElement::attributes-Identifies an element's attributes

SimpleXMLElement::children-Finds children of given node

SimpleXMLElement::__construct-Creates a new SimpleXMLElement object

SimpleXMLElement::count-Counts the children of an element

SimpleXMLElement::getDocNamespaces-Returns namespaces declared in document

SimpleXMLElement::getName-Gets the name of the XML element

SimpleXMLElement::getNamespaces-Returns namespaces used in document

SimpleXMLElement::registerXPathNamespace-Creates a prefix/ns context for the next XPath query

SimpleXMLElement::saveXML-alias SimpleXMLElement::asXML

SimpleXMLElement::__toString-Returns the string content

SimpleXMLElement::xpath-Runs XPath query on XML data

SimpleXMLIterator class

SimpleXMLIterator::current-Returns the current element

SimpleXMLIterator::getChildren-Returns the sub-elements of the current element

SimpleXMLIterator::hasChildren-Checks whether the current element has sub elements.

SimpleXMLIterator::key-Return current key

SimpleXMLIterator::next-Move to next element

SimpleXMLIterator::rewind-Rewind to the first element

SimpleXMLIterator::valid-Check whether the current element is valid

Thank you for reading! This is the end of this article on "sample Analysis of XML Operation of PHP extension". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report