<abbr id="ciwa6"><option id="ciwa6"></option></abbr>
  • <sup id="ciwa6"><kbd id="ciwa6"></kbd></sup>
    <small id="ciwa6"></small>
  • 千鋒教育-做有情懷、有良心、有品質(zhì)的職業(yè)教育機構(gòu)

    400-811-9990
    手機站
    千鋒教育

    千鋒學(xué)習(xí)站 | 隨時隨地免費學(xué)

    千鋒教育

    掃一掃進入千鋒手機站

    領(lǐng)取全套視頻
    千鋒教育

    關(guān)注千鋒學(xué)習(xí)站小程序
    隨時隨地免費學(xué)習(xí)課程

    上海
    • 北京
    • 鄭州
    • 武漢
    • 成都
    • 西安
    • 沈陽
    • 廣州
    • 南京
    • 深圳
    • 大連
    • 青島
    • 杭州
    • 重慶
    當(dāng)前位置:成都千鋒IT培訓(xùn)  >  技術(shù)干貨  >  使用python自帶的xml.dom創(chuàng)建和解析xml

    使用python自帶的xml.dom創(chuàng)建和解析xml

    來源:千鋒教育
    發(fā)布人:xqq
    時間: 2023-11-05 20:18:49

    python中的xml.dom模塊使用的就是傳統(tǒng)的dom解析api和方法。所以也就不寫什么了,主要就是練習(xí)敲敲代碼,繼續(xù)熟悉python。本文通過xml.dom.minidom創(chuàng)建一個xml文檔,然后再解析出來,用以熟悉相關(guān)接口方法的使用。

    創(chuàng)建一個xml文檔:

    '''

    Createdon2012-1-10

    Createaxmldocument

    @author:xiaojay

    '''

    fromxml.domimportminidom

    doc=minidom.Document()

    doc.appendChild(doc.createComment("Thisisasimplexml."))

    booklist=doc.createElement("booklist")

    doc.appendChild(booklist)

    defaddBook(newbook):

    book=doc.createElement("book")

    book.setAttribute("id",newbook["id"])

    title=doc.createElement("title")

    title.appendChild(doc.createTextNode(newbook["title"]))

    book.appendChild(title)

    author=doc.createElement("author")

    name=doc.createElement("name")

    firstname=doc.createElement("firstname")

    firstname.appendChild(doc.createTextNode(newbook["firstname"]))

    lastname=doc.createElement("lastname")

    lastname.appendChild(doc.createTextNode(newbook["lastname"]))

    name.appendChild(firstname)

    name.appendChild(lastname)

    author.appendChild(name)

    book.appendChild(author)

    pubdate=doc.createElement("pubdate")

    pubdate.appendChild(doc.createTextNode(newbook["pubdate"]))

    book.appendChild(pubdate)

    booklist.appendChild(book)

    addBook({"id":"1001","title":"Anapple","firstname":"Peter","lastname":"Zhang","pubdate":"2012-1-12"})

    addBook({"id":"1002","title":"Love","firstname":"Mike","lastname":"Li","pubdate":"2012-1-10"})

    addBook({"id":"1003","title":"Steve.Jobs","firstname":"Tom","lastname":"Wang","pubdate":"2012-1-19"})

    addBook({"id":"1004","title":"HarryPotter","firstname":"Peter","lastname":"Chen","pubdate":"2012-11-11"})

    f=file("book.xml","w")

    doc.writexml(f)

    f.close()

    通過doc.toprettyxml(indent,newl,encoding)方法可以優(yōu)雅顯示xml文檔,但是要避免直接寫入文本,否則會給解析帶來麻煩,盡量使用自帶的writexml方法。

    生成的文檔內(nèi)容:

    Peter

    Zhang

    2012-1-12

    .................

    解析該xml文檔:

    '''

    Createdon2012-1-10

    Scanaxmldoc

    @author:xiaojay

    '''

    fromxml.domimportminidom,Node

    classbookscanner:

    def__init__(self,doc):

    forchildindoc.childNodes:

    ifchild.nodeType==Node.ELEMENT_NODE\

    andchild.tagName=="book":

    bookid=child.getAttribute("id")

    print"*"*20

    print"Bookid:",bookid

    self.handle_book(child)

    defhandle_book(self,node):

    forchildinnode.childNodes:

    ifchild.nodeType==Node.ELEMENT_NODE:

    ifchild.tagName=="title":

    print"Title:",self.getText(child.firstChild)

    ifchild.tagName=="author":

    self.handle_author(child)

    ifchild.tagName=="pubdate":

    print"Pubdate:",self.getText(child.firstChild)

    defgetText(self,node):

    ifnode.nodeType==Node.TEXT_NODE:

    returnnode.nodeValue

    else:return""

    defhandle_author(self,node):

    author=node.firstChild

    forchildinauthor.childNodes:

    ifchild.nodeType==Node.ELEMENT_NODE:

    ifchild.tagName=="firstname":

    print"Firstname:",self.getText(child.firstChild)

    ifchild.tagName=="lastname":

    print"Lastname:",self.getText(child.firstChild)

    doc=minidom.parse("book.xml")

    forchildindoc.childNodes:

    ifchild.nodeType==Node.COMMENT_NODE:

    print"Conment:",child.nodeValue

    ifchild.nodeType==Node.ELEMENT_NODE:

    bookscanner(child)

    以上內(nèi)容為大家介紹了使用python自帶的xml.dom創(chuàng)建和解析xml,希望對大家有所幫助,如果想要了解更多Python相關(guān)知識,請關(guān)注IT培訓(xùn)機構(gòu):千鋒教育。

    聲明:本站稿件版權(quán)均屬千鋒教育所有,未經(jīng)許可不得擅自轉(zhuǎn)載。

    猜你喜歡LIKE

    python delattr函數(shù)如何使用?

    2023-11-10

    python time.strptime的格式化

    2023-11-10

    pythonGIL在Python多線程的應(yīng)用

    2023-11-10

    最新文章NEW

    python中pdb模塊怎么用?

    2023-11-10

    Python如何截圖保存?

    2023-11-10

    python?中缺少module怎么辦?

    2023-11-10

    相關(guān)推薦HOT

    更多>>

    快速通道 更多>>

    最新開班信息 更多>>

    網(wǎng)友熱搜 更多>>