Para ver cómo se procesa un documento XML trabajaremos con el siguiente ejemplo:
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<book isbn="1-56592-724-9">
<title>The Cathedral & the
Bazaar</title>
<author>Eric S.
Raymond</author>
</book>
<book isbn="1-56592-051-1">
<title>Making TeX Work</title>
<author>Norman Walsh</author>
</book>
</catalog>
Interfaz SAX
import xml.sax.handler
class BookHandler(xml.sax.handler.ContentHandler):
def __init__(self):
self.inTitle = 0
self.mapping = {}
def startElement(self, name, attributes):
if name == "book":
self.buffer = ""
self.isbn = attributes["isbn"]
elif name == "title":
self.inTitle = 1
def characters(self, data):
if self.inTitle:
self.buffer += data
def endElement(self, name):
if name == "title":
self.inTitle = 0
self.mapping[self.isbn] =
self.buffer
Interfaz DOM.-
import xml.dom.minidom
from xml.dom.minidom import Node
doc = xml.dom.minidom.parse("books.xml")
for node in doc.getElementsByTagName("book"):
isbn = node.getAttribute("isbn")
L = node.getElementsByTagName("title")
for node2 in L:
title = ""
for node3 in node2.childNodes:
if node3.nodeType ==
Node.TEXT_NODE:
title +=
node3.data
print 'Encontrado libro "%s"' %
title
Hay 3 opiniones. Opina sobre este curso.
| Cursos | Valoración | Alumnos | Vídeo | |
|---|---|---|---|---|
|
PHP y MySQL. Aplicaciones Web: programación PHP I (quinta parte) Programación de aplicaciones Web con PHP y MySQL. Ahora estudiaremos el Lenguaje de programación PHP. Aprende ahora las formas de escribir las etiquetas ... [02/12/08] |
|
1.009 | ||
|
MS-DOS avanzado Microsoft Windows nunca fue realmente un Sistema Operativo con verdadero entorno gráfico hasta Windows95. Este curso es la segunda parte del exitoso curso de int... [15/06/07] |
|
6.496 | ||
|
PC. Hardware y software Informática. Hardware y software de un ordenador tipo PC. El curso ofrece una descripción básica de... [13/03/09] |
|
2.176 | ||
Publicar en
del.icio.us
digg
meneame