When I was writing about how I use web scraping, I was still hadn’t tried using Xpath (shame on me). sssscripting blog responded to my article with very good and rich post about all sorts of different techniques for scraping (with Ruby examples) and after reading this post in Kore Nordmann blog I finally decided to try making something with Xpath.
It turned out, that using Xpath is extremely easy, really. When you master it, you can do everything in seconds. Yes, you need to know how XML works and how to write correct Xpath queries (brief explanation of Xpath syntax is available at W3Schools), but hey - these topics are in 1st year of university.
Lately I got involved with some AJAX, namely dynamic maps and autocompletion. It is much more easier to use JSON as the serialization format than XML because no complex parsing is needed, JSON structures automagically become JavaScript objects. So I knew I need my webservice endpoints to return JSON, but the DIY Framework is based on XML serialization of objects.
Client-side developers always had it easy - libraries such as jQuery and Prototype make finding elements on the page reliable and efficient. In PHP, regular expressions tend to get rather messy, DOM calls can be confusing and verbose, and often the string functions just aren’t enough. In this tutorial, I’ll show you how to use the middle ground - the open source PHP Simple HTML DOM Parser library, which provides jQuery-grade awesomeness for easy screen scraping without messy regular expressions.
The Simple HTML DOM Parser is implemented as a simple PHP class and a few helper functions. It supports CSS selector style screen scraping (such as in jQuery), can handle invalid HTML, and even provides a familiar interface to manipulate a DOM.
To be frank, how many times in your life as a PHP developer have you had to deal with XML documents? Probably dozens of times, particularly if you use it to develop PHP applications that implement some kind of web service, or possibly when creating RSS feeds and template files for different web sites. And, surely, the list goes on and on.
But wait a minute! Of course, I’m not saying that XML is the evil player of this game, because you’d be completely misunderstanding my point. XML is great for different reasons, which are definitely out of the scope of this article. But when you need to build large XML documents from scratch or even parse them in some way, you can be faced with a quite challenging and time-consuming process that might cause hair loss!
However, as you know, PHP 5 comes equipped with a number of extensions that will make your life much easier when it comes to building and parsing things like XML data. For example, there’s the “SimpleXML” library, which permits you to perform some useful tasks, such as reading XML data from files and strings, iterating over XML documents, parsing individual nodes, and so forth.
PHP 5 has yet another extension that can be really helpful for handling XML data in all sorts of clever ways. In this case, I’m talking about the one called “XML DOM.” If you’ve already worked with it during the development of your PHP 5 applications, then you should be well aware of its strong capabilities when it comes to dealing with XML data.
Of course, there’s the possibility that aside from being a PHP programmer, you’re also a professional diver, who spends most of your time exploring the beauty of the oceans and, in consequence, you've never heard about this PHP extension. In that case, let me tell you a couple of interesting things that you can do with it.
Simply put, the DOM XML extension, as its name suggests, will permit you to work on XML documents by way of the DOM API. In this way, it allows you to read from and write to XML files, build XML data from scratch and parse specific nodes, extract them as an array, and even process HTML markup in different ways by using a handful of intuitive methods that closely resemble the ones used with JavaScript.
As you can see, this PHP extension offers a bunch of powerful features that can be useful for working with XML data. Therefore, provided that you’re interested in learning how to get started using it within your own PHP applications, in this article series I’ll be discussing some of its most relevant methods and properties and accompanying all of these explanations with concise and instructive hands-on examples.
Serialize is useful for storing or passing PHP values around without losing type and structure.
But if you want to serialize a SimpleXml object, you will have some problem on unserialize with the error “Warning: unserialize() [function.unserialize]: Node no longer exists in DIRECTORY/FILE.php on line X”.
So I found some solutions on the web and in the php documentation like this one : http://theserverpages.com/php/manual/en/ref.simplexml.php#52761
Replacing SimpleXMLObject with stdClass is a good idea but in this solution we loose all of attributes, and how can we make simplexml->xpath after ?
n this article I will show how to generate dynamic XML documents with Object-Oriented PHP. Before I get started, let's get right to the heart of this functionality. Although the code may look fancy, being packaged up inside objects and inheritance, the key elements of this functionality rest in the the mysql_list_fields() and mysql_num_fields() functions.
What they do is get a field list array and a column count for a given table in a database. The way that we'll be able to have a generic XML output method in our class is by taking advantage of these features. So, in simplicity, instead of outputting our XML by handcoded column names, we are going to create a method that dynamically creates XML output with only a record id and the table name to query from. The implementation, however, is a bit more complex.
At the office today, I had a talk with our designers, so they want a "designer-friendly" template engine. Sounds easy, but it isnt :).
That is why I decided to write this very short post to help others having such “easy” problems. Also after alot of researching with Google, I found that there is only 1 example of creating such Template Engine (DOM based) that is a little old.
Ever wished for an easy way to transform SQL result sets into XML? It's a PEAR package named XML_Query2XML, and it provides a comprehensive framework to efficiently turn the results of a database query into a customizable XML document. This article introduces the package, and demonstrates useful real-world applications, including using it with XSL and XPath, combining it with data from external Web services, and creating database dump files.
A canny data import technique that emerged from praxis, while working on the import of data-centered XML resources, is utilitizing the abilities of Xslt. The generation of the required SQL statements actually only needs a simple Xsl stylesheet which might import for an PHP XSLTProcessor object or pass to the xsltproc command line tool. Both further described approaches are based upon the libxslt™ library and are assuming the use of XSLT 1.0.
One of the most common things web coders run into is the need to parse some type of XML file. Many web services return API calls in XML format, so it’s just handy to know how to parse these results quickly. With PHP4 you usually have to rely on some large parsing library to get the job done or deal with overly complicated PHP functions, but PHP 5 has a great extension called SimpleXML.
When I say parsing XML, I’m talking about navigating through XML markup to return data of interest. For example, let’s take a look at the Yahoo! geocoding API. With the geocoding API you can call a specially crafted request URL with parameters, such as city and state, to receive latitude and longitude coordinates which come in handy when creating mapping mashups.








