473,324 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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

Apr 4 '06 #1
4 1520
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

Apr 4 '06 #2
Ron Adam wrote:
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>

Apr 4 '06 #3
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.

Apr 4 '06 #4
Fredrik Lundh wrote:
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
Apr 5 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Brett | last post by:
Hello All, I'm trying to parse through a webpage, and I used HTML::treebuilder to try to get it into an easily managable data structure. I am having a bit of trouble parsing through it though....
4
by: Jean-Christophe Michel | last post by:
Hi, In a complex merging of two (non ordered) xml files i need to keep track of the elements of the second tree that were already merged with first tree, to copy only unused elements at the end....
18
by: Tjerk Wolterink | last post by:
i have the following rule, <xsl:template match="br"> <br/> </xsl:template> This should convert all <br/> to <br/> but, my transformer transforms it all to
1
by: Stanimir Stamenkov | last post by:
I've wondered if it is right thing to do: element.parentNode.insertBefore(element, beforeElement); where 'beforeElement' is one of the 'element.parentNode.childNodes'? First, I've used: ...
5
by: Patient Guy | last post by:
In my reading of the Strict and Transitional DTD for HTML 4.0, the table row (TR) elements are contained within table section elements: THEAD, TFOOT, and TBODY. The table section elements are...
1
by: Satish.Talyan | last post by:
hi, i want to create a dynamic tree hierarchy in javascript.there are two parts in tree, group & user.when we click on group then users come under that's tree category will be opened.problem is...
8
by: VK | last post by:
Can be multiple instances of element used as the root element? That's a curly way of asking, but I did not come up with a better sentence, sorry. What I mean is with a document like: <?xml...
4
by: Steve | last post by:
I thought that this was available for all elements in Firefox, but recently had a page where a div didn't have it. I put in an id style for it, thinking that would do the trick, but it didn't. ...
2
by: hankypan1 | last post by:
Hi All, I need a tree data structure for my application. It is the non - cyclic simple tree where i can have any number of children node and each child can recursively become a sub tree like a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.