B
B
beduin012019-09-02 18:22:07
Python
beduin01, 2019-09-02 18:22:07

How to get XML from xpath Element?

I need to get XML from xpath Element.
afile.xml:

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

The code:
etxml = etree.parse(afile)
    root = etxml.getroot()

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

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

And I need to get XML from this

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
ScriptKiddo, 2019-09-02
@ScriptKiddo

Docs
Element to String Element
Text
test.xml

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

Python 3
from lxml import etree

etxml = etree.parse('test.xml')

root = etxml.getroot()
values = root.xpath("//*[local-name() = 'journalNumber']")


for value in values:
    print('XML: ', etree.tostring(value, encoding='unicode'))
    print('Text: ', value.text)

Output
XML:  <journalNumber>1</journalNumber>
    
Text:  1
XML:  <journalNumber>2</journalNumber>
    
Text:  2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question