Nerd Post: the joys of xml
That title is a lie, I actually kind of hate working with XML, or at least I used to. Before I understood objects and the basics of OO programming I would get lost in traversing what could sometimes be a huge tangle of children and attributes. I am working on a new personal project in my off time. It’s partially for the Design for America link I posted yesterday, but basically I am working with Government data and thinking of new ways to simplify how to show it to people (me included).
One of the sites provides an XML sheet that lists out all members of congress and the senate along with varied data. Its a pretty massive list and could be difficult to find the data that I needed. I am building this in CakePHP, mostly due to it being what I am most familiar with, and also because in my admittedly non-technical opinion, I feel it does all the things I want it to do without complication without over including things I don’t need. Now I realize some of that is not true, Cake is a big library and can sometimes add some processing time, but if you are selective and optimize and cache, I’ve found it to be a great framework to write PHP in, but arguing frameworks is yet another post and we all know it kind of grinds my gears, what we are here to talk about today is XML and the newly discovered by me, but long existing xpath. If you know what xpath is and its old hat to you, then please feel free to move on and stop reading this post now, because this is a novice tutorial on reading in an xml file and searching it.
You ready for this big reveal…it’s going to blow your mind, I will find any congress person in the state of california with two lines of code:
$this->xml = simplexml_load_file($this->xml_path, null, true);
$results = $this->xml->xpath('person[@state="CA"]');
That’s it, but I will explain it a little bit. The first thing I am doing (and this is stretched across a few functions in my class, hence the “$this->”) is reading in the xml file I have cached on my server. After that I am returning a list of simpleXML objects that meet my search criteria. Essentially I go to the first set of children “person” and I search the attribute “state” for the term CA. You can traverse as deep as you need, I could say:
$results = $this->xml->xpath('person/role[@district="43"]');
which would return all people who are in the 43rd district. Pretty powerful and amazingly fast. You can read a lot more about what queries you can make and how to form them at the W3Schools Tutorial site as well as the W3 Doc.
I hope this little bit of information can save you a few minutes on how to query and manage XML with PHP, and if you care to share more I am just beginning to learn this as well, but always welcome opinions.