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 4 1539
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
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'>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.
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>
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.
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.getiterato r(): elem.tag, klass = MAP[elem.tag] if klass: elem.set("class ", klass)
print ET.tostring(tre e)
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
Here is a snippet of code:
$tree = HTML::TreeBuilder->new;
$tree->parse($response->content);
|
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.
I tried two solutions:
* first is to 'substract' the used element from existing tree
|
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
|
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:
var parentElement = element.parentNode;
parentElement.removeChild(element);
|
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 the defined contents of the TABLE element.
The TR element is not defined as an "immediate" or "direct" contained
element of TABLE.
Given that
| |
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 that i am not able
to arrange three things simultaneously,group,users & there
functionality simultaneously.dynamic means group & users come from...
|
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 version="1.0" encoding="UTF-8"?>
<root>
<element>Content</element>
<root><element>Content</element></root>
<element>Content</element>
|
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.
Is that something that isn't there for all elements that are correctly
formed, or possibly something that disappears due to malformed HTML? Or
is...
|
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 normal tree. Now the
thing is i can popullate my tree at compile time like a global data.
Since
i know my tree definition at compile time, instead...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |