xml element tree to html problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ron Adam

    xml element tree to html problem


    I'm new to element tree and haven't been able to find a simple solution
    to this problem yet. So maybe someone can point me in the right direction.

    I have an element tree structure of nested elements that I want to
    convert to html as nested definition and unordered lists in the
    following way.

    <object>
    <name>ball</ball>
    <desc>
    <color>red</color>
    <size>large</size>
    </desc>
    </object>


    To...

    <dl class='object'>
    <dt class='name'>ba ll</dt>
    <dd class='desc'>
    <ul>
    <li class='color'>r ed</li>
    <li class='size'>la rge</li>
    </ul>
    </dd>
    </dl>


    Where each xml tag has a predefined relationship to either definition
    list or an unordered list html tag. 'object' is always mapped to <dl
    class='object'> , 'name' is always mapped to <dt class='name'>. etc...

    So I will probably have a dictionary to look them up. The problem I
    have is finding a relatively painless way to do the actual translation.

    Thanks in advance for any advise.

    Cheers,
    Ron

  • akameswaran@gmail.com

    #2
    Re: xml element tree to html problem

    are you using PyXML?
    If this is a simple one to one relationship with dependence on order,
    I'd forgo the whole PyXML, read the file line by line, and replace tags
    as appropriate. You may have to do some simple checkin, in case there
    is n <object> <name>object</name>


    Otherwise, have fun with the parsers - nothing is painless is SAX or
    DOM.

    as far as simple translation? once you have tokenized or used PyXML to
    get the elements, something like?

    for tag in listofTags:
    if tagDictionary.h as_key(tag):
    doSomething(tag Dictionary[tag])


    IMHO - PyXML is a great tool, but for something like simple
    substitution, it's so so much overkill. For simple processing tasks
    like this I almost always just treat it as a text file. I resort to
    PyXML when things like heirarchy are important. Hope this helps

    Comment

    • Fredrik Lundh

      #3
      Re: xml element tree to html problem

      Ron Adam wrote:
      [color=blue]
      > I have an element tree structure of nested elements that I want to
      > convert to html as nested definition and unordered lists in the
      > following way.
      >
      > <object>
      > <name>ball</ball>
      > <desc>
      > <color>red</color>
      > <size>large</size>
      > </desc>
      > </object>
      >
      >
      > To...
      >
      > <dl class='object'>
      > <dt class='name'>ba ll</dt>
      > <dd class='desc'>
      > <ul>
      > <li class='color'>r ed</li>
      > <li class='size'>la rge</li>
      > </ul>
      > </dd>
      > </dl>
      >
      >
      > Where each xml tag has a predefined relationship to either definition
      > list or an unordered list html tag. 'object' is always mapped to <dl
      > class='object'> , 'name' is always mapped to <dt class='name'>. etc...
      >
      > So I will probably have a dictionary to look them up. The problem I
      > have is finding a relatively painless way to do the actual translation.[/color]

      here's one way to do it:

      import cElementTree as ET

      tree = ET.XML("""
      <object>
      <name>ball</name>
      <desc>
      <color>red</color>
      <size>large</size>
      </desc>
      </object>
      """)

      MAP = {
      "object": ("dl", "object"),
      "name": ("dt", "name"),
      "desc": ("ul", None),
      "color": ("li", "color"),
      "size": ("li", "size"),
      }

      for elem in tree.getiterato r():
      elem.tag, klass = MAP[elem.tag]
      if klass:
      elem.set("class ", klass)

      print ET.tostring(tre e)

      this prints:

      <dl class="object">
      <dt class="name">ba ll</dt>
      <ul>
      <li class="color">r ed</li>
      <li class="size">la rge</li>
      </ul>
      </dl>


      here's a somewhat simpler (but less general) version:

      MAP = dict(object="dl ", name="dt", desc="ul", color="li", size="li")

      for elem in tree.getiterato r():
      if elem.tag != "desc":
      elem.set("class ", elem.tag)
      elem.tag = MAP[elem.tag]

      hope this helps!

      </F>



      Comment

      • akameswaran@gmail.com

        #4
        Re: xml element tree to html problem

        Frederick,
        I didn't know about cElementTree before, wow - this is a lot nicer than
        PyyXML - and a whole lot faster. Almost makes my comments about
        dealing with the xml as text, completely pointless.

        Comment

        • Ron Adam

          #5
          Re: xml element tree to html problem

          Fredrik Lundh wrote:
          [color=blue]
          > here's one way to do it:
          >
          > import cElementTree as ET
          >
          > tree = ET.XML("""
          > <object>
          > <name>ball</name>
          > <desc>
          > <color>red</color>
          > <size>large</size>
          > </desc>
          > </object>
          > """)
          >
          > MAP = {
          > "object": ("dl", "object"),
          > "name": ("dt", "name"),
          > "desc": ("ul", None),
          > "color": ("li", "color"),
          > "size": ("li", "size"),
          > }
          >
          > for elem in tree.getiterato r():
          > elem.tag, klass = MAP[elem.tag]
          > if klass:
          > elem.set("class ", klass)
          >
          > print ET.tostring(tre e)[/color]


          Thanks a *LOT!* :-)

          This is what I needed. Now I can play with finding the best data
          structure along with what elements to translate each tag to.

          This is for a rewrite of PyDoc.py. I'm hoping it will be as easy to
          write to other formats from the XML as it is to html.

          Cheers,
          Ron

          Comment

          Working...