XML을 보기 좋게 출력하는 방법?
조회수 4620회
발생하는 문제 및 실행환경
xml을 예쁘게 출력하려면 어떻게 해야 되나요?
xml파일이 이렇게 있고,
<foo>
<bar>
<type foobar="1"/>
<type foobar="2"/>
</bar>
</foo>
파이썬 코드를 실행했을 때
import xml.dom.minidom
xml = xml.dom.minidom.parse("myxml.xml")
pretty_xml_as_string = xml.toprettyxml()
print(pretty_xml_as_string)
결과 :
<?xml version="1.0" ?>
<foo>
<bar>
<type foobar="1"/>
<type foobar="2"/>
</bar>
</foo>
이렇게 들여쓰기가 너무 많이 나왔어요.
1 답변
-
lxml.etree.parse는 개행 없이 xml파일 그 자체를 출력해줍니다.
import lxml.etree as etree x = etree.parse("myxml.xml") print etree.tostring(x, pretty_print = True)
결과 :
<foo> <bar> <type foobar="1"/> <type foobar="2"/> </bar> </foo>
댓글 입력