I have some data I converted to XML from a KML file and I was curious how to use PHP or Ruby to get back things like the neighborhood names and coordinates. I know when they have a tag around them like so.
<cities>
<neighborhood>Gotham</neighborhood>
</cities>
but the data is unfortunately formatted as:
<SimpleData name="neighborhd">Colgate Center</SimpleData>
instead of
<neighborhd>Colgate Center</neighborhd>
This is the KML source:
How can I use PHP or Ruby to pull data from something like this? I installed some Ruby gems for parsing XML data but XML is just something I haven't worked with much.
Your XML is invalid, but Nokogiri will attempt to fix it up.
Here's how to check for invalid XML/XHTML/HTML and how to rewrite the section you want.
Here's the setup:
Here's how to see if there are errors. Any time
errors
is not empty you have a problem.Here's one way to find the
SimpleData
nodes throughout a document. I prefer to use CSS accessors over XPath for readability reasons. Sometimes XPath is better because it allows better granularity when searching. You need to learn them both.Here's the output after running:
I'm not trying to modify the DOM, but it's easy to do:
After running this is the affected section: