Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32306

Re: problems with xml parsing (python 3.3)

From Dieter Maurer <dieter@handshake.de>
Subject Re: problems with xml parsing (python 3.3)
Date 2012-10-28 08:30 +0100
References <97d8de0d-3daa-49be-a91f-c65fc8a9019f@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.2953.1351409448.27098.python-list@python.org> (permalink)

Show all headers | View raw


jannidis@gmail.com writes:

> I am new to Python and have a problem with the behaviour of the xml parser. Assume we have this xml document:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <bibliography>
>     <entry>
>             Title of the first book.
>         </entry>
>         <entry>
>             <coauthored/>
> Title of the second book.
>         </entry>
> </bibliography>    
>
>
> If I now check for the text of all 'entry' nodes, the text for the node with the empty element isn't shown
>
>
>
> import xml.etree.ElementTree as ET
> tree = ET.ElementTree(file='test.xml')
> root = tree.getroot()
> resultSet = root.findall(".//entry")
> for r in resultSet:
> 	print (r.text)

I do not know about "xml.etree" but the (said) quite compatible
"lxml.etree" handles text nodes in a quite different way from
that of "DOM": they are *not* considered children of the parent
element but are attached as attributes "text" and "tail" to either
the container element (if the first DOM node is a text node) or the preceeding
element, otherwise.

Your code snippet suggests that "xml.etree" behaves identically in
this respect. In this case, you would find "Title of the second book"
as the "tail" attribute of the element "coauthored".

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

problems with xml parsing (python 3.3) jannidis@gmail.com - 2012-10-27 19:27 -0700
  Re: problems with xml parsing (python 3.3) jannidis@gmail.com - 2012-10-27 19:30 -0700
  Re: problems with xml parsing (python 3.3) MRAB <python@mrabarnett.plus.com> - 2012-10-28 03:08 +0000
  Re: problems with xml parsing (python 3.3) Dieter Maurer <dieter@handshake.de> - 2012-10-28 08:30 +0100
  Re: problems with xml parsing (python 3.3) jannidis@gmail.com - 2012-10-29 15:54 -0700
  Re: problems with xml parsing (python 3.3) jannidis@gmail.com - 2012-10-30 05:37 -0700

csiph-web