M
M
Maxim2020-08-20 09:59:04
Python
Maxim, 2020-08-20 09:59:04

How to remove xml declaration for parsing with lxml objectify?

There is an xml response from the view server:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<A>
  <url replyId="fb71e2db-2912-44c3-aab6-e92ced783e11">http://localhost:8080/opt/out/Ticket/3</url>
  <url replyId="4ca8fbae-b05f-42bf-bf22-16fa7d2a2b0b">http://localhost:8080/opt/out/Ticket/5</url>
  <url replyId="e3cdc104-98a9-4b45-941a-559649c6969a">http://localhost:8080/opt/out/Ticket/6</url>
  <ver>1</ver>
</A>

when trying to parse this response using the lxml.fromstring method, an error occurs
ValueError: Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration.

Obviously, it's in the very first line:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>

but I can't get rid of it. thanks in advance

UPD
python code
import requests
from lxml import objectify as ob

r = requests.get('http://localhost:8080/opt/out')
obj = ob.fromstring(r.text)


UPD
There is an idea to parse the response with a regular expression, removing the format declaration. Thereby preparing the data for parsing. But, for sure there is a solution, more concise and simple.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-08-20
@maxpointn2point

You have r.text, you can either remove the first line from it (if there are line breaks) or stupidly replace declaration with an empty string.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question