TechHui

Hawaiʻi's Technology Community

Please pardon my ignorance here as I'm getting my feet wet again with development. I'm developing a PHP app that's traversing some xml docs, specifically an XML doc which is essentially outputting a few database records.

I'm playing with the various php XML libs like simplexml, and DOMDocument, and slogging my way through it, having fun. But as I'm doing this I keep thinking that there's got to be a higher level php library that can transform an XML-based doc into a php array. I guess can I code it myself but in my gut I know it's out there.

My problem is that I don't know what kinds of terms to seach on or look for. Anybody got any ideas? The question is: "What kind of php lib decodes the content similar to the attached XML file, which is essentially the output of a few simple records, into a php array for further processing?"

Pls let me know. Very much appreciated.

Sample XML doc attached.

Views: 51

Attachments:

Replies to This Discussion

Peter,

Sorry if this seems insultingly obvious, but have you considered using a library that supports XPath? It sounds exactly like what you're looking for -- a way to randomly access the nodes in a nested-array like manner.

With Xpath, you can hit any node in the document that's a descendant of the root node -- it's pretty powerful stuff to say something like "/Row[3]/Column[2]/Value" and just get the string-value, instead of manually walking down a DOM Tree "the hard way" by going node to node.

In my experience, it's by far the the easiest and most useful way to parse and manipulate XML, and I've done some wickedly hairy stuff like resolving lookup table values that are stored elsewhere in the XML document. That kind of stuff is definitely not for the faint-hearted, but is possible with some creative coding.

Caution: Avoid namespace-prefixed XML while you're a newbie learning how to do XPath Queries -- namespaces add to the confusion, and can totally throw off otherwise functional XPath queries. Depending on what platform you're using, there are ways to enter the namespaced scope to accomplish the query you need.
Thanks man. No insults taken! Yes, I should have mentioned Xpath too and yes, thats avail. I just keep thinking there's got to be something out there that knows this is a db table and will put it into a table-like array structure for me.

But since you've done wicked stuff, I'll sigh, shake my head, and horse around w/ Xpath. I'm using SketchPath as my little xpath tool. Any better ones?

Mahalo!
Since you're using an interpreted language like PHP, here's a thought: Use an XSLT to convert the original XML document into your language's source code, and just eval() it. :-) No muss, no fuss, and you have control over how the data is structured in your platform.
very cool idea! (as I now recursively dive even deeper into ref. manuals (and line up to buy "XML in a nutshell") )

RSS

Sponsors

web design, web development, localization

© 2024   Created by Daniel Leuck.   Powered by

Badges  |  Report an Issue  |  Terms of Service