0

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"))
1
  • 1
    share the crucial enclosing html/xml fragment raw presentation Commented Sep 2, 2019 at 15:11

1 Answer 1

2
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

Sign up to request clarification or add additional context in comments.

4 Comments

If you don't mind, can you please explain (or refer me to a link explaining) how the format("application") method works here? If find it confusing and my searches didn't come up with anything.
No, I'm familiar with that (though I usually use f-strings); I just don't understand the mechanism of using an xml tag name ("application", in this case) as a replacement string for all local-name()s to get you to the expected output.
That was the OP's original line of code, I only quoted it, I did not question it, because how he gets an element from the tree is irrelevant to the question that was asked.
"//*[local-name() = '{0}']".format("application") results in "//*[local-name() = 'application'], which is valid XPath. It's not necessarily what I would do, and it's and indication that the OP's XML document contains namespaces and that he doesn't know how to handle them properly, but as I said, that's besides the point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.