Tuesday, March 29, 2011

Python дээр XML үүсгэх, засварлах жишээ

Уг жишээг Б. Буянбадрах хийж гүйцэтгэв.

# To change this template, choose Tools | Templates
# and open the template in the editor.

__author__="Benefaction"
__date__ ="$Mar 23, 2011 11:40:30 AM$"
from xml.dom.minidom import Document
from xml.dom.minidom import parseString

def createXmlFile():
    doc = Document()

    wml = doc.createElement("wml")
    doc.appendChild(wml)

    maincard = doc.createElement("card")
    wml.appendChild(maincard)

    ptext = doc.createTextNode("This is xmlSample project")
    maincard.appendChild(ptext)
    print(doc.toprettyxml(indent="  "))
    xmlFile = open('d://xmlSample.xml','w')
    try:
        xmlFile.write(doc.toprettyxml(indent="  "))
    except():
        print("I/O error")
     
def editXmlFile():
    file = open('d://xmlSample.xml','r')
    data = file.read()
    file.close()
    dom = parseString(data)

    dom = parseString(dom.toprettyxml(indent="  ").replace('','').replace('',''))
    xmlFile = open('d://xmlSample.xml','w')
    print(dom.toprettyxml(indent="  "))
    try:
        xmlFile.write(dom.toprettyxml(indent="  "))
    except():
        print("I/O error")
 
if __name__ == "__main__":
    createXmlFile()
    editXmlFile()




Ч. Эрдэнэбат
2011-3-29

No comments:

Post a Comment