Python Forum
Python - joining xmls together invalid token error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python - joining xmls together invalid token error
#1
I have a function which makes get request, parse xml file and returns:

def get_object(object_name):
...
    encoded_text = response.text.encode('utf-8', 'replace')
    root = ET.fromstring(encoded_text)
    tree = ET.ElementTree(root)
    return tree
Then I loop through tasks with this function and want to put it inside one variable:

jx_task_tree = ''
for jx in jx_tasks_lst:
    jx_task_tree += str(get_object(jx))
    jx_task_tree += ('\n')
Then I want to parse jx_task_tree:

root = ET.fromstring(jx_task_tree).encode('utf-8')
tree_out = ET.tostring(root)
print(tree_out)
But it throws me an error:

File "import_uac_wf.py", line 51, in <module>
   root = ET.fromstring(jx_task_tree).encode('utf-8')
File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 1300, in XML
   parser.feed(text)
File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 1642, in feed
   self._raiseerror(v)
File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 1506, in 
   _raiseerror
raise err
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, 
column 42
When using this:

parser = ET.XMLParser(encoding="utf-8")
tree = ET.parse(jx_tasks_lst, parser=parser)
print(ET.tostring(tree.getroot()))
There is error:

Traceback (most recent call last):
  File "import_uac_wf.py", line 57, in <module>
    tree = ET.parse(jx_tasks_lst, parser=parser)
  File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 1182, in parse
    tree.parse(source, parser)
 File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 647, in parse
    source = open(source, "rb")
TypeError: coercing to Unicode: need string or buffer, list found
Can anybody help what am I doing wrong or how to fix it?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error: invalid command 'egg_info' TimTu 0 958 Jul-27-2023, 07:30 AM
Last Post: TimTu
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 11,040 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  Joining two jupyter notebooks and getting an error! Led_Zeppelin 1 1,142 Oct-20-2022, 04:28 PM
Last Post: deanhystad
  Invalid format specifier Error Led_Zeppelin 2 7,840 Jul-11-2022, 03:55 PM
Last Post: Led_Zeppelin
  Refresh token for Wyze SDK duckredbeard 0 1,111 May-16-2022, 04:33 AM
Last Post: duckredbeard
  Need to sign JWT token with JWK key stucoder 1 1,692 Feb-21-2022, 09:04 AM
Last Post: stucoder
  Python requests oauth2 access token herobpv 6 3,927 Sep-27-2021, 06:54 PM
Last Post: herobpv
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 3,175 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  Unexplained Invalid syntax Error cybertooth 5 3,264 Aug-02-2021, 10:05 AM
Last Post: cybertooth
  unexpected token < in json at position 0 Frodoxzibit 5 2,807 Jul-10-2021, 09:33 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020