Last active
May 20, 2016 16:11
-
-
Save sxslex/0c075076b905a87c6b7a0f5fd918b146 to your computer and use it in GitHub Desktop.
Separa Node XML Level
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pprint | |
conteudo = """ | |
<?xml encoding="utf-8"?> | |
<Bola> | |
<Imovel> | |
<sssss>asdasd</sssss> | |
<AAAA>asdasd</AAAA> | |
</Imovel> | |
<Imovel>2<!--Bolas?><?<# --></Imovel> | |
<Imovel>3</Imovel> | |
<Imovel>11</Imovel> | |
<Imovel>12</Imovel> | |
<Imovel>13</Imovel> | |
<Veiculo>1</Veiculo> | |
<Veiculo>1.1</Veiculo> | |
<Veiculo>1.1</Veiculo> | |
<Veiculo>2</Veiculo> | |
<Veiculo>2.1</Veiculo> | |
<Veiculo>3</Veiculo> | |
<Veiculo>3.1</Veiculo> | |
</Bola> | |
""" | |
def split_xml_tag_level(conteudo, nivel=2, lower_tags=False): | |
header = '' | |
conteudo = conteudo.strip() | |
lconteudo = conteudo.strip().split('?>') | |
if len(lconteudo) > 1 and conteudo[:6].lower() == '<?xml ': | |
header = lconteudo[0] + '?>' | |
conteudo = '?>'.join(lconteudo[1:]) | |
segundos_niveis = dict() | |
rnotes = conteudo.split('<')[nivel: (nivel - 1) * -1] | |
# rnotes = [node.split('>')[0] for node in rnotes] | |
# pprint.pprint(rnotes) | |
# return True | |
last_tag = '' | |
conteudotag = '' | |
for node in rnotes: | |
conteudotag += '<' + node | |
tag = node.split('>')[0] | |
if lower_tags: | |
tag = tag.lower() | |
if last_tag: | |
if '/' + last_tag == tag: | |
segundos_niveis[last_tag].append( | |
conteudotag.strip() | |
) | |
last_tag = '' | |
conteudotag = '' | |
continue | |
if tag not in segundos_niveis: | |
segundos_niveis[tag] = [] | |
last_tag = tag | |
return header, segundos_niveis | |
pprint.pprint(split_xml_tag_level(conteudo, 2, True)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment