Cheap Web Hosting for Developers

PHP, MySQL, Java, Unix Cheap Web Hosting

CHAPTER 7 SIMPLEXML 261 The next example

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

CHAPTER 7 SIMPLEXML 261 The next example will query for the text node, which will be the content, of the firstname element: $book = simplexml_load_file(’sxml.xml’); $arAuthor = $book->xpath(”/book/bookinfo/author/firstname/child::text()”); foreach($arAuthor AS $node) { print $node.”n”; } Of course, the result from the print statement is Rob. There is only a single text node after all. The $node object, however, is really the firstname element. You can check this by either importing it to the DOM extension or checking the class type of the object, which will be SimpleXMLElement. If you have read Chapter 4 and Chapter 6, you already know there is an issue when dealing with documents using default namespaces and XPath. Just like the DOM extension, SimpleXML offers a method to register namespaces and associated prefixes: registerXPathNamespace(). This method works the same way and even takes the same parameters as the method in the DOM extension. The first parameter is the prefix, and the second parameter is the namespace URI. Caution The method registerXPathNamespace is available only in PHP 5.1+. To perform XPath queries dealing with default namespaces in PHP 5.0.x, you will need to leverage the XPath functionality in the DOM extension or write XPath queries in such a way that the qualifiers bypass any namespace checks. Refer to Chapters 4 and 6 for additional information. Using the document in Listing 7-3, retrieving the firstnameelement requires the use of namespaces. The namespaced elements reside in a default namespace, so the registerXPathNamespace() method will be used to register a prefix that can be used in the XPath expression: $book = simplexml_load_file(’sxmlns2.xml’); $book->registerXPathNamespace(”sxe”, “http://www.example.com/ns1″); if ($arAuthor = $book->xpath(”/book/sxe:bookinfo/sxe:author/sxe:firstname”)) { foreach($arAuthor AS $node) { print $node.”n”; } } The prefix sxe is registered and associated with the namespace http://www.example.com/ ns1. The query is executed, and the resulting variable is tested to make sure that nodes were returned. In the event no nodes result from the query, the method xpath() returns FALSE rather than an array. In some cases, an empty array is returned and occurs when nodes are returned, but they are not a valid type under SimpleXML. For instance, a query that results in a PI node is valid in XPath, but the node type is not supported in SimpleXML. In this case, an empty array is returned, indicating that the query was successful but no usable nodes are available.

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap 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