Creating an element with 'class' attribute throws a syntax error
Question: When I try to do this with the `lxml` module:

    
    
    div = etree.SubElement(body, "div", class="hmi")
    

I get a:

    
    
    user@localhost:metk $ sudo python mbscan.py -r 192.168.0.0/24 --hmi
      File "mbscan.py", line 481
        div = etree.SubElement(body, "div", class="hmi")
                                                ^
    SyntaxError: invalid syntax
    

Because class is a keyword reserved for Python for another use. IE this:

    
    
    div = etree.SubElement(body, "div", hello="hmi")
    

works fine.

I know there is another engine made specifically for `html` in `lxml` but for
the sake of keeping my `xml` and `html` report generators as similar as
possible I'm wondering if there isn't another workaround here?

**How can I create an`xml` tag with a `class` attribute without upsetting
Python?**


Answer: Define a dictionary of attributes and pass it as `attrib` argument.

For example:

    
    
    >>> from lxml.html import Element, tostring
    >>> 
    >>> div = Element("div", attrib={"class": "hmi"})
    >>> 
    >>> print(tostring(div))
    <div class="hmi"></div>
    

Note that I'm using `lxml.html` here, but the same idea applies for `etree`.

