简体   繁体   中英

invalid token in xml string, fail to create element tree python

I am stuck on a problem that is probably weasy to solve for anyone else. I am trying to create an element tree from an xml string received over socket, not from a file.

Method:

This python script below is a socket client that receives a python string (happens to be xml) that was created by a c++ server using tinyxml.

program steps: 1) create socket 2) receive xml string 3) parse xml into an element tree which can be used elsewhere

Problem:

the function fromstring() can't seem to figure it out. Here is my code:

import socket
import sys
import struct
import binascii
import io
import re
from xml.etree import ElementTree

#illegal characters to remove from string later before going to xml
RE_XML_ILLEGAL = u'([\u0000-\u0008\u000b-\u000c\u000e-\u001f\ufffe-\uffff])' + \
             u'|' + \
             u'([%s-%s][^%s-%s])|([^%s-%s][%s-%s])|([%s-%s]$)|(^[%s-%s])' % \
              (unichr(0xd800),unichr(0xdbff),unichr(0xdc00),unichr(0xdfff),
               unichr(0xd800),unichr(0xdbff),unichr(0xdc00),unichr(0xdfff),
               unichr(0xd800),unichr(0xdbff),unichr(0xdc00),unichr(0xdfff))

HOST = 'localhost'   
PORT = 50008

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
print 'Socket now connecting'
s.connect((HOST,PORT))
s.send('1')#as long as we are not sending "0" cpp server will return information.          

#declare global xml object "root"
global root

while 1:
    data = s.recv(1024)#receive the initial message
    data3 = data[:3]#get first 3 letters
    if (data3 == "New"):
        #get ready for new packet
        nextsizestring = data[3:]
        nextsizestring2 = nextsizestring.rstrip('\0')
        nextsize = int(nextsizestring2,10)
        s.send('b')#tell cpp we are ready for the packet

        databuf = s.recv(nextsize)#data buffer as a python string
        databuf2 = re.sub(RE_XML_ILLEGAL, "?", databuf)#remove illegal xml characters
        print(databuf2)
        root = ElementTree.ElementTree(ElementTree.fromstring(databuf2))#convert to element tree
        print(root)

    elif (data3 != "New"):
        print("WARNING! TCP SYNCH HAS FAILED")
    if not data: break#if not data then stop listening for more

    s.send('b')#keep sending anything but zero to get more stuff
conn.close()
s.close()

And here is the output:

Socket created
Socket now connecting
<Frame>
     <FrameNumber ="1509677" />
     <Time ="27427839" />
     <Forceplatedata>
          <Forceplate_0>
               <Subframe#_0>
                    <F_x ="0" />
                    <F_y ="0" />
                    <F_z ="0" />
               </Subframe#_0>
.
.
.
</Frame>

Traceback (most recent call last):
  File "<string>", line 11, in <module>
  File "C:\Users\Gelsey Torres-   Oviedo\Desktop\VizardFolderVRServer\Python2CPP_Client_rev1.py", line 50, in <module>
    root = ElementTree.ElementTree(ElementTree.fromstring(databuf2))
  File "C:\Program Files (x86)\WorldViz\Vizard4\bin\lib\xml\etree\ElementTree.py", line     1282, in XML
    parser.feed(text)
  File "C:\Program Files (x86)\WorldViz\Vizard4\bin\lib\xml\etree\ElementTree.py", line 1624, in feed
    self._raiseerror(v)
  File "C:\Program Files (x86)\WorldViz\Vizard4\bin\lib\xml\etree\ElementTree.py", line 1488, in _raiseerror
    raise err
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 2, column 18

I took the liberty of truncating the xml string above since it is quite long. As you can see in the error, it looks like it is having a problem with line 2 col 18, which I think is the space " " character. I do not understand why this is happening.

Failed Solutions:

1) pass string as stringIO to parse() 2) several variations of encode and decode utf-8 3) similar methods with minidom

I guessing it's a syntax problem? I'm probably doing something really stupid...

What Senshin said was the key issue. I was creating xml with bad format.

By changing all the the places where it looks like

<FrameNumber ="1381949" />

to

<FrameNumber attribute="1381949" />

The program can now create the element tree.

I knew it was something simple like that, Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM