Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would say python handles XML pretty well. The number of different libraries available speaks to that - you have lots of options. And if there are features missing from libraries that you would like to use, feel free to contribute some patches!</p> <p>I personally use the DOM and lxml.etree (etree is <em>really</em> fast). However, I feel your pain about the namespace thing. I wrote a quick helper function to deal with it: </p> <pre><code>DEFAULT_NS = "http://www.domain.org/path/to/xml" def add_xml_namespace(path, namespace=DEFAULT_NS): """Adds namespaces to an XPath-ish expression path for etree Test simple expression: &gt;&gt;&gt; add_xml_namespace('image/namingData/fileBaseName') '{http://www.domain.org/path/to/xml}image/{http://www.domain.org/path/to/xml}namingData/{http://www.domain.org/path/to/xml}fileBaseName' More complicated expression &gt;&gt;&gt; add_xml_namespace('.//image/*') './/{http://www.domain.org/path/to/xml}image/*' &gt;&gt;&gt; add_xml_namespace('.//image/text()') './/{http://www.domain.org/path/to/xml}image/text()' """ pattern = re.compile(r'^[A-Za-z0-9-]+$') tags = path.split('/') for i in xrange(len(tags)): if pattern.match(tags[i]): tags[i] = "{%s}%s" % (namespace, tags[i]) return '/'.join(tags) </code></pre> <p>I use it like so:</p> <pre><code>from lxml import etree from utilities import add_xml_namespace as ns tree = etree.parse('file.xml') node = tree.get_root().find(ns('root/group/subgroup')) # etc. </code></pre> <p>If you don't know the namespace ahead of time, you can extract it from a root node:</p> <pre><code>tree = etree.parse('file.xml') root = tree.getroot().tag namespace = root[1:root.index('}')] ns = lambda path: add_xml_namespace(path, namespace) ... </code></pre> <p>Additional comment: There <em>is</em> a little work involved here, but work is necessary when dealing with XML. That's not a python issue, it's an XML issue. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload