How to use the lxml.etree.tostring function in lxml

To help you get started, we’ve selected a few lxml examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github django-oscar / django-oscar-mws / tests / unit / test_models.py View on Github external
def test_getting_standard_product_from_upc(self):
        upc = '1234567890'
        profile = factories.AmazonProfileFactory(product__upc=upc)
        spi = profile.get_standard_product_id()
        self.assertEquals(
            etree.tostring(spi),
            PRODUCT_TYPE_XML.format('UPC', upc)
        )
github networkupstools / nut / tools / nut-hclinfo.py View on Github external
attr = ""
            innerHTML = ""
            for key, value in cell.iteritems():
                val = unicode(str(value), "utf-8")
                if key != "text":
                    attr += " %s='%s'" % (key, val)
                else:
                    innerHTML = val
            
            r.append(html.fromstring("%s" % (attr, innerHTML)))
            
        tbody.append(r)
            
    table.append(tbody)
    
    return etree.tostring(table, pretty_print=True)
github Bcfg2 / bcfg2 / tools / posixusers_baseline.py View on Github external
entry.set(attr, str(data[idx]))
        if entry.tag == 'POSIXUser':
            try:
                entry.set("group", grp.getgrgid(data[3])[0])
            except KeyError:
                logger.warning("User %s is a member of nonexistent group %s" %
                               (entry.get("name"), data[3]))
                entry.set("group", str(data[3]))
            for group in users.user_supplementary_groups(entry):
                memberof = lxml.etree.SubElement(entry, "MemberOf",
                                                 group=group[0])

        entry.tag = "Bound" + entry.tag
        baseline.append(entry)

    print(lxml.etree.tostring(baseline, pretty_print=True))
github theatlantic / django-xml / djxml / xmlmodels / fields.py View on Github external
def format_value(self, value):
        formatted = etree.tostring(value, encoding='unicode', method='html')
        if self.strip_xhtml_ns:
            formatted = formatted.replace(u' xmlns="http://www.w3.org/1999/xhtml"', '')
        return formatted
github iotile / coretools / iotilebuild / iotile / build / config / scons-local-3.0.1 / SCons / Tool / docbook / __init__.py View on Github external
read_network=False, 
                                      write_network=False)
    xsl_style = env.subst('$DOCBOOK_XSL')
    xsl_tree = etree.parse(xsl_style)
    transform = etree.XSLT(xsl_tree, access_control=xslt_ac)
    doc = etree.parse(str(source[0]))
    # Support for additional parameters
    parampass = {}
    if parampass:
        result = transform(doc, **parampass)
    else:
        result = transform(doc)
        
    try:
        of = open(str(target[0]), "wb")
        of.write(of.write(etree.tostring(result, pretty_print=True)))
        of.close()
    except:
        pass

    return None
github hydroshare / hydroshare / hs_swat_modelinstance / models.py View on Github external
else:
                hsterms_swat_model_parameters.text = \
                    self.model_parameter.get_swat_model_parameters()

        if self.model_input:
            modelInputFields = ['warmupPeriodType', 'warmupPeriodValue', 'rainfallTimeStepType',
                                'rainfallTimeStepValue', 'routingTimeStepType',
                                'routingTimeStepValue', 'simulationTimeStepType',
                                'simulationTimeStepValue', 'watershedArea', 'numberOfSubbasins',
                                'numberOfHRUs', 'demResolution', 'demSourceName', 'demSourceURL',
                                'landUseDataSourceName', 'landUseDataSourceURL',
                                'soilDataSourceName', 'soilDataSourceURL'
                                ]
            self.add_metadata_element_to_xml(container, self.model_input, modelInputFields)

        return etree.tostring(RDF_ROOT, pretty_print=pretty_print)
github kovidgoyal / calibre / src / calibre / ebooks / oeb / parse_utils.py View on Github external
def _html4_parse(data):
    data = html.fromstring(data)
    data.attrib.pop('xmlns', None)
    for elem in data.iter(tag=etree.Comment):
        if elem.text:
            elem.text = elem.text.strip('-')
    data = etree.tostring(data, encoding='unicode')

    # Setting huge_tree=True causes crashes in windows with large files
    parser = etree.XMLParser(no_network=True)
    try:
        data = etree.fromstring(data, parser=parser)
    except etree.XMLSyntaxError:
        data = etree.fromstring(data, parser=RECOVER_PARSER)
    return data
github powdahound / ec2instances.info / scrape.py View on Github external
def add_eni_info(instances):
    eni_url = "http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-eni.html"
    tree = etree.parse(urllib2.urlopen(eni_url), etree.HTMLParser())
    table = tree.xpath('//div[@class="table-contents"]//table')[0]
    rows = table.xpath('.//tr[./td]')
    by_type = {i.instance_type: i for i in instances}

    for r in rows:
        instance_type = etree.tostring(r[0], method='text').strip().decode()
        max_enis = locale.atoi(etree.tostring(r[1], method='text').decode())
        ip_per_eni = locale.atoi(etree.tostring(r[2], method='text').decode())
        if instance_type not in by_type:
            print("Unknown instance type: {}".format(instance_type))
            continue
        by_type[instance_type].vpc = {
            'max_enis': max_enis,
            'ips_per_eni': ip_per_eni}
github eevee / spline / spline / feature / feed.py View on Github external
# Recommended
                # TODO author is REQUIRED if the whole feed lacks one!
                #atom_el.author("TODO"),
                #atom_el.content("TODO"),
                atom_el.link(rel="alternate", href=url),
                #atom_el.summary("TODO"),

                # Optional
                #atom_el.category("..."),
                #atom_el.contributor("..."),
                #atom_el.published("..."),
                #atom_el.source("..."),
                #atom_el.rights("..."),
            ))

        xml = etree.tostring(
            root,
            pretty_print=True,
            encoding="UTF-8",
            xml_declaration=True)
        return Response(xml, content_type="text/xml", charset="UTF-8")