Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

get a specific value mentioned in the XML output

Posted on 2005-03-17
9
Medium Priority
?
228 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:tn_bobbie
  • 5
  • 4
9 Comments
 
LVL 14

Expert Comment

by:RichieHindle
ID: 13564112
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
0
 

Author Comment

by:tn_bobbie
ID: 13564164
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
0
 
LVL 14

Expert Comment

by:RichieHindle
ID: 13564270
It works for me on both Linux and Windows.  Did you accidentally introduce some leading spaces into the first line?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:tn_bobbie
ID: 13564323
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..
0
 

Author Comment

by:tn_bobbie
ID: 13564333
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.
0
 
LVL 14

Expert Comment

by:RichieHindle
ID: 13564592
> 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)
0
 
LVL 14

Expert Comment

by:RichieHindle
ID: 13564738
Sorry, there was a typo there.  It should be:

for match in re.finditer(r'<response[^>]*>([^<]*)</response>', XML):
    print match.group(1)
0
 
LVL 14

Accepted Solution

by:
RichieHindle earned 400 total points
ID: 13565332
I just realised that the reason you got the syntax error might be that you're using a very old version of Python on your Linux machine.  Here's a version that works with Python 1.5.2:

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.findall(r'<response[^>]*>([^<]*)</response>', XML):
    print match
0
 

Author Comment

by:tn_bobbie
ID: 13571935
Thanks a lot RichieHindle. That works fine.

Bobbie
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question