0

I have a string with xml:

string = '<catalog><category>iPhone</category><category>iPad</category><category>iPod</category><category>iMac</category><category>MacBook</category><category>Mac Mini</category><category>Чехлы</category><category>Защитные   пленки</category><category>Акксессуары</category><category>Наушники и гарнитуры</category><category>Переходники и Провода</category><category>Автомобильные аксессуары</category><category>Aккустические системы</category><category>Зарядные устройства</category><category>Видео очки</category><category>Wi-Fi оборудование</category><category>Аккумуляторы и блоки питания</category><category>BlackBerry</category><category>Подставки и док-станции</category><category>Игрушки</category><category>Цифровые ручки</category><category>Полезные устройства</category><category>Планшетные компьютеры</category><category>Samsung</category><category>GoPro</category><category>Память</category></catalog>'

please tell me how to display this xml on the screen indented reflecting nesting.

Sergey
  • 869
  • 2
  • 13
  • 22

2 Answers2

1

use pretty_print=True:

In [95]: from lxml import etree

In [97]: rt=etree.fromstring(string)

In [98]: print etree.tostring(rt, pretty_print=True)
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
0

You have to use xml module

import xml.dom.minidom

xml_obj = xml.dom.minidom.parseString(string)
xml_indent_string = xml.toprettyxml()
print xml_indent_string
Nilesh
  • 20,521
  • 16
  • 92
  • 148