Link to home
Start Free TrialLog in
Avatar of tn_bobbie
tn_bobbie

asked on

get a specific value mentioned in the XML output

Hi Guys,
My application returns a reponse in this way
---------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <response retcode="0" status="1">Customer already exist</response>
</root>

---------------------------------------------------------------

Now I need to get the value "Customer already exist" & store in a output table. How do I break this?
Can some one plz help me doing this? I badly need this...

Thanks
Bobbie
Avatar of RichieHindle
RichieHindle

Here's a working example:

import xml.dom.minidom as dom
XML = """\
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <response retcode="0" status="1">Customer already exist</response>
</root>"""

doc = dom.parseString(XML)
for response in doc.getElementsByTagName("response"):
    textNode = response.childNodes[0]
    print textNode.data
Avatar of tn_bobbie

ASKER

Hi RichieHindle,
Thanks for the help, but unfortunately it gives a syntax error.
see this. I work in Linux

[root@host247 python]# python test.py
  File "test.py", line 1
    import xml.dom.minidom as dom
                            ^
SyntaxError: invalid syntax
[root@host247 python]#

Thanks
Bobby
It works for me on both Linux and Windows.  Did you accidentally introduce some leading spaces into the first line?
It works fine in windows, but not in linux. Should I have the whole XML data in one single line? Ofcourse I tried having XML data in one line also..
But one thing is that the python on my server is not configured to work with XML DOM parsers. So using strings will be very much hepful to me.
> Should I have the whole XML data in one single line?

That won't make any difference.

> the python on my server is not configured to work with XML DOM parsers

That won't make it give a syntax error, but it will stop it from working.

> using strings will be very much hepful to me.

Here's another version, which uses string processing rather than XML parsing:

import re

XML = """\
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <response retcode="0" status="1">Customer already exist</response>
</root>"""

for match in re.finditer(r'<response[^>]*>([^[<]*)</response>', XML):
    print match.group(1)
Sorry, there was a typo there.  It should be:

for match in re.finditer(r'<response[^>]*>([^<]*)</response>', XML):
    print match.group(1)
ASKER CERTIFIED SOLUTION
Avatar of RichieHindle
RichieHindle

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks a lot RichieHindle. That works fine.

Bobbie