only a simple xml reader <tag:id>value</tag:id>

Discussion in 'Python' started by martijn@gamecreators.nl, Feb 8, 2006.

  1. Guest

    H!,

    Is it possible to get a <tag:id>value</tag:id> value ?

    When I do this:
    -----------------------------------------------------
    theXML = """<?xml version="1.0"?>
    <title>The Fascist Menace</title>
    """
    import xml.dom.minidom as dom
    doc = dom.parseString(theXML)
    print doc.getElementsByTagName('title')[0].toxml()

    I get : <title>The Fascist Menace</title> thats oke for me
    -----------------------------------------------------

    But the xmlfile I must read have other tags:
    theXML = """<?xml version="1.0"?>
    <title:id>The Fascist Menace</title:id>
    <title:name>bla la etc</title:name>
    """

    how to get that values ?
    I try things like:
    print doc.getElementsByTagName('title:id')[0].toxml() <--error

    Thanks,
    GC-Martijn
    , Feb 8, 2006
    #1
    1. Advertising

  2. Guest

    wrote:
    > H!,
    >
    > Is it possible to get a <tag:id>value</tag:id> value ?
    >
    > When I do this:
    > -----------------------------------------------------
    > theXML = """<?xml version="1.0"?>
    > <title>The Fascist Menace</title>
    > """
    > import xml.dom.minidom as dom
    > doc = dom.parseString(theXML)
    > print doc.getElementsByTagName('title')[0].toxml()
    >
    > I get : <title>The Fascist Menace</title> thats oke for me
    > -----------------------------------------------------
    >
    > But the xmlfile I must read have other tags:
    > theXML = """<?xml version="1.0"?>
    > <title:id>The Fascist Menace</title:id>
    > <title:name>bla la etc</title:name>
    > """
    >
    > how to get that values ?
    > I try things like:
    > print doc.getElementsByTagName('title:id')[0].toxml() <--error


    Addressing your general question, unfortunately you're a bit stuck.
    Minidom is rather confused about whether or not it's a namespace aware
    library. Addressing your specific example, I strongly advise you not
    to use documents that are not well-formed according to Namespaces 1.0.
    Your second example is a well-formed XML 1.0 external parsed entity,
    but not a well-formed XML 1.0 document entity, because it has multiple
    elements at document level. It's also not well-formed according to
    XMLNS 1.0 unless you declare the "title" prefix. You will not be able
    to use a non XMLNS 1.0 document with most XML technologies, including
    XSLT, WXS, RELAX NG, etc.

    If you have indeed declared a namespace and are just giving us a very
    bad example, use:

    print doc.getElementsByTagNameNS(title_namespace, 'id')

    --
    Uche Ogbuji Fourthought, Inc.
    http://uche.ogbuji.net http://fourthought.com
    http://copia.ogbuji.net http://4Suite.org
    Articles: http://uche.ogbuji.net/tech/publications/
    , Feb 8, 2006
    #2
    1. Advertising

  3. Guest

    I'm newbie with that xml stuff.

    The only thing I must read is the response I get from a EPP server.
    A response like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <epp xmlns=" http://www.eurid.eu/xml/epp/epp-1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:contact="http://www.eurid.eu/xml/epp/contact-1.0"
    xmlns:domain="http://www.eurid.eu/xml/epp/domain-1.0"
    xmlns:eurid="http://www.eurid.eu/xml/epp/eurid-1.0"
    xmlns:nsgroup="http://www.eurid.eu/xml/epp/nsgroup-1.0"
    xsi:schemaLocation="http://www.eurid.eu/xml/epp/epp-1.0 epp-1.0.xsd
    http://www.eurid.eu/xml/epp/contact-1.0 contact-1.0.xsd
    http://www.eurid.eu/xml/epp/domain-1.0 domain-1.0.xsd
    http://www.eurid.eu/xml/epp/eurid-1.0 eurid-1.0.xsd
    http://www.eurid.eu/xml/epp/nsgroup-1.0 nsgroup-1.0.xsd">
    <response>
    <result code="1500">
    <msg>Command completed successfully; ending session</msg>
    </result>
    <resData>
    <domain:appData>
    <domain:name>c-and-a.eu</domain:name>
    <domain:reference> c-and-a_1</domain:reference>
    <domain:code>2565100006029999</domain:code>
    <domain:crDate>2005-11-08T14:51:08.929Z</domain:crDate>
    </domain:appData>
    </resData>
    <extension>
    <eurid:ext>
    <eurid:result>
    <eurid:msg>OK</dnsbe:msg>
    </eurid:result>
    </eurid:ext>
    </extension>
    <trID>
    <clTRID>clientref-12310026</clTRID>
    <svTRID>eurid-1589</svTRID>
    </trID>
    </response>
    </epp>
    ----------------
    //<result code="1500">
    //<msg>Command completed successfully; ending session</msg>

    what is the official/best way to handle/parse such xml response ?

    Thats maybe a better question
    , Feb 8, 2006
    #3
  4. wrote:

    >I'm newbie with that xml stuff.
    >
    >The only thing I must read is the response I get from a EPP server.
    >A response like this:
    >
    ><?xml version="1.0" encoding="UTF-8"?>
    ><epp xmlns=" http://www.eurid.eu/xml/epp/epp-1.0"
    >xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    >xmlns:contact="http://www.eurid.eu/xml/epp/contact-1.0"
    >xmlns:domain="http://www.eurid.eu/xml/epp/domain-1.0"
    >xmlns:eurid="http://www.eurid.eu/xml/epp/eurid-1.0"
    >xmlns:nsgroup="http://www.eurid.eu/xml/epp/nsgroup-1.0"
    >xsi:schemaLocation="http://www.eurid.eu/xml/epp/epp-1.0 epp-1.0.xsd
    >http://www.eurid.eu/xml/epp/contact-1.0 contact-1.0.xsd
    >http://www.eurid.eu/xml/epp/domain-1.0 domain-1.0.xsd
    >http://www.eurid.eu/xml/epp/eurid-1.0 eurid-1.0.xsd
    >http://www.eurid.eu/xml/epp/nsgroup-1.0 nsgroup-1.0.xsd">
    ><response>
    ><result code="1500">
    ><msg>Command completed successfully; ending session</msg>
    ></result>
    ><resData>
    ><domain:appData>
    ><domain:name>c-and-a.eu</domain:name>
    ><domain:reference> c-and-a_1</domain:reference>
    ><domain:code>2565100006029999</domain:code>
    ><domain:crDate>2005-11-08T14:51:08.929Z</domain:crDate>
    ></domain:appData>
    ></resData>
    ><extension>
    ><eurid:ext>
    ><eurid:result>
    ><eurid:msg>OK</dnsbe:msg>
    ></eurid:result>
    ></eurid:ext>
    ></extension>
    ><trID>
    ><clTRID>clientref-12310026</clTRID>
    ><svTRID>eurid-1589</svTRID>
    ></trID>
    ></response>
    ></epp>
    >----------------
    >//<result code="1500">
    >//<msg>Command completed successfully; ending session</msg>
    >
    >what is the official/best way to handle/parse such xml response ?
    >
    >Thats maybe a better question
    >
    >
    >

    try something like this:

    import xml.dom.minidom as dom
    doc = dom.parseString("""<?xml version="1.0"?>
    <title>The Fascist Menace</title>""")
    print doc.getElementsByTagName('title')[0].childNodes[0].data
    Albert Leibbrandt, Feb 9, 2006
    #4
  5. Guest

    wrote:
    > The only thing I must read is the response I get from a EPP server.
    > A response like this:
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <epp xmlns=" http://www.eurid.eu/xml/epp/epp-1.0"
    > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    > xmlns:contact="http://www.eurid.eu/xml/epp/contact-1.0"
    > xmlns:domain="http://www.eurid.eu/xml/epp/domain-1.0"
    > xmlns:eurid="http://www.eurid.eu/xml/epp/eurid-1.0"
    > xmlns:nsgroup="http://www.eurid.eu/xml/epp/nsgroup-1.0"
    > xsi:schemaLocation="http://www.eurid.eu/xml/epp/epp-1.0 epp-1.0.xsd
    > http://www.eurid.eu/xml/epp/contact-1.0 contact-1.0.xsd
    > http://www.eurid.eu/xml/epp/domain-1.0 domain-1.0.xsd
    > http://www.eurid.eu/xml/epp/eurid-1.0 eurid-1.0.xsd
    > http://www.eurid.eu/xml/epp/nsgroup-1.0 nsgroup-1.0.xsd">
    > <response>
    > <result code="1500">
    > <msg>Command completed successfully; ending session</msg>
    > </result>
    > <resData>
    > <domain:appData>
    > <domain:name>c-and-a.eu</domain:name>
    > <domain:reference> c-and-a_1</domain:reference>
    > <domain:code>2565100006029999</domain:code>
    > <domain:crDate>2005-11-08T14:51:08.929Z</domain:crDate>
    > </domain:appData>
    > </resData>
    > <!-- SNIP -->
    > </response>
    > </epp>


    So to get the msg, you can do:

    print doc.getElementsByTagName('msg')[0].toxml()

    But to get the domain:name you have to use the declared namespace:

    print
    doc.getElementsByTagNameNS('http://www.eurid.eu/xml/epp/domain-1.0',
    'name')[0].toxml()

    Or you can make life a bit easier with Amara [1]:

    import amara
    doc = amara.parse(theXML)
    print doc.response.result.msg #to get the text content
    print doc.response.result.msg.xml() #to get the XML source for that
    element
    print doc.response.resData.appData.name
    print doc.response.resData.appData.name.xml()

    [1] http://uche.ogbuji.net/tech/4Suite/amara/

    --
    Uche Ogbuji Fourthought, Inc.
    http://uche.ogbuji.net http://fourthought.com
    http://copia.ogbuji.net http://4Suite.org
    Articles: http://uche.ogbuji.net/tech/publications/
    , Feb 11, 2006
    #5
    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. shruds
    Replies:
    1
    Views:
    779
    John C. Bollinger
    Jan 27, 2006
  2. Guilherme Grillo

    reader inside a reader

    Guilherme Grillo, Nov 7, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    525
    sloan
    Nov 7, 2007
  3. Simon Pickles

    REALLY simple xml reader

    Simon Pickles, Jan 27, 2008, in forum: Python
    Replies:
    4
    Views:
    268
    Ricardo Aráoz
    Jan 29, 2008
  4. Stefan Behnel

    Re: REALLY simple xml reader

    Stefan Behnel, Jan 29, 2008, in forum: Python
    Replies:
    23
    Views:
    704
    Stefan Behnel
    Feb 2, 2008
  5. Pokkai Dokkai
    Replies:
    1
    Views:
    232
    Hassan Schroeder
    Mar 24, 2008
Loading...

Share This Page