Help with libxml2dom

N

Nuno Santos

I have just started using libxml2dom to read html files and I have some
questions I hope you guys can answer me.

The page I am working on (teste.htm):
<html>
<head>
<title>
Title
</title>
</head>
<body bgcolor = 'FFFFF'>
<table>
<tr bgcolor="#EEEEEE">
<td nowrap="nowrap">
<font size="2" face="Tahoma, Arial"> <a name="1375048"></a>
</font>
</td>
<td nowrap="nowrap">
<font size="-2" face="Verdana"> 8/15/2009</font>
</td>
</tr>
</table>
</body>
u'a'


It seems like sometimes there are some text elements 'hidden'. This is
probably a standard in DOM I simply am not familiar with this and I
would very much appreciate if anyone had the kindness to explain me this.

Thanks.
 
D

Diez B. Roggisch

Nuno said:
I have just started using libxml2dom to read html files and I have some
questions I hope you guys can answer me.

The page I am working on (teste.htm):
<html>
<head>
<title>
Title
</title>
</head>
<body bgcolor = 'FFFFF'>
<table>
<tr bgcolor="#EEEEEE">
<td nowrap="nowrap">
<font size="2" face="Tahoma, Arial"> <a name="1375048"></a>
</font>
</td>
<td nowrap="nowrap">
<font size="-2" face="Verdana"> 8/15/2009</font>
</td>
</tr>
</table>
</body>

u'a'


It seems like sometimes there are some text elements 'hidden'. This is
probably a standard in DOM I simply am not familiar with this and I
would very much appreciate if anyone had the kindness to explain me this.

Without a schema or something similar, a parser can't tell if whitespace is
significant or not. So if you have

<root>
<child/>
</root>

you will have not 2, but 4 nodes - root, text containing a newline + 2
spaces, child, and again a text with a newline.

You have to skip over those that you are not interested in, or use a
different XML-library such as ElementTree (e.g. in the form of lxml) that
has a different approach about text-nodes.

Diez
 
P

Paul Boddie

I have just started using libxml2dom to read html files and I have some
questions I hope you guys can answer me.
[...]

 >>> table = body.firstChild
 >>> table.nodeName
u'text' #?! Why!? Shouldn't it be a table? (1)

You answer this yourself just below.
 >>> table = body.firstChild.nextSibling #why this works? is there a
text element hidden? (2)
 >>> table.nodeName
u'table'

Yes, in the DOM, the child nodes of elements include text nodes, and
even though one might regard the whitespace before the first child
element and that appearing after the last child element as
unimportant, the DOM keeps it around in case it really is important.

[...]
It seems like sometimes there are some text elements 'hidden'. This is
probably a standard in DOM I simply am not familiar with this and I
would very much appreciate if anyone had the kindness to explain me this.

Well, the nodes are actually there: they're whitespace used to provide
the indentation in your example. I recommend using XPath to get actual
elements:

table = body.xpath("*")[0] # get child elements and then select the
first

Although people make a big "song and dance" about the DOM being a
nasty API, it's quite bearable if you use it together with XPath
queries.

Paul
 

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,773
Messages
2,569,594
Members
45,118
Latest member
LatishaWhy
Top