xml.dom.minidom question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Geiregat Jonas

    xml.dom.minidom question

    I'm using xml.dom.minidom , I get some data by using
    obj.getElements ByTagName("name ")[0] then I have an object, but how can I
    get the data between the tags ? I could do .toxml() and then strip it
    away. But is there a better way maybe a function made for it ?
    Also is it possible using the xml.dom.minidom module to create modify an
    xml file ?

  • Greg Krohn

    #2
    Re: xml.dom.minidom question


    "Geiregat Jonas" <eniac@sdf-eu.org> wrote in message
    news:3f79ad4b$0 $10696$ba620e4c @reader2.news.s kynet.be...[color=blue]
    > I'm using xml.dom.minidom , I get some data by using
    > obj.getElements ByTagName("name ")[0] then I have an object, but how can I
    > get the data between the tags ? I could do .toxml() and then strip it
    > away. But is there a better way maybe a function made for it ?
    > Also is it possible using the xml.dom.minidom module to create modify an
    > xml file ?[/color]

    Given <name>Greg</name>, 'Greg' is considered an element inside the 'name'
    element. So, all you have to do is go one level of elements deeper.

    myname = obj.getElements ByTagName("name ")[0].childNodes[0].data


    greg


    Comment

    • John J. Lee

      #3
      Re: xml.dom.minidom question

      "Greg Krohn" <ask@me.com> writes:
      [color=blue]
      > "Geiregat Jonas" <eniac@sdf-eu.org> wrote in message
      > news:3f79ad4b$0 $10696$ba620e4c @reader2.news.s kynet.be...[color=green]
      > > obj.getElements ByTagName("name ")[0] then I have an object, but how can I
      > > get the data between the tags ? I could do .toxml() and then strip it[/color][/color]
      [...][color=blue]
      > Given <name>Greg</name>, 'Greg' is considered an element inside the 'name'
      > element. So, all you have to do is go one level of elements deeper.
      >
      > myname = obj.getElements ByTagName("name ")[0].childNodes[0].data[/color]

      Might want to read up about normalization, too.


      John

      Comment

      • Geiregat Jonas

        #4
        Re: xml.dom.minidom question

        What do you mean by normalization ?

        Comment

        • John J. Lee

          #5
          Re: xml.dom.minidom question

          Geiregat Jonas <eniac@sdf-eu.org> writes:
          [color=blue]
          > What do you mean by normalization ?[/color]

          Search for the normalize method in here:




          also:

          Expert insights into Microsoft ASP.NET, classic ASP, and the latest web development trends. Click to read ASP Today, by Matthew Pomar, a Substack publication. Launched 2 years ago.



          John

          Comment

          • Peter Hansen

            #6
            Re: xml.dom.minidom question

            Geiregat Jonas wrote:[color=blue]
            >
            > What do you mean by normalization ?[/color]


            Comment

            • Alan Kennedy

              #7
              Re: xml.dom.minidom question

              Geiregat Jonas wrote:[color=blue][color=green]
              >> What do you mean by normalization ?[/color][/color]

              Peter Hansen wrote:[color=blue]
              > http://www.w3.org/TR/REC-xml#AVNormalize[/color]

              Actually, Peter, that link has nothing to do with the normalization of
              text nodes in a DOM, which was, I think, the "normalizat ion" in
              question.

              The link you have provided gives detail on "attribute value
              normalization", which is the normalization of whitespace in attribute
              values, not DOM text nodes.

              Of course, the normalization of test nodes in a DOM is a not a part of
              the XML spec, it is a part of the DOM spec.



              Check out the Node.normalize( ) method.

              --
              alan kennedy
              -----------------------------------------------------
              check http headers here: http://xhaus.com/headers
              email alan: http://xhaus.com/mailto/alan

              Comment

              • Geiregat Jonas

                #8
                Re: xml.dom.minidom question

                so What about writing modifying a xml file with minidom ?

                Comment

                • Geoff Gerrietts

                  #9
                  Re: xml.dom.minidom question

                  Quoting Geiregat Jonas (eniac@sdf-eu.org):[color=blue]
                  > But how ???[/color]

                  from xml.dom.minidom import parse

                  dom = parse(my_xml_fi le)

                  dom = makeChangesToMy DomTree(dom)

                  f = open(my_xml_fil e)
                  f.write(dom.tox ml())

                  You should verify that this works properly but it should. I remember
                  at one point observing a problem with a document that declared a DTD,
                  where the DTD was not preserved and invalid XML was dumped. I don't
                  remember if minidom was the parser I was using, though, and I don't
                  have the time to test right now.

                  --G.

                  --
                  Geoff Gerrietts "Me and my homies, we tag O.D.."
                  <geoff at gerrietts net> --Unknown grafitti artist at a party

                  Comment

                  • Uche Ogbuji

                    #10
                    Re: xml.dom.minidom question

                    "Greg Krohn" <ask@me.com> wrote in message news:<2mjeb.102 37$NX3.9179@new sread3.news.pas .earthlink.net> ...[color=blue]
                    > "Geiregat Jonas" <eniac@sdf-eu.org> wrote in message
                    > news:3f79ad4b$0 $10696$ba620e4c @reader2.news.s kynet.be...[color=green]
                    > > I'm using xml.dom.minidom , I get some data by using
                    > > obj.getElements ByTagName("name ")[0] then I have an object, but how can I
                    > > get the data between the tags ? I could do .toxml() and then strip it
                    > > away. But is there a better way maybe a function made for it ?
                    > > Also is it possible using the xml.dom.minidom module to create modify an
                    > > xml file ?[/color]
                    >
                    > Given <name>Greg</name>, 'Greg' is considered an element inside the 'name'
                    > element. So, all you have to do is go one level of elements deeper.[/color]

                    You mean it's considered a text node inside of the 'name' element
                    node.

                    I'll point out that it might actually be more than one text node.
                    Only safe way is to use the normalize() method.

                    Better yet, why torture yourself with DOM API, especially when all you
                    need is to extract data from XML files?

                    My recommendations would be to use Anobind, Elementtree,
                    gnosis.xml.obje ctify or 4XPath (an advanage of XPath is that at least
                    the extraction expression is ross-language):

                    Anobind:


                    www.XML.com,Textuality Services,Uche Ogbuji,Applications,Introducing Anobind


                    Elementtree:


                    www.XML.com,Textuality Services,Uche Ogbuji,Instruction, Open Source,Simple XML Processing With elementtree


                    gnosis.xml.obje ctify:

                    www.XML.com,Textuality Services,Uche Ogbuji,Tools,XML Data Bindings in Python, Part 2



                    4XPath:


                    www.XML.com,Textuality Services,Uche Ogbuji,Instruction,A Tour of 4Suite


                    Good luck.

                    --Uche
                    Igbo-American immigrant from Nigeria, settled near Boulder, Colorado with my wife, three sons and daughter. Restless mind in a restless body, I do a million things without getting very much truly done

                    Comment

                    Working...