
April 4th, 2006, 09:05 PM
|
|
|
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'>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
|

April 4th, 2006, 09:35 PM
|
|
|
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.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
|

April 4th, 2006, 09:35 PM
|
|
|
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'>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.[/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.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>
|

April 4th, 2006, 10:05 PM
|
|
|
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.
|

April 5th, 2006, 05:16 AM
|
|
|
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.getiterator():
> elem.tag, klass = MAP[elem.tag]
> if klass:
> elem.set("class", klass)
>
> print ET.tostring(tree)[/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
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|