[PyQt] Kind of newbie question about reading XML file

David Boddie david at boddie.org.uk
Sat Dec 13 14:29:08 GMT 2008


On Thu, 11 Dec 2008 13:07:53 -0200, Gustavo A. Diaz wrote:
>
> Hi Again, Anyone any tip about his?

I find that the elementsByTagName() method provided by QDomElement is useful
for obtaining the elements I'm interested in. So, you can get the FirstTag
elements by calling

  elements = self.docElem.elementsByTagName("FirstTag")

then directly obtain the data1 and data2 elements within each of these by
calling the same method on the corresponding FirstTag element:

  for i in range(elements.count()):
      element = elements.item(i)
      data1Elements = element.elementsByTagName("data1")
      data2Elements = element.elementsByTagName("data2")

Beware: if there are multiple SecondTag elements within each FirstTag
element, each containing its own data1 and data2 elements, you will get
all of them at once. In this case, you might want to obtain all the
SecondTag elements inside each FirstTag and search for data1 and data2
elements inside those.

  firstElements = self.docElem.elementsByTagName("FirstTag")

  for i in range(firstElements.count()):
      secondElements = firstElements.item(i).elementsByTagName("SecondTag")

      for j in range(secondElements.count()):
          element = secondElements.item(j)
          data1Elements = element.elementsByTagName("data1")
          data2Elements = element.elementsByTagName("data2")

Note that I haven't tested the above, but hopefully you get the idea.

David


More information about the PyQt mailing list