xml handling treat strings as if they were files

2

I'm getting a xml in the format of string in my response. I want to manipulate this xml and generate a list of the <item> tags of the xml returned by the web service. Here is the snippet of my code:

if (op == 2):

    print '\n'*3
    print 'TESTE Viagem'; sleep(2);
    print '\n'*3

    response = oRodov.viagem() #xml

    tree = ET.parse(response)
    root = tree.getroot()
    iterador = root.getiterator()
    for x in iterador:
        if x.tag == "item":
            print x.items()

The code is returning me a IOError

Traceback (most recent call last):
  File "rodoviario.py", line 134, in ?
    tree = ET.parse(response)
  File "/usr/local/lib/python2.4/site-packages/elementtree/ElementTree.py", line 1120, in parse
    tree.parse(source, parser)
  File "/usr/local/lib/python2.4/site-packages/elementtree/ElementTree.py", line 642, in parse
    source = open(source, "rb")
IOError: [Errno 2] No such file or directory: '<?xml version="1.0" encoding="ISO-8859-1"?>\n\n<viagem origem="1" destino="2" data="2014-07-02" servico="2311" grupo="DEMON">\n <saida dia="0" hora="02:00" />\n <chegada dia="0" hora="12:15" />\n <empresa>DEMON</empresa>\n <mensagem-servico></mensagem-servico>\n <destino>2</destino>\n <moeda>R$</moeda>\n <preco>51.10</preco>\n <layout>\n  <secao nome="Unica">\n   <item x="0" y="0">\n    <assento ocupado="0" tipo=" " numero="01" />\n   </item>\n   <item x="1" y="0">\n    <assento ocupado="0" tipo=" " numero="05" />\n   </item>\n   <item x="2" y="0">\n    <assento ocupado="0" tipo=" " numero="09" />\n   </item>\n   <item x="3" y="0">\n    <assento ocupado="0" tipo=" " numero="13" />\n   </item>\n   <item x="4" y="0">\n    <assento ocupado="0" tipo=" " numero="17" />\n   </item>\n   <item x="5" y="0">\n    <assento ocupado="0" tipo=" " numero="21" />\n   </item>\n   <item x="6" y="0">\n    <assento ocupado="0" tipo=" " numero="25" />\n   </item>\n   <item x="7" y="0">\n    <assento ocupado="0" tipo=" " numero="29" />\n   </item>\n   <item x="8" y="0">\n    <assento ocupado="0" tipo=" " numero="33" />\n   </item>\n   <item x="9" y="0">\n    <assento ocupado="0" tipo=" " numero="37" />\n   </item>\n   <item x="10" y="0">\n    <assento ocupado="0" tipo=" " numero="42" />\n   </item>\n   <item x="0" y="1">\n    <assento ocupado="0" tipo=" " numero="02" />\n   </item>\n   <item x="1" y="1">\n    <assento ocupado="0" tipo=" " numero="06" />\n   </item>\n   <item x="2" y="1">\n    <assento ocupado="0" tipo=" " numero="10" />\n   </item>\n   <item x="3" y="1">\n    <assento ocupado="0" tipo=" " numero="14" />\n   </item>\n   <item x="4" y="1">\n    <assento ocupado="0" tipo=" " numero="18" />\n   </item>\n   <item x="5" y="1">\n    <assento ocupado="0" tipo=" " numero="22" />\n   </item>\n   <item x="6" y="1">\n    <assento ocupado="0" tipo=" " numero="26" />\n   </item>\n   <item x="7" y="1">\n    <assento ocupado="0" tipo=" " numero="30" />\n   </item>\n   <item x="8" y="1">\n    <assento ocupado="0" tipo=" " numero="34" />\n   </item>\n   <item x="9" y="1">\n    <assento ocupado="0" tipo=" " numero="38" />\n   </item>\n   <item x="10" y="1">\n    <assento ocupado="0" tipo=" " numero="41" />\n   </item>\n   <item x="12" y="2">\n    <assento ocupado="0" tipo=" " numero="04" />\n   </item>\n   <item x="1" y="3">\n    <assento ocupado="0" tipo=" " numero="08" />\n   </item>\n   <item x="2" y="3">\n    <assento ocupado="0" tipo=" " numero="12" />\n   </item>\n   <item x="3" y="3">\n    <assento ocupado="0" tipo=" " numero="16" />\n   </item>\n   <item x="4" y="3">\n    <assento ocupado="0" tipo=" " numero="20" />\n   </item>\n   <item x="5" y="3">\n    <assento ocupado="0" tipo=" " numero="24" />\n   </item>\n   <item x="6" y="3">\n    <assento ocupado="0" tipo=" " numero="28" />\n   </item>\n   <item x="7" y="3">\n    <assento ocupado="0" tipo=" " numero="32" />\n   </item>\n   <item x="8" y="3">\n    <assento ocupado="0" tipo=" " numero="36" />\n   </item>\n   <item x="9" y="3">\n    <assento ocupado="0" tipo=" " numero="40" />\n   </item>\n   <item x="10" y="3">\n    <assento ocupado="0" tipo=" " numero="44" />\n   </item>\n   <item x="0" y="4">\n    <assento ocupado="0" tipo=" " numero="03" />\n   </item>\n   <item x="1" y="4">\n    <assento ocupado="0" tipo=" " numero="07" />\n   </item>\n   <item x="2" y="4">\n    <assento ocupado="0" tipo=" " numero="11" />\n   </item>\n   <item x="3" y="4">\n    <assento ocupado="0" tipo=" " numero="15" />\n   </item>\n   <item x="4" y="4">\n    <assento ocupado="0" tipo=" " numero="19" />\n   </item>\n   <item x="5" y="4">\n    <assento ocupado="0" tipo=" " numero="23" />\n   </item>\n   <item x="6" y="4">\n    <assento ocupado="0" tipo=" " numero="27" />\n   </item>\n   <item x="7" y="4">\n    <assento ocupado="0" tipo=" " numero="31" />\n   </item>\n   <item x="8" y="4">\n    <assento ocupado="0" tipo=" " numero="35" />\n   </item>\n   <item x="9" y="4">\n    <assento ocupado="0" tipo=" " numero="39" />\n   </item>\n   <item x="10" y="4">\n    <assento ocupado="0" tipo=" " numero="43" />\n   </item>\n  </secao>\n </layout>\n</viagem>\n'

I have identified that this error is happening because I am not reporting a xml file directory but passing xml to string format:

tree = ET.parse(response)

But I do not want to have to save this string as a file xml to then make parse and manipulate the file.

How do I manipulate this string as xml without having to save it as a file in some directory?!

    
asked by anonymous 01.07.2014 / 19:27

1 answer

2

Many Python libraries expect files or " file-likes " as parameters. While this may seem restrictive, there is a specific built-in to treat strings as if they were files: o StringIO (or its alternate implementation, cStringIO ).

from StringIO import StringIO
tree = ET.parse(StringIO(response))

Explaining: StringIO(string) creates a " file-like " (i.e. object that behaves as if it were a file) whose "content" is the string passed as an argument. This object implements the entire interface of a file, with methods to open, read, write, etc. Passing this object to ET.parse allows it to "open" and "read" it normally, accessing its content without the need to create an intermediate file.

    
01.07.2014 / 19:58