url fetching from xml depending upon selection

Discussion in 'Python' started by shanti bhushan, May 18, 2010.

  1. I have a sample.XML file
    the code is like this

    <?xml version="1.0" encoding="UTF-8"?>
    <opml version="1.0">
    <head>
    <title>My Podcasts</title>
    <dateCreated>Sun, 07 Mar 2010 15:53:26

    GMT</dateCreated>
    <dateModified>Sun, 07 Mar 2010 15:53:26

    GMT</dateModified>
    </head>
    <body>
    <TestCase name="Sprint_001">
    <Input url="http://first.co.jp" />
    <Input url="http://www.google.com" />
    <Input url="http://www.epaper.times.india.com" />
    </TestCase>
    <TestCase name="Sprint_002">
    <Input url="http://second.co.jp" />
    <Input url="http://www.google.com" />
    <Input url="http://www.epaper.times.india.com" />
    </TestCase>
    <TestCase name="Sprint_003">
    <Input url="http://third.co.jp" />
    <Input url="http://www.google.com" />
    <Input url="http://www.epaper.times.india.com" />
    </TestCase>
    </body>
    </opml>


    This my python code
    from xml.etree import ElementTree

    with open('our.xml', 'rt') as f:
    tree = ElementTree.parse(f)

    for node, value in tree.findall('.//TestCase/Input'):

    url=node.attrib.get('url')
    print url



    i want to print the url depending on name="sprint_001".
    If i change my option to sprint_002 it should print url for sprint_002
     
    shanti bhushan, May 18, 2010
    #1
    1. Advertising

  2. On May 18, 10:18 am, shanti bhushan <> wrote:
    > I have a sample.XML file
    > the code is like this
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <opml version="1.0">
    > <head>
    >         <title>My Podcasts</title>
    >         <dateCreated>Sun, 07 Mar 2010 15:53:26
    >
    > GMT</dateCreated>
    >         <dateModified>Sun, 07 Mar 2010 15:53:26
    >
    > GMT</dateModified>
    > </head>
    > <body>
    >   <TestCase name="Sprint_001">
    >     <Input url="http://first.co.jp" />
    >     <Input url="http://www.google.com" />
    >     <Input url="http://www.epaper.times.india.com" />
    >   </TestCase>
    >   <TestCase name="Sprint_002">
    >     <Input url="http://second.co.jp" />
    >     <Input url="http://www.google.com" />
    >     <Input url="http://www.epaper.times.india.com" />
    >   </TestCase>
    >   <TestCase name="Sprint_003">
    >     <Input url="http://third.co.jp" />
    >     <Input url="http://www.google.com" />
    >     <Input url="http://www.epaper.times.india.com" />
    >   </TestCase>
    > </body>
    > </opml>
    >
    > This my python code
    > from xml.etree import ElementTree
    >
    > with open('our.xml', 'rt') as f:
    >     tree = ElementTree.parse(f)
    >
    > for node, value in tree.findall('.//TestCase/Input'):
    >
    >         url=node.attrib.get('url')
    >         print url
    >
    > i want to print the url depending on name="sprint_001".
    > If i change my option to sprint_002 it should print url for sprint_002


    this code print all URL but there is no option to select URLs
    depending upon my choice

    from xml.etree import ElementTree
    with open('our.xml', 'rt') as f:
    tree = ElementTree.parse(f)
    for node in tree.findall('.//TestCase/Input'):
    url=node.attrib.get('url')
    print url
     
    shanti bhushan, May 18, 2010
    #2
    1. Advertising

  3. On May 18, 10:18 am, shanti bhushan <> wrote:
    > I have a sample.XML file
    > the code is like this
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <opml version="1.0">
    > <head>
    >         <title>My Podcasts</title>
    >         <dateCreated>Sun, 07 Mar 2010 15:53:26
    >
    > GMT</dateCreated>
    >         <dateModified>Sun, 07 Mar 2010 15:53:26
    >
    > GMT</dateModified>
    > </head>
    > <body>
    >   <TestCase name="Sprint_001">
    >     <Input url="http://first.co.jp" />
    >     <Input url="http://www.google.com" />
    >     <Input url="http://www.epaper.times.india.com" />
    >   </TestCase>
    >   <TestCase name="Sprint_002">
    >     <Input url="http://second.co.jp" />
    >     <Input url="http://www.google.com" />
    >     <Input url="http://www.epaper.times.india.com" />
    >   </TestCase>
    >   <TestCase name="Sprint_003">
    >     <Input url="http://third.co.jp" />
    >     <Input url="http://www.google.com" />
    >     <Input url="http://www.epaper.times.india.com" />
    >   </TestCase>
    > </body>
    > </opml>
    >
    > This my python code
    > from xml.etree import ElementTree
    >
    > with open('our.xml', 'rt') as f:
    >     tree = ElementTree.parse(f)
    >
    > for node, value in tree.findall('.//TestCase/Input'):
    >
    >         url=node.attrib.get('url')
    >         print url
    >
    > i want to print the url depending on name="sprint_001".
    > If i change my option to sprint_002 it should print url for sprint_002




    i am trying to do with this code
    please correct my code

    from xml.etree import ElementTree

    with open('our.xml', 'rt') as f:
    tree = ElementTree.parse(f)

    for node in tree.findall('.//TestCase/Input'):
    if name=='Sprint_001':
    print node.attrib.get('url')

    i want to print only url that comes under name=Sprint_001
     
    shanti bhushan, May 18, 2010
    #3
  4. shanti bhushan, 18.05.2010 07:18:
    > I have a sample.XML file
    > the code is like this
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <opml version="1.0">
    > <head>
    > <title>My Podcasts</title>
    > <dateCreated>Sun, 07 Mar 2010 15:53:26
    >
    > GMT</dateCreated>
    > <dateModified>Sun, 07 Mar 2010 15:53:26
    >
    > GMT</dateModified>
    > </head>
    > <body>
    > <TestCase name="Sprint_001">
    > <Input url="http://first.co.jp" />
    > <Input url="http://www.google.com" />
    > <Input url="http://www.epaper.times.india.com" />
    > </TestCase>
    > <TestCase name="Sprint_002">
    > <Input url="http://second.co.jp" />
    > <Input url="http://www.google.com" />
    > <Input url="http://www.epaper.times.india.com" />
    > </TestCase>
    > <TestCase name="Sprint_003">
    > <Input url="http://third.co.jp" />
    > <Input url="http://www.google.com" />
    > <Input url="http://www.epaper.times.india.com" />
    > </TestCase>
    > </body>
    > </opml>
    >
    >
    > This my python code
    > from xml.etree import ElementTree
    >
    > with open('our.xml', 'rt') as f:
    > tree = ElementTree.parse(f)
    >
    > for node, value in tree.findall('.//TestCase/Input'):
    >
    > url=node.attrib.get('url')
    > print url
    >
    > i want to print the url depending on name="sprint_001".


    for test_case in tree.findall('.//TestCase'):
    if test_case.get('name') == 'sprint_002':
    for input_el in test_case:
    print input_el.get('url')

    Stefan
     
    Stefan Behnel, May 18, 2010
    #4
  5. On May 18, 10:18 am, shanti bhushan <> wrote:
    > I have a sample.XML file
    > the code is like this
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <opml version="1.0">
    > <head>
    >         <title>My Podcasts</title>
    >         <dateCreated>Sun, 07 Mar 2010 15:53:26
    >
    > GMT</dateCreated>
    >         <dateModified>Sun, 07 Mar 2010 15:53:26
    >
    > GMT</dateModified>
    > </head>
    > <body>
    >   <TestCase name="Sprint_001">
    >     <Input url="http://first.co.jp" />
    >     <Input url="http://www.google.com" />
    >     <Input url="http://www.epaper.times.india.com" />
    >   </TestCase>
    >   <TestCase name="Sprint_002">
    >     <Input url="http://second.co.jp" />
    >     <Input url="http://www.google.com" />
    >     <Input url="http://www.epaper.times.india.com" />
    >   </TestCase>
    >   <TestCase name="Sprint_003">
    >     <Input url="http://third.co.jp" />
    >     <Input url="http://www.google.com" />
    >     <Input url="http://www.epaper.times.india.com" />
    >   </TestCase>
    > </body>
    > </opml>
    >
    > This my python code
    > from xml.etree import ElementTree
    >
    > with open('our.xml', 'rt') as f:
    >     tree = ElementTree.parse(f)
    >
    > for node, value in tree.findall('.//TestCase/Input'):
    >
    >         url=node.attrib.get('url')
    >         print url
    >
    > i want to print the url depending on name="sprint_001".
    > If i change my option to sprint_002 it should print url for sprint_002



    i have tried some new things here

    from xml.etree import ElementTree

    with open('our.xml', 'rt') as f:
    tree = ElementTree.parse(f)

    for node in tree.findall('.//TestCase/'):
    print node.attrib.keys()
    print node.attrib.items()
    print node.attrib.get(1)

    url=node.attrib.get('url')
    print url



    but i am not getting idea ... how to achive my motive
     
    shanti bhushan, May 18, 2010
    #5
  6. On May 18, 12:04 pm, Stefan Behnel <> wrote:
    > shanti bhushan, 18.05.2010 07:18:
    >
    >
    >
    >
    >
    > > I have a sample.XML file
    > > the code is like this

    >
    > > <?xml version="1.0" encoding="UTF-8"?>
    > > <opml version="1.0">
    > > <head>
    > >    <title>My Podcasts</title>
    > >    <dateCreated>Sun, 07 Mar 2010 15:53:26

    >
    > > GMT</dateCreated>
    > >    <dateModified>Sun, 07 Mar 2010 15:53:26

    >
    > > GMT</dateModified>
    > > </head>
    > > <body>
    > >    <TestCase name="Sprint_001">
    > >      <Input url="http://first.co.jp" />
    > >      <Input url="http://www.google.com" />
    > >      <Input url="http://www.epaper.times.india.com" />
    > >    </TestCase>
    > >    <TestCase name="Sprint_002">
    > >      <Input url="http://second.co.jp" />
    > >      <Input url="http://www.google.com" />
    > >      <Input url="http://www.epaper.times.india.com" />
    > >    </TestCase>
    > >    <TestCase name="Sprint_003">
    > >      <Input url="http://third.co.jp" />
    > >      <Input url="http://www.google.com" />
    > >      <Input url="http://www.epaper.times.india.com" />
    > >    </TestCase>
    > > </body>
    > > </opml>

    >
    > > This my python code
    > > from xml.etree import ElementTree

    >
    > > with open('our.xml', 'rt') as f:
    > >      tree = ElementTree.parse(f)

    >
    > > for node, value in tree.findall('.//TestCase/Input'):

    >
    > >          url=node.attrib.get('url')
    > >          print url

    >
    > > i want to print the url depending on name="sprint_001".

    >
    >      for test_case in tree.findall('.//TestCase'):
    >          if test_case.get('name') == 'sprint_002':
    >             for input_el in test_case:
    >                 print input_el.get('url')
    >
    > Stefan- Hide quoted text -
    >
    > - Show quoted text -


    Hi Stevan
    i tried your logic i am not getting any out put for this
     
    shanti bhushan, May 18, 2010
    #6
  7. shanti bhushan, 18.05.2010 07:18:
    > I have a sample.XML file
    > the code is like this
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <opml version="1.0">
    > <head>
    > <title>My Podcasts</title>
    > <dateCreated>Sun, 07 Mar 2010 15:53:26
    >
    > GMT</dateCreated>
    > <dateModified>Sun, 07 Mar 2010 15:53:26
    >
    > GMT</dateModified>
    > </head>
    > <body>
    > <TestCase name="Sprint_001">
    > <Input url="http://first.co.jp" />
    > <Input url="http://www.google.com" />
    > <Input url="http://www.epaper.times.india.com" />
    > </TestCase>
    > <TestCase name="Sprint_002">
    > <Input url="http://second.co.jp" />
    > <Input url="http://www.google.com" />
    > <Input url="http://www.epaper.times.india.com" />
    > </TestCase>
    > <TestCase name="Sprint_003">
    > <Input url="http://third.co.jp" />
    > <Input url="http://www.google.com" />
    > <Input url="http://www.epaper.times.india.com" />
    > </TestCase>
    > </body>
    > </opml>
    >
    >
    > This my python code
    > from xml.etree import ElementTree
    >
    > with open('our.xml', 'rt') as f:
    > tree = ElementTree.parse(f)
    >
    > for node, value in tree.findall('.//TestCase/Input'):
    >
    > url=node.attrib.get('url')
    > print url
    >
    >
    >
    > i want to print the url depending on name="sprint_001".
    > If i change my option to sprint_002 it should print url for sprint_002


    Actually, try this:

    test_cases = dict(
    (test_case.get('name'), [ el.get('url') for el in test_case ])
    for test_case in tree.findall('.//TestCase') )

    print test_cases['Sprint_001'] # -> prints list of URLs

    That gives you a dict of test case names that are mapped to the list of
    their URLs.

    Stefan
     
    Stefan Behnel, May 18, 2010
    #7
  8. On May 18, 12:04 pm, Stefan Behnel <> wrote:
    > shanti bhushan, 18.05.2010 07:18:
    >
    >
    >
    >
    >
    > > I have a sample.XML file
    > > the code is like this

    >
    > > <?xml version="1.0" encoding="UTF-8"?>
    > > <opml version="1.0">
    > > <head>
    > >    <title>My Podcasts</title>
    > >    <dateCreated>Sun, 07 Mar 2010 15:53:26

    >
    > > GMT</dateCreated>
    > >    <dateModified>Sun, 07 Mar 2010 15:53:26

    >
    > > GMT</dateModified>
    > > </head>
    > > <body>
    > >    <TestCase name="Sprint_001">
    > >      <Input url="http://first.co.jp" />
    > >      <Input url="http://www.google.com" />
    > >      <Input url="http://www.epaper.times.india.com" />
    > >    </TestCase>
    > >    <TestCase name="Sprint_002">
    > >      <Input url="http://second.co.jp" />
    > >      <Input url="http://www.google.com" />
    > >      <Input url="http://www.epaper.times.india.com" />
    > >    </TestCase>
    > >    <TestCase name="Sprint_003">
    > >      <Input url="http://third.co.jp" />
    > >      <Input url="http://www.google.com" />
    > >      <Input url="http://www.epaper.times.india.com" />
    > >    </TestCase>
    > > </body>
    > > </opml>

    >
    > > This my python code
    > > from xml.etree import ElementTree

    >
    > > with open('our.xml', 'rt') as f:
    > >      tree = ElementTree.parse(f)

    >
    > > for node, value in tree.findall('.//TestCase/Input'):

    >
    > >          url=node.attrib.get('url')
    > >          print url

    >
    > > i want to print the url depending on name="sprint_001".

    >
    >      for test_case in tree.findall('.//TestCase'):
    >          if test_case.get('name') == 'sprint_002':
    >             for input_el in test_case:
    >                 print input_el.get('url')
    >
    > Stefan- Hide quoted text -
    >
    > - Show quoted text -


    Great Help Stefan -
    my regards to you
    thankyou once again it waorks fine ...
     
    shanti bhushan, May 18, 2010
    #8
  9. shanti bhushan, 18.05.2010 10:08:
    > On May 18, 12:04 pm, Stefan Behnel<> wrote:
    >> shanti bhushan, 18.05.2010 07:18:
    >>> [...]
    >>> <body>
    >>> <TestCase name="Sprint_001">
    >>> <Input url="http://first.co.jp" />
    >>> <Input url="http://www.google.com" />
    >>> <Input url="http://www.epaper.times.india.com" />
    >>> </TestCase>
    >>> <TestCase name="Sprint_002">
    >>> <Input url="http://second.co.jp" />
    >>> <Input url="http://www.google.com" />
    >>> <Input url="http://www.epaper.times.india.com" />
    >>> </TestCase>
    >>> <TestCase name="Sprint_003">
    >>> <Input url="http://third.co.jp" />
    >>> <Input url="http://www.google.com" />
    >>> <Input url="http://www.epaper.times.india.com" />
    >>> </TestCase>
    >>> </body>
    >>> </opml>

    >>
    >>> This my python code
    >>> from xml.etree import ElementTree

    >>
    >>> with open('our.xml', 'rt') as f:
    >>> tree = ElementTree.parse(f)

    >>
    >>> for node, value in tree.findall('.//TestCase/Input'):

    >>
    >>> url=node.attrib.get('url')
    >>> print url

    >>
    >>> i want to print the url depending on name="sprint_001".

    >>
    >> for test_case in tree.findall('.//TestCase'):
    >> if test_case.get('name') == 'sprint_002':
    >> for input_el in test_case:
    >> print input_el.get('url')
    >>

    > i tried your logic i am not getting any out put for this


    Obviously, because the correct name value is 'Sprint_002', not
    'sprint_002'. Please ask back if you need help in understanding the code I
    posted.

    Stefan
     
    Stefan Behnel, May 18, 2010
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. misiek
    Replies:
    7
    Views:
    6,835
    misiek
    Feb 28, 2006
  2. param
    Replies:
    3
    Views:
    342
    Ivan Vecerina
    Feb 23, 2006
  3. curiousity

    HOWTO autopost the selection list upon selection

    curiousity, Nov 21, 2003, in forum: ASP .Net Mobile
    Replies:
    0
    Views:
    271
    curiousity
    Nov 21, 2003
  4. Replies:
    1
    Views:
    134
  5. rajasekhar
    Replies:
    5
    Views:
    89
    -Lost
    Apr 21, 2007
Loading...

Share This Page