title: Parsing a complicated XML file with Python
body: <p>I am trying to parse a very ugly XML file with Python. I manage to get pretty well into it, but at the npdoc element it fails. What am I doing wrong?</p>
  
  <p>XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;npexchange xmlns="http://www.example.com/npexchange/3.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.5"&gt;
  &lt;article id="123" refType="Article"&gt;
  &lt;articleparts&gt;
      &lt;articlepart id="1234" refType="ArticlePart"&gt;
          &lt;data&gt;
              &lt;npdoc xmlns="http://www.example.com/npdoc/2.1" version="2.1" xml:lang="sv_SE"&gt;
                  &lt;body&gt;
                      &lt;p&gt;Lorem ipsum some random text here.&lt;/p&gt;
                      &lt;p&gt;
                          &lt;b&gt;Yes this is HTML markup, and I would like to keep that.&lt;/b&gt;
                      &lt;/p&gt;
                  &lt;/body&gt;
                  &lt;headline&gt;
                      &lt;p&gt;I am a headline&lt;/p&gt;
                  &lt;/headline&gt;
                  &lt;leadin&gt;
                      &lt;p&gt;I am some other text&lt;/p&gt;
                  &lt;/leadin&gt;
              &lt;/npdoc&gt;
          &lt;/data&gt;
      &lt;/articlepart&gt;
  &lt;/articleparts&gt;
  &lt;/article&gt;
  &lt;/npexchange&gt;
  </code></pre>
  
  <p>This is the python code I have so far:</p>
  
  <pre><code>from xml.etree.ElementTree import ElementTree
  
  def parse(self):
      tree = ElementTree(file=filename)
  
      for item in tree.iter("article"):
          articleParts = item.find("articleparts")
          for articlepart in articleParts.iter("articlepart"):
              data = articlepart.find("data")
              npdoc = data.find("npdoc")
  
              id = item.get("id")
              headline = npdoc.find("headline").text
              leadIn = npdoc.find("leadin").text
              body = npdoc.find("body").text
  
  
      return articles
  </code></pre>
  
  <p>What happens is that I get the id out, but the fields that are inside the npdoc element I cannot access. The npdoc variable gets set to None.</p>
  
  <p>Update:
  Managed to get the elements into variables by using the namespace in the .find() calls. How do I get the value? As it is HTML it does not come out correctly with the .text attribute.</p>