Sie sind auf Seite 1von 4

Ejemplos de uso de DOM

Ejemplo 1 (Básico)

demoDom.py
from xml.dom.minidom import parse, parseString
 
dom1 = parse('notas.xml')
 
print dom1
for e in dom1.getElementsByTagName('body'):
print e.getAttribute('ancho')
for d in e.childNodes:
print d.data

notas.xml

<?xml version="1.0" encoding="latin1"?>


 
<!DOCTYPE note SYSTEM "demo.dtd">
<note>
<to>otro
linea</to>
<from nombre="me"></from>
<heading></heading>
<body ancho="56">Mensaje de texto</body>
<to>otro2</to>
<from nombre="me de nuevo"></from>
<heading></heading>
<body ancho="33">otro comentario</body>
</note>

Ejemplo 2 (Procesar Slide con DOM)

procSlide.py
import xml.dom.minidom
 
document = """\
<slideshow>
<title>Demo de Presentacion</title>
<slide><title>Primer Slide</title>
<point>Primer punto</point>
<point>Segundo punto</point>
</slide>
 
<slide><title>Otro slide</title>
<point>Esto es otra prueba</point>
<point>Otra mas</point>
<point>Ultimo punto</point>
</slide>
</slideshow>
"""
 
dom = xml.dom.minidom.parseString(document)
 
def getText(nodelist):
rc = ""
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc = rc + node.data
return rc
 
def handleSlideshow(slideshow):
print "<html>"
handleSlideshowTitle(slideshow.getElementsByTagName("title")[0])
slides = slideshow.getElementsByTagName("slide")
handleToc(slides)
handleSlides(slides)
print "</html>"
 
def handleSlides(slides):
for slide in slides:
handleSlide(slide)
 
def handleSlide(slide):
handleSlideTitle(slide.getElementsByTagName("title")[0])
handlePoints(slide.getElementsByTagName("point"))
 
def handleSlideshowTitle(title):
print "<title>%s</title>" % getText(title.childNodes)
 
def handleSlideTitle(title):
print "<h2>%s</h2>" % getText(title.childNodes)
 
def handlePoints(points):
print "<ul>"
for point in points:
handlePoint(point)
print "</ul>"
 
def handlePoint(point):
print "<li>%s</li>" % getText(point.childNodes)
 
def handleToc(slides):
for slide in slides:
title = slide.getElementsByTagName("title")[0]
print "<p>%s</p>" % getText(title.childNodes)
 
handleSlideshow(dom)

Ejemplo 3 (Navegador)

demoDom.html
demoDom.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"


"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Demo DOM con tablas</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script>
var cont=1;
function addrow(tc1,tc2) {
var tbl = document.getElementById('tabla');
var row = document.createElement('tr');
var c0 = document.createElement('td')
c0.innerHTML=cont;
cont++;
var c1 = document.createElement('td')
c1.innerHTML=tc1;
var c2 = document.createElement('td')
c2.innerHTML=tc2;
row.appendChild(c0);
row.appendChild(c1);
row.appendChild(c2);
tbl.appendChild(row);
}
 
function addrow2() {
var frm = document.getElementById('forma');
addrow(frm.c1.value,frm.c2.value);
}
</script>
</head>
<body>
<h2>Demo de DOM con tablas</h2>
<form id='forma'>
Columna 1:<input type="text" name="c1">
Columna 2:<input type="text" name="c2">
<input type="button" value="add" onclick="javascript:addrow2()">
<input type="reset">
</form>
<div onclick="javascript:addrow('vacio','vacio')">Adicionar fila</div><br/>
<table cellpadding="0" cellspacing="0" border="1" id="tabla">
<tr>
<td></td><th>col 1</th><th>col 2</th>
</tr>
</table>
<hr>
</body>
</html>

Demo de DOM con tablas


Reset
Columna 1: Columna 2:
Adicionar fila

Das könnte Ihnen auch gefallen