■ Element 클래스의 appendChild 메소드를 사용해 자식 엘리먼트를 추가하는 방법을 보여준다.
▶ book.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" ?> <booklist cnt="3"> <book ISBN="0399250395"> <title>The Very Hungry Caterpillar Pop-Up Book</title> <author name="Eric Carle" /> <author name="Keith Finch" /> <publisher> Philomel Books</publisher> <description> Celebrating the 40th anniverary of one of the most popular children's books ever created</description> </book> <book ISBN="0964729237"> <title lang="english">The Shack</title> </book> <book ISBN="0553281097"> <title>You Can Negotiate Anything</title> <author name="Herb Cohen" /> <category cid="12">Negotiate and narrative skill</category> </book> </booklist> |
▶ main.py
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 |
import xml.dom.minidom document = xml.dom.minidom.parse("book.xml") titleElement = document.createElement("title") titleText = document.createTextNode("파이썬 3.6 프로그래밍") titleElement.appendChild(titleText) bookElement = document.createElement("book") bookElement.setAttribute("ISBN", "979-11-5839-071-6") bookElement.appendChild(titleElement) booklistElement = document.firstChild booklistElement.appendChild(bookElement) lastBookElement = booklistElement.childNodes[booklistElement.childNodes.length - 1] print(lastBookElement.attributes["ISBN"].value) print(lastBookElement.childNodes[0].childNodes[0].data) """ 979-11-5839-071-6 파이썬 3.6 프로그래밍 """ |