Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How *build* new elements and *replace* elements with xml.dom.minidom?

Reply
Thread Tools

How *build* new elements and *replace* elements with xml.dom.minidom?

 
 
Chris Seberino
Guest
Posts: n/a
 
      06-10-2009
How build new elements to replace existing ones using xml.dom.minidom?

Specifically, I have an HTML table of numbers. I want to replace
those numbers with hyperlinks to create a table of hyperlinks.

So I need to build hyperlinks (a elements) with href attribute and
replace the text elements (numbers) somehow.

How do that?

chris
 
Reply With Quote
 
 
 
 
Stefan Behnel
Guest
Posts: n/a
 
      06-11-2009
Chris Seberino wrote:
> How build new elements to replace existing ones using xml.dom.minidom?
>
> Specifically, I have an HTML table of numbers. I want to replace
> those numbers with hyperlinks to create a table of hyperlinks.
>
> So I need to build hyperlinks (a elements) with href attribute and
> replace the text elements (numbers) somehow.


Try lxml.html instead. It makes it really easy to do these things. For
example, you can use XPath to find all table cells that contain numbers:

td_list = doc.xpath("//td[number() >= 0]")

or maybe using regular expressions to make sure it's an int:

td_list = doc.xpath("//td[re:match(., '^[0-9]+$')]",
namespaces={'re':'http://exslt.org/regular-expressions'})

and then replace them by a hyperlink:

# assuming links = ['http://...', ...]

from lxml.html.builder import A
for td in td_list:
index = int(td.text)
a = A("some text", href=links[index])
td.getparent().replace(td, a)

Stefan
 
Reply With Quote
 
 
 
 
Johannes Bauer
Guest
Posts: n/a
 
      06-11-2009
Stefan Behnel schrieb:

>> So I need to build hyperlinks (a elements) with href attribute and
>> replace the text elements (numbers) somehow.

>
> Try lxml.html instead. It makes it really easy to do these things. For
> example, you can use XPath to find all table cells that contain numbers:
>
> td_list = doc.xpath("//td[number() >= 0]")
>
> or maybe using regular expressions to make sure it's an int:
>
> td_list = doc.xpath("//td[re:match(., '^[0-9]+$')]",
> namespaces={'re':'http://exslt.org/regular-expressions'})
>
> and then replace them by a hyperlink:
>
> # assuming links = ['http://...', ...]
>
> from lxml.html.builder import A
> for td in td_list:
> index = int(td.text)
> a = A("some text", href=links[index])
> td.getparent().replace(td, a)


Oh no! I was looking for something like this for *ages* but always
fought with minidom - where this is a real pain

Had I only known before that such a wonderful library exists. I'll
definitely use lxml from now on. Does it compile with Python3?

Kind regards,
Johannes

--
"Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit,
verlästerung von Gott, Bibel und mir und bewusster Blasphemie."
-- Prophet und Visionär Hans Joss aka HJP in de.sci.physik
<48d8bf1d$0$7510$(E-Mail Removed)>
 
Reply With Quote
 
Stefan Behnel
Guest
Posts: n/a
 
      06-12-2009
Johannes Bauer wrote:
> Stefan Behnel schrieb:
>
>>> So I need to build hyperlinks (a elements) with href attribute and
>>> replace the text elements (numbers) somehow.

>> Try lxml.html instead. It makes it really easy to do these things. For
>> example, you can use XPath to find all table cells that contain numbers:
>>
>> td_list = doc.xpath("//td[number() >= 0]")
>>
>> or maybe using regular expressions to make sure it's an int:
>>
>> td_list = doc.xpath("//td[re:match(., '^[0-9]+$')]",
>> namespaces={'re':'http://exslt.org/regular-expressions'})
>>
>> and then replace them by a hyperlink:
>>
>> # assuming links = ['http://...', ...]
>>
>> from lxml.html.builder import A
>> for td in td_list:
>> index = int(td.text)
>> a = A("some text", href=links[index])
>> td.getparent().replace(td, a)

>
> Oh no! I was looking for something like this for *ages* but always
> fought with minidom - where this is a real pain
>
> Had I only known before that such a wonderful library exists. I'll
> definitely use lxml from now on.


Yep, I keep advertising it all over the place, but there are still so many
references to minidom on the web that it's hard to become the first hit in
Google when you search for "Python XML".

Actually, the first hit (for me) is currently PyXML, which is officially
unmaintained. Wasn't there 'some' Python developer working for Google? What
about fixing their database?


> Does it compile with Python3?


Sure.

Stefan
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
number of distinct elements in a const container and iterating over the distinct elements Hicham Mouline C++ 1 04-11-2010 10:56 AM
delete() and new() on elements of an array created by new[]() [rob desbois] C++ 2 04-15-2008 01:24 PM
newb: recurse over elements children and disable all form elements SteveKlett@gmail.com Javascript 4 09-20-2006 07:42 AM
XSLT: iterating all child elements and accessing homonymous childrenin sibling elements Gerald Aichholzer XML 2 06-27-2006 03:46 PM



Advertisments