xml element tree to html problem

R

Ron Adam

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'>ball</dt>
<dd class='desc'>
<ul>
<li class='color'>red</li>
<li class='size'>large</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
 
F

Fredrik Lundh

Ron said:
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'>ball</dt>
<dd class='desc'>
<ul>
<li class='color'>red</li>
<li class='size'>large</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.

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.getiterator():
elem.tag, klass = MAP[elem.tag]
if klass:
elem.set("class", klass)

print ET.tostring(tree)

this prints:

<dl class="object">
<dt class="name">ball</dt>
<ul>
<li class="color">red</li>
<li class="size">large</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.getiterator():
if elem.tag != "desc":
elem.set("class", elem.tag)
elem.tag = MAP[elem.tag]

hope this helps!

</F>
 
A

akameswaran

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.has_key(tag):
doSomething(tagDictionary[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
 
A

akameswaran

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.
 
R

Ron Adam

Fredrik said:
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.getiterator():
elem.tag, klass = MAP[elem.tag]
if klass:
elem.set("class", klass)

print ET.tostring(tree)


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top