Python lxml.objectify.deannotate() Examples

The following are 12 code examples of lxml.objectify.deannotate(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module lxml.objectify , or try the search function .
Example #1
Source File: models.py    From pygameweb with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def remove_prefix(fname):
    """This removes namespace prefix from all the things in the xml.
    """
    from lxml import etree, objectify
    parser = etree.XMLParser(remove_blank_text=True)
    tree = etree.parse(fname, parser)
    root = tree.getroot()
    for elem in root.getiterator():
        if not hasattr(elem.tag, 'find'):
            continue
        i = elem.tag.find('}')
        if i >= 0:
            elem.tag = elem.tag[i + 1:]
    objectify.deannotate(root, cleanup_namespaces=True)
    # fname_out = fname.replace('.xml', '.out.xml')
    # tree.write(fname_out,
    #            pretty_print=True,
    #            xml_declaration=True,
    #            encoding='UTF-8')
    return tree 
Example #2
Source File: modify_payload.py    From hoverpy with Apache License 2.0 5 votes vote down vote up
def main():
    data = sys.stdin.readlines()
    payload = data[0]
    payload_dict = json.loads(payload)

    if "response" in payload_dict and "body" in payload_dict["response"]:
        body = payload_dict["response"]["body"]
        try:
            root = objectify.fromstring(str(body))
            ns = "{http://ws.cdyne.com/}"
            logging.debug("transforming")

            ipe = ns + "ResolveIPResponse"
            ipt = ns + "ResolveIPResult"

            root.Body[ipe][ipt].City = "New York"

            objectify.deannotate(root.Body[ipe][ipt].City)
            etree.cleanup_namespaces(root.Body[ipe][ipt].City)

            payload_dict["response"]["body"] = etree.tostring(root)

            logging.debug(etree.tostring(root))

        except Exception:
            pass

    print(json.dumps(payload_dict)) 
Example #3
Source File: article.py    From allofplos with MIT License 5 votes vote down vote up
def rich_title(self):
        """For an individual PLOS article, get its title with HTML formatting.

        Preserves HTML formatting but removes all other XML tagging, namespace/xlink info, etc.
        Doesn't do xpath directly on `self.root` so can deannotate separate object
        See http://lxml.de/objectify.html#how-data-types-are-matched for more info on deannotate process
        Exceptions that still need handling:
        10.1371/journal.pone.0179720, 10.1371/journal.pone.0068479, 10.1371/journal.pone.0069681,
        10.1371/journal.pone.0068965, 10.1371/journal.pone.0083868, 10.1371/journal.pone.0069554,
        10.1371/journal.pone.0068324, 10.1371/journal.pone.0067986, 10.1371/journal.pone.0068704,
        10.1371/journal.pone.0068492, 10.1371/journal.pone.0068764, 10.1371/journal.pone.0068979,
        10.1371/journal.pone.0068544, 10.1371/journal.pone.0069084, 10.1371/journal.pone.0069675

        :return: string of article title at specified xpath location
        """
        root = self.root
        objectify.deannotate(root, cleanup_namespaces=True, xsi_nil=True)
        art_title = root.xpath("/article/front/article-meta/title-group/article-title")
        art_title = art_title[0]
        try:
            text = art_title.text
            if text is None:
                text = ''
            text += ''.join(et.tostring(child, encoding='unicode') if child.tag not in ('ext-link', 'named-content', 'sc', 'monospace') \
                                                                   else child.text + child.tail if child.tail is not None \
                                                                   else child.text
                            for child in art_title.getchildren())
            title = text.replace(' xmlns:xlink="http://www.w3.org/1999/xlink"', '') \
                        .replace(' xmlns:mml="http://www.w3.org/1998/Math/MathML"', '') \
                        .replace(' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance', '')
        except TypeError:
            # try to rewrite so this isn't needed
            print('Error processing article title for {}'.format(self.doi))
            title = et.tostring(art_title, method='text', encoding='unicode')
        return title 
Example #4
Source File: Dragon.py    From chemml with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _save_script(self):
        # objectify.deannotate(self.dragon)
        etree.cleanup_namespaces(self.dragon)
        self.drs_name = 'Dragon_script.drs'
        with open(os.path.join(self.output_directory, self.drs_name), 'w') as outfile:
            outfile.write(etree.tostring(self.dragon, pretty_print=True).decode()) 
Example #5
Source File: Dragon.py    From chemml with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def printout(self):
        objectify.deannotate(self.dragon)
        etree.cleanup_namespaces(self.dragon)
        print(objectify.dump(self.dragon)) 
Example #6
Source File: utils.py    From jaide with GNU General Public License v2.0 5 votes vote down vote up
def xpath(source_xml, xpath_expr, req_format='string'):
    """ Filter xml based on an xpath expression.

    Purpose: This function applies an Xpath expression to the XML
           | supplied by source_xml. Returns a string subtree or
           | subtrees that match the Xpath expression. It can also return
           | an xml object if desired.

    @param source_xml: Plain text XML that will be filtered
    @type source_xml: str or lxml.etree.ElementTree.Element object
    @param xpath_expr: Xpath expression that we will filter the XML by.
    @type xpath_expr: str
    @param req_format: the desired format of the response, accepts string or
                     | xml.
    @type req_format: str

    @returns: The filtered XML if filtering was successful. Otherwise,
            | an empty string.
    @rtype: str or ElementTree
    """
    tree = source_xml
    if not isinstance(source_xml, ET.Element):
        tree = objectify.fromstring(source_xml)
    # clean up the namespace in the tags, as namespaces appear to confuse
    # xpath method
    for elem in tree.getiterator():
        # beware of factory functions such as Comment
        if isinstance(elem.tag, basestring):
            i = elem.tag.find('}')
            if i >= 0:
                elem.tag = elem.tag[i+1:]

    # remove unused namespaces
    objectify.deannotate(tree, cleanup_namespaces=True)
    filtered_list = tree.xpath(xpath_expr)
    # Return string from the list of Elements or pure xml
    if req_format == 'xml':
        return filtered_list
    matches = ''.join(etree.tostring(
        element, pretty_print=True) for element in filtered_list)
    return matches if matches else "" 
Example #7
Source File: parse.py    From django-business-logic with MIT License 5 votes vote down vote up
def cleanup_namespace(root):
        # http://stackoverflow.com/a/18160164
        for elem in root.getiterator():
            if not hasattr(elem.tag, 'find'):
                continue
            i = elem.tag.find('}')
            if i >= 0:
                elem.tag = elem.tag[i + 1:]
        objectify.deannotate(root, cleanup_namespaces=True) 
Example #8
Source File: manifest_models.py    From fabric8-analytics-server with Apache License 2.0 5 votes vote down vote up
def __getitem__(self, key):
        """Return the value for attr key."""
        attr = getattr(self.root, key, None)
        objectify.deannotate(self.root)
        return attr 
Example #9
Source File: manifest_models.py    From fabric8-analytics-server with Apache License 2.0 5 votes vote down vote up
def __setitem__(self, key, value):
        """Set value for attr key."""
        _prev = getattr(self.root, 'modelVersion', None)
        if key in ('groupId', 'artifactId', 'name', 'version', 'packaging') and _prev is not None:
            #  add these tags just after modelVersion tag.
            element = etree.Element(key)
            element.text = value
            _prev.addnext(element)
        else:
            setattr(self.root, key, value)
        objectify.deannotate(self.root)
        self._reload(self.root) 
Example #10
Source File: manifest_models.py    From fabric8-analytics-server with Apache License 2.0 5 votes vote down vote up
def tostring(obj, decoding=False):
        """Convert the xml object into string.

        :returns: String
        """
        if getattr(obj, '_commit', None) is not None:
            obj._commit()

        objectify.deannotate(obj.root, xsi_nil=True,
                             pytype=False, xsi=False, cleanup_namespaces=True)
        _str = etree.tostring(obj.root, pretty_print=True)
        if decoding:
            return _str.decode()
        return _str 
Example #11
Source File: manifest_models.py    From fabric8-analytics-server with Apache License 2.0 5 votes vote down vote up
def _reload(obj):
        obj = objectify.fromstring(etree.tostring(obj))
        objectify.deannotate(obj, xsi_nil=True, cleanup_namespaces=True)
        return obj 
Example #12
Source File: manifest_models.py    From fabric8-analytics-server with Apache License 2.0 5 votes vote down vote up
def __getitem__(self, key):
            """Return the value for attr key."""
            attr = getattr(self.root, key, None)
            objectify.deannotate(self.root)
            return attr