Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Thomas Weinert:
FluentDOM 5 + XML Namespaces
Aug 07, 2014 @ 15:50:22

In this new post to his site Thomas Weinert shows how to use the FluentDOM library (a PHP implementation of a Javascript library by the same name) when XML namespaces are involved.

FluentDOM 5 allows to register namespaces on the DOM document class. These are not the namespaces of a loaded document, but a definition of namespaces for your programming logic.

He compares it to both a PHP example, using the DOMXpath handling and a Javascript sample using its own xmlDocument functionality. Finally he compares these examples to the few lines of FluentDOM code to handle the same kind of evaluation. He wraps up the post with a brief mention of the "appendElement" function that wraps serveral operations in one for easy element additions.

tagged: fluentdom xml namespace tutorial javascript domxpath element

Link: http://www.a-basketful-of-papayas.net/2014/08/fluentdom-5-xml-namespaces.html

James Morris' Blog:
Parsing HTML with DOMDocument and DOMXPath::Query
Jun 27, 2012 @ 15:19:35

In the latest post to his blog James Morris looks at using XPath's query() function to locate pieces of data in your XML.

The other day I needed to do some html scraping to trim out some repeated data stuck inside nested divs and produce a simplified array of said data. My first port of call was SimpleXML which I have used many times. However this time, the son of a bitch just wouldn’t work with me and kept on throwing up parsing errors. I lost my patience with it and decided to give DomDocument and DOMXpath a go which I’d heard of but never used.

He includes a code (and XML document) example showing how to extract out some content from an HTML structure - grabbing each of the images from inside a div and associating them with their description content.

tagged: dom domdocument domxpath xpath tutorial html

Link:

Thomas Weinert's Blog:
Using PHP DOM With XPath
Apr 13, 2010 @ 18:18:32

Thomas Weinert has a recent post to his blog showing how to use one of the more powerful XML-handling features that PHP's DOM extension includes - XPath.

Often I hear people say "We use SimpleXML, because DOM is so noisy and complex". Well, I don't think so. This article explains how you can parse a XML (an Atom feed) using the PHP DOM extension. No other libraries are involved.

In his example he loads an external feed (his own) into a DOM object, blocks any errors with a few handy functions and creates a DOMXPath object on the DOM object to get ready for his queries. He shows how to make searches for titles, subtitles, looping over attributes and an element list returned from one of the first queries. A full code listing is also provided to show how it all fits together.

tagged: dom xpath domxpath tutorial search atom

Link:


Trending Topics: