?
Solved

get a specific value mentioned in the XML output

Posted on 2005-03-17
9
Medium Priority
?
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

 

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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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

777 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