Python module that makes working with XML feel like working with JSON
github.com
github.com
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
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}.index (I don't remember it exactly and I don't have an example here with me)
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).
It's not as much as dodging the requirements but rather an inconsistency in the API.
However, while LXML can do this, and makes it easy, the documentation does not stress this way of using LXML. I like this project's emphasis on simplicity and doing one thing. It's the difference between "You can use LXML to get a dict" vs "Here is how to use xmltodict to get a dict". And it's right there in the name. Emphasis and naming are important when getting started.
1) you have to build libxml to use lxml
2) lxml has a large, powerful, complicated API
For 1), A friend of mine had to do an annoying workaround to use lxml on his box, due to its limited memory preventing him from being able to build libxml. Because xmltodict is Expat[1]-based, you don't have to build libxml in your environment to use it.
For 2), When I went to write a simple rss-reader project this past weekend, I dreaded going back to lxml. I knew that I'd have to go peruse its huge documentation to answer questions about whether to use lxml.XML or lxml.fromstring, whether methods I wanted were on Elements or ElementTrees, xpath syntax, custom parsers, etc. If I'd ever seen the objectify API I'd forgotten about it, because there's just so much _other_ stuff in lxml.
I happen to have found xmltodict in a brief search for lxml alternatives. It's in PyPI, so pip grabbed it with no complaints. It installed without building anything. And in less than a minute of glancing at the README, I grokked the API as "pydict = xmltodict.parse(xml_string)". I don't know if there are other things. I never had to find out.
Less than 10 minutes from finding it to forgetting I was reading XML as a source -- really a wonderful project. But if I were doing something 'serious', I'd absolutely use lxml. That large API and those byzantine docs exist for a good reason: they're dealing with XML properly. But sometimes coders just wanna have fun, or build a quick prototype or hack.
E.g. for extracting URLs from a sitemap:
from lxml import etree
root = etree.XML(data)
urls = root.xpath(
'.//sitemap:loc/text()',
namespaces={
'sitemap': 'http://www.sitemaps.org/schemas/sitemap/0.9',
}
)
For RSS reading, here's a straightforward example. Obviously you can factor out the index access on each item, and calling `/text()` too. import urllib2
from lxml import etree
data = urllib2.urlopen('https://news.ycombinator.com/rss').read()
root = etree.XML(data)
for i, item in enumerate(root.xpath('.//item')):
print i, item.xpath('title/text()')[0]
print item.xpath('description/text()')[0]
print item.xpath('link/text()')[0]
print
All pretty simple, xpath selectors can get a bit gnarly at times though. The tradeoff being that you can be very expressive with them.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.
For example you can do XPath style interaction with JSON in python which for many things works out just a good if not better.
I've used the perl one and it only makes sense in very simple use-cases.
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.
Since then the builtin interfaces have improved and I use mostly xml.etree.cElementTree
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.
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.
I've seen people do terrible mind rotting things with XML (e.g. encoded XML documents as attribute values in other XML documents and nested a few levels deep, using XML documents as keys in databases, using formats that aren't quite valid XML and then there is SOAP....)
http://en.wikipedia.org/wiki/Billion_laughs
And it especially is bloated if you throw in the whole suite of XML-related technologies, such as Schema (you may think that's unfair, but XML advocates frequently point to such technologies as the solution to various deficiencies of pure XML).
For example:
<e1> first <e2> second </e2> third </e1>
If you're parsing this into a data structure, how you you store "first" and "third"? There are a few different, very similar options. This ambiguity means XML parsers account for this differently and a particular programmer is going to have to sort through the various options to find what he needs (dom/etree/xpath/sax/etc). JSON (mostly) forces the author to decide how it should be parsed before encoding the data.I'd say it's unfair to say XML is bloated, it's reasonable to say that the "world of XML parsing" is bloated and confusing, especially if you're a programmer looking for a data serialization format.
I disagree pretty strongly with that statement: http://www.jelks.nu/XML/xmlebnf.txt
XML's grammar is far from simple.
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...
It may be awkward to use at times, but XML::LibXML is robust and consistent.
I am going to give it a try.