Using ElementTree to tidy up an XML string to my liking

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • peterbe@gmail.com

    Using ElementTree to tidy up an XML string to my liking

    I have an XML string coming in from one system that I'd like to tidy up
    and return in a very particular format. I'm picky!

    If the input is
    <SOMETHING attr1="foo1"
    attr2='foo2' >

    Then the output must be
    <something
    attr1="foo1"
    attr2="foo2">

    The file might have comments and namespaces and all of this should be
    preserved as it was when it came in.

    I was hoping to use ElementTree which can parse my input so that I rest
    on solid foundations instead of mad regular expressions and string
    manipulation.
    But, if mad regular expressions and string manipulations is what it
    takes I'll have to settle on that.

    What are my options?

  • Simon Dahlbacka

    #2
    Re: Using ElementTree to tidy up an XML string to my liking

    ...I hope that you are aware that xml is *case sensitive*

    Comment

    • peterbe@gmail.com

      #3
      Re: Using ElementTree to tidy up an XML string to my liking

      Yes I am but, I'm using a DOM Serializer in Firefox which for some
      reason turns myCamelNames into MYCAMELNAMES for the nodenames. I'll
      therefore need to control the case-spelling of these things as I'm
      formatting the XML string.

      Comment

      • Magnus Lycka

        #4
        Re: Using ElementTree to tidy up an XML string to my liking

        peterbe@gmail.c om wrote:[color=blue]
        > Yes I am but, I'm using a DOM Serializer in Firefox which for some
        > reason turns myCamelNames into MYCAMELNAMES for the nodenames. I'll
        > therefore need to control the case-spelling of these things as I'm
        > formatting the XML string.[/color]

        I realize that it's difficult to make sure that every user of your
        system has a corrected Firefox, but it seems to me that there is
        a bug in that DOM Serializer, and that it would be good to mend
        that.

        Concerning element names, it's your coice of course, but I agree
        more and more with Guido and PEP008 that camelCase is ugly. (Not
        that ALLCAPS is better...)

        Comment

        • Heikki Toivonen

          #5
          Re: Using ElementTree to tidy up an XML string to my liking

          peterbe@gmail.c om wrote:[color=blue]
          > Yes I am but, I'm using a DOM Serializer in Firefox which for some
          > reason turns myCamelNames into MYCAMELNAMES for the nodenames. I'll
          > therefore need to control the case-spelling of these things as I'm
          > formatting the XML string.[/color]

          I am almost certain there is something wrong you are doing in Mozilla if
          that is happening. My first guess is that you are really doing HTML even
          thought you think you are doing XML, and therefore Mozilla converts your
          stuff to uppercase. Without seeing the source it is hard to say. I would
          advice you write to the Mozilla forums for advice on that.

          --
          Heikki Toivonen

          Comment

          • Richard Townsend

            #6
            Re: Using ElementTree to tidy up an XML string to my liking

            On Fri, 24 Feb 2006 18:21:59 +0100, Magnus Lycka wrote:
            [color=blue]
            > Concerning element names, it's your coice of course, but I agree
            > more and more with Guido and PEP008 that camelCase is ugly. (Not
            > that ALLCAPS is better...)[/color]

            I can see in PEP008 where it says Capitalized_Wor ds_With_Undersc ores is
            ugly, but I can't see where it says pure camelCase is ugly ?

            --
            Richard

            Comment

            • Magnus Lycka

              #7
              Re: Using ElementTree to tidy up an XML string to my liking

              Richard Townsend wrote:[color=blue]
              > On Fri, 24 Feb 2006 18:21:59 +0100, Magnus Lycka wrote:[color=green]
              >>Concerning element names, it's your coice of course, but I agree
              >>more and more with Guido and PEP008 that camelCase is ugly. (Not
              >>that ALLCAPS is better...)[/color]
              >
              > I can see in PEP008 where it says Capitalized_Wor ds_With_Undersc ores is
              > ugly, but I can't see where it says pure camelCase is ugly ?[/color]

              Sorry, meant mixedCase (as it was in the XML example). Peter confused
              me by writing "myCamelNam es"

              """
              Function Names

              Function names should be lowercase, possibly with words separated by
              underscores to improve readability. mixedCase is allowed only in
              contexts where that's already the prevailing style (e.g. threading.py),
              to retain backwards compatibility.

              Method Names and Instance Variables

              The story is largely the same as with functions: in general, use
              lowercase with words separated by underscores as necessary to improve
              readability.
              """

              Comment

              Working...