25 Jun 2012 14:10
[groovy-user] Parsing HTTPBuilder response
Hi,
I'm using RESTClient to communicate with a WS, and I'm having trouble parsing the response.
According to the API of the WS, the response looks like this:
[code]
HTTP/1.1 201 Created Content-Type: application/xml Content-Length: 1183 <?xml version='1.0' encoding='utf-8' ?> <instance href='http://localhost:3001/api/instances/i-cbb861aa' id='i-cbb861aa'> <name>ami-f51aff9c</name> <owner_id>393485797142</owner_id> <image href='http://localhost:3001/api/images/ami-f51aff9c' id='ami-f51aff9c'></image> <realm href='http://localhost:3001/api/realms/us-east-1c' id='us-east-1c'></realm> <state>PENDING</state> <hardware_profile href='http://localhost:3001/api/hardware_profiles/c1.medium' id='c1.medium'> </hardware_profile> <actions> <link href='http://localhost:3001/api/instances/i-cbb861aa/stop' method='post' rel='stop' /> <link href='http://localhost:3001/api/instances/i-cbb861aa/run;id=i-cbb861aa' method='post' rel='run' /> </actions> <launch_time>2011-07-22T16:09:45.000Z</launch_time> <public_addresses></public_addresses> <private_addresses></private_addresses> <firewalls> <firewall href='http://localhost:3001/api/firewalls/test' id='test'></firewall> <firewall href='http://localhost:3001/api/firewalls/default' id='default'></firewall> </firewalls> <authentication type='key'> <login> <keyname>eftah</keyname> </login> </authentication> </instance> [/code]
I want to obtain the id "i-cbb861aa", which is found in the second line:
<instance href='http://localhost:3001/api/instances/i-cbb861aa' id='i-cbb861aa'>
My code looks like:
[code]
String response="" def d = new RESTClient( 'http://localhost:3001/' ) d.post( path:'api/instances',query: [image_id:'xxxx',hwp_id:'xxxx',keyname:'xxx']){ resp, xml -> response = xml } [/code]
And I've tried every possibility I can think of, with no success:
xml.-6R4OnXMdPX4@public.gmane.org()
xml.-sWXNVSUVqxpqoCd7debKuQ@public.gmane.org()
xml.instance.-6R4OnXMdPX4@public.gmane.org()
xml.instance.-sWXNVSUVqxpqoCd7debKuQ@public.gmane.org()
xml.instance[0]. <at> id.text()
xml.instance[0]. <at> id.text.text()
Could somebody give me a hint?
It must be really simple to obtain it, but I can't figure out how.
Thanks a lot,
Dario.
I'm using RESTClient to communicate with a WS, and I'm having trouble parsing the response.
According to the API of the WS, the response looks like this:
[code]
HTTP/1.1 201 Created Content-Type: application/xml Content-Length: 1183 <?xml version='1.0' encoding='utf-8' ?> <instance href='http://localhost:3001/api/instances/i-cbb861aa' id='i-cbb861aa'> <name>ami-f51aff9c</name> <owner_id>393485797142</owner_id> <image href='http://localhost:3001/api/images/ami-f51aff9c' id='ami-f51aff9c'></image> <realm href='http://localhost:3001/api/realms/us-east-1c' id='us-east-1c'></realm> <state>PENDING</state> <hardware_profile href='http://localhost:3001/api/hardware_profiles/c1.medium' id='c1.medium'> </hardware_profile> <actions> <link href='http://localhost:3001/api/instances/i-cbb861aa/stop' method='post' rel='stop' /> <link href='http://localhost:3001/api/instances/i-cbb861aa/run;id=i-cbb861aa' method='post' rel='run' /> </actions> <launch_time>2011-07-22T16:09:45.000Z</launch_time> <public_addresses></public_addresses> <private_addresses></private_addresses> <firewalls> <firewall href='http://localhost:3001/api/firewalls/test' id='test'></firewall> <firewall href='http://localhost:3001/api/firewalls/default' id='default'></firewall> </firewalls> <authentication type='key'> <login> <keyname>eftah</keyname> </login> </authentication> </instance> [/code]
I want to obtain the id "i-cbb861aa", which is found in the second line:
<instance href='http://localhost:3001/api/instances/i-cbb861aa' id='i-cbb861aa'>
My code looks like:
[code]
String response="" def d = new RESTClient( 'http://localhost:3001/' ) d.post( path:'api/instances',query: [image_id:'xxxx',hwp_id:'xxxx',keyname:'xxx']){ resp, xml -> response = xml } [/code]
And I've tried every possibility I can think of, with no success:
xml.-6R4OnXMdPX4@public.gmane.org()
xml.-sWXNVSUVqxpqoCd7debKuQ@public.gmane.org()
xml.instance.-6R4OnXMdPX4@public.gmane.org()
xml.instance.-sWXNVSUVqxpqoCd7debKuQ@public.gmane.org()
xml.instance[0]. <at> id.text()
xml.instance[0]. <at> id.text.text()
Could somebody give me a hint?
It must be really simple to obtain it, but I can't figure out how.
Thanks a lot,
Dario.
RSS Feed