7 ms·
Python module that makes working with XML feel like working with JSON
- dkhenry 13y agoI think a better title would be make working with XML feel like native dictionaries, There are many ways for interacting with JSON dictionaries are only one of them. For example you can do XPath style interaction with JSON in python which for many things works out just a good if not better.
- zamalek 13y agoWhat about namespaces? If you are not using namespaces you are very likely using XML as the wrong solution for a problem - still your library has use for helping cleaning up old code that did use XML as the wrong solution. Good work!
- eli 13y agoThe overwhelming majority of XML I encounter in the real world does not use namespaces.
- HNLogInShit 13y ago" If you are not using namespaces you are very likely using XML as the wrong solution for a problem" What? Namespaces were a MISTAKE in the design of XML in the first place. Elements' meaning is already made clear by their context. Namespaces are an amateurish, common-sense-defying hack.
- hafabnew 13y agoCool, although LXML (mature, fast, reliable, generally awesome) can do this (and more): http://lxml.de/FAQ.html#how-can-i-map-an-xml-tree-into-a-dict-of-dicts http://lxml.de/FAQ.html#how-can-i-map-an-xml-tree-into-a-dic... It also has the 'objectify' API, where you can access XML nodes via regular object access (i.e. `access.nodes[0].like.this`). http://lxml.de/objectify.html http://lxml.de/objectify.html
- raverbashing 13y agoGood, I had never seen this transformation tip in LXML But usually, yes, LXML is "good" meaning it's the least worse way of dealing with XML Also, it has some idiosyncrasies, like insisting on adding the namespace on tag names, so you end with something like {http://example.com/your.xlsd http://example.com/your.xlsd}.index (I don't remember it exactly and I don't have an example here with me)
- toyg 13y agoCorrectly handling namespaced QNames is a requirement, not a bug. It makes things awkward at times, but that's a job for lib writers to provide decent interfaces. I haven't used LXML in a while, but ElementTree, for example, forces you to use the QName in XPath expressions, which is technically correct but a huge pain; it would be nice if there was a ScrewNamespace option that would allow "simple" searches, although this might blow up in your face one day (when two namespaces define the same element name, and your xpath search brings up elements you didn't really want).
- raverbashing 13y agoActually the short name worked for the searches, the problem was reading element names from a subtree It's not as much as dodging the requirements but rather an inconsistency in the API.
- georgebashi 13y agoThat's the "node-name", i.e. the fully qualified name of the tag in its namespace. You probably wanted to ask for the local-name, which in your example would just be "index". Not sure how to with LXML, but it's a common mistake people make when dealing with XML.
- TimSAstro 13y agoI also found the namespacing to be a bit weird, and it took quite a while to grok the documentation. In case anyone wants a working example, I implemented a wrapper to drop the namespacing (resulting in simple objectify attribute access) for one particular XML schema here: https://github.com/timstaley/voevent-parse/blob/master/voeparse/voevent.py#L79 https://github.com/timstaley/voevent-parse/blob/master/voepa...
- cronin101 13y agoThe Ruby equivalent would be XmlSimple: http://xml-simple.rubyforge.org/ http://xml-simple.rubyforge.org/
- eCa 13y agoAnd the Perl equivalent is XML::Simple: https://metacpan.org/module/XML::Simple https://metacpan.org/module/XML::Simple
- fuzzix 13y agoI would not recommend XML::Simple, there are various cases (not even that uncommon - say having an attribute and an element of the same name) where it breaks down. It may be awkward to use at times, but XML::LibXML is robust and consistent.
- anaphor 13y agoDoesn't this defeat some of the advantages of using XML though? Like Xpath?
- raverbashing 13y agoUsing XPath is good but it's a small comfort in the bloated and overcomplicated world of XML I don't see any advantage in using XML to be honest
- jbaiter 13y ago> I don't see any advantage in using XML to be honest Well, it's great for what it was intended for: structured documents. I work at a library, where I'm dealing with thousands of digitized books and I haven't yet found a proper non-XML alternative to something like TEI.
- sigzero 13y agoI was going to pipe up on this. XML is for documents. JSON is for data. Sadly, the worlds isn't black and white like that.
- emn13 13y agoI've never understood this criticism. XML is quite simple - perhaps too simple. If you're really offended by needing to repeat an element name in the closing tag... well... that's not exactly a serious criticism is it? I mean, you could make various reasonable criticsms - XML sure isn't perfect. You might say the object model maps to documents, but not so well to typical programming constructs such as unordered hashes or ordered lists of unnamed items. You might complain that its data model is too simple in that it can only easily represent printable text-strings not binary data, numbers, dates etc. You might complain that namespaces are a hassle in many API's. You might complain that the distinction between attributes and elements is odd and somewhat arbitrary. But bloated? You're just ranting.
- arethuza 13y ago
- toyg 13y agoThere are umpteen versions of this sort (there was even an ActiveState cookbook recipe at one point, I think). The problem is that they tend to be coded up with certain requirements in mind, without really going through XML specs, so inevitably you'll find places where they break down. In this case, I don't see any namespace in examples nor (more damning) in unit tests, which tells me it's likely going to fail on complex documents with multiple namespaces.
- HNLogInShit 13y agoNamespaces in XML were a blunder and shouldn't even exist. Sadly we're still paying the price.
- clarkevans 13y agoIn 1999, there were a group of us working on a subset of XML (we called SML at the time) that had a simpler information model and syntax limitations so it'd be easier to work with. It seems that any simple encoding of XML onto JSON might find the deliberations interesting (if quaint and outdated): http://tech.groups.yahoo.com/group/sml-dev/message/0 http://tech.groups.yahoo.com/group/sml-dev/message/0
- fosap 13y agoToday there is MicroXML. http://www.w3.org/community/microxml/ http://www.w3.org/community/microxml/
- pknerd 13y agoI recently used it for a project and I am quite a happy consumer of this library.
- fr33k3y 13y agoThere is XML::Simple perl module since a century ago and it's ruby "copy" with the same name since long time ago also. I've used the perl one and it only makes sense in very simple use-cases.
- nmcfarl 13y agoI’ve used the perl one as well. And while I’ll agree the XML handled needs to be simple, I’d say the the use case needn’t be. At one job dealt with 10 and 20 Megabyte XML files on a daily basis - and I never thought of throwing XML::Simple at those. The results would be anything but Simple. On the other hand I’ve run production code for years with an XML::Simple client to XML only RESTish API with ~100 endpoints. And that suckers still critical infrastructure - and barely being touched. Just humming along.
- tootie 13y agoXML::Simple was glorious for working with 1-100K documents. I've used it on bigger docs and it actually handles them pretty gracefully although I've never tried at scale.
- mathattack 13y agoThis seems so obvious that I wonder why it wasn't done before. Well done!
- martinblech 13y agoThanks!
- watchdogtimer 13y agoI used this to parse some XML when creating a shipping calculator. It was a huge time-saver.
- Erwin 13y ago10 years ago (he was just about 16 then!!), Aaron Swartz wrote this which has been very useful to me: http://www.aaronsw.com/2002/xmltramp/ http://www.aaronsw.com/2002/xmltramp/ Since then the builtin interfaces have improved and I use mostly xml.etree.cElementTree
- wulczer 13y agoWe wrote a similar utility for our ORM-for-SaaS library some time ago, using lxml if available and falling back to ElementTree: https://github.com/ducksboard/libsaas/blob/master/libsaas/xml.py https://github.com/ducksboard/libsaas/blob/master/libsaas/xm... Bear in mind that both approaches are lossy - trying to support every XML quirk would quickly lead to reimplementing the libxml wheel...
- lechevalierd3on 13y agoThis reminds me of PHP's SimpleXML http://php.net/manual/en/book.simplexml.php http://php.net/manual/en/book.simplexml.php Which belongs to the good parts of PHP.
- Dru89 13y agoI haven't had much of a chance to play with it yet, but I wonder how it would handle a query to find all the "baz" elements for a structure like this: http://pastebin.com/m7qTy8kw http://pastebin.com/m7qTy8kw. Does it just support a loop, or possibly something similar to xpath like /document/foo/bar/baz to get an array of all of them. (Perhaps document["foo"]["bar"]["baz"], though I think that would break other things.)
- bonemachine 13y agoWhich is a pity, because... XML is nothing like JSON.
- brianmcconnell 13y agoI work with a lot of translation services. While the machine translation services are easy to deal with, the professional translation agencies _love_ XML. They also generally know f--- all about building usable web APIs. This looks pretty useful.
- larvaetron 13y agoI used this to build http://showhen.me http://showhen.me and it worked very well for my use case.
- level09 13y agoDoes this mean we can simply convert any xml api to a json equivalent by just json.dumps(xmltodict(xml)) ? I am going to give it a try.
- vittore 13y agoThere are two older similar projects - beautiful soup and untangle.
- xmjee 13y agoMade a port for FreeBSD, thanks much!