Dmitry Bubnenkov
Dmitry Bubnenkov

Reputation: 9859

How to convert xpath Element to XML?

I am using python xpath and I need to convert Element to XML.

<Element {http://zakupki.gov.ru/oos/types/1}application at 0x16568142d48>

I discovered methods with dir, and did not found any proper method.

['__bool__', '__class__', '__contains__', '__copy__', '__deepcopy__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '_init', 'addnext', 'addprevious', 'append', 'attrib', 'base', 'clear', 'cssselect', 'extend', 'find', 'findall', 'findtext', 'get', 'getchildren', 'getiterator', 'getnext', 'getparent', 'getprevious', 'getroottree', 'index', 'insert', 'items', 'iter', 'iterancestors', 'iterchildren', 'iterdescendants', 'iterfind', 'itersiblings', 'itertext', 'keys', 'makeelement', 'nsmap', 'prefix', 'remove', 'replace', 'set', 'sourceline', 'tag', 'tail', 'text', 'values', 'xpath']

App:

afile.xml:

<?xml version = "1.0" encoding = "UTF-8"?>
<applications >
  <application >
      <journalNumber > 1 < /journalNumber >
   </application >
   <application >
      <journalNumber > 2</journalNumber>
   </application >
</applications >

app:

    etxml = etree.parse(afile)
    root = etxml.getroot()

    value = root.xpath("//*[local-name() = '{0}']".format("application"))

Upvotes: 0

Views: 520

Answers (1)

Tomalak
Tomalak

Reputation: 338238

etxml = etree.parse(afile)
root = etxml.getroot()

value = root.xpath("//*[local-name() = '{0}']".format("application"))

etree.tostring(value)

See: https://docs.python.org/3.7/library/xml.etree.elementtree.html#xml.etree.ElementTree.tostring

Upvotes: 2

Related Questions