-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcatxml-0.1.0.py
More file actions
50 lines (43 loc) · 1.31 KB
/
Copy pathcatxml-0.1.0.py
File metadata and controls
50 lines (43 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import click
from xml.dom import minidom
def imprime_atributos(nodo, l):
"""
Imprime los atributos de un nodo
"""
for nom, val in nodo.attributes.items():
print(" "*l + "|-", nom, "=", val)
def imprime_hijos(nodo, l=0):
"""
Imprime el contenido de un nodo en la salida estándar
"""
if nodo.attributes:
imprime_atributos(nodo, l)
for hijo in nodo.childNodes:
if hijo.nodeName == "#text":
if hijo.data.strip() != "":
print(" "*l, "|-", hijo.data)
else:
print(" "*l + "|-", hijo.nodeName)
imprime_hijos(hijo, l+1)
@click.command()
@click.option("--raw", is_flag=True,
help="Imprime el contenido del archivo sin modificación alguna")
@click.argument("pathfile", type=click.Path(exists=True), required=True)
def catxml(pathfile, raw):
"""
Imprime en la salida estándar el contenido del archivo xml
indicado por PATHFILE
"""
if raw:
with open(pathfile) as f:
for linea in f:
print(linea, end="")
else:
dom = minidom.parse(pathfile)
raiz = dom.childNodes[0]
print(raiz.nodeName)
imprime_hijos(raiz)
if __name__ == "__main__":
catxml()