def save(self, file_name: str) -> None: 'Saves the file.' impl = dom.getDOMImplementation() d = impl.createDocument(None, 'export', dom.DocumentType('export')) for key, rlist in self._data.items(): suite = d.createElement("suite") suite.setAttribute("directory", os.path.split(key.properties.directory)[1]) total = d.createElement("total") total.appendChild(d.createTextNode(str(key.totalPercent))) suite.appendChild(total) average = d.createElement("average") average.appendChild(d.createTextNode(str(key.averagePercent))) suite.appendChild(average) for result in rlist: relem = d.createElement("result") relem.setAttribute("inspection", result.inspection) shape = d.createElement("shape") shape.appendChild(d.createTextNode(str(result.shapeScore))) relem.appendChild(shape) color = d.createElement("color") color.appendChild(d.createTextNode(str(result.colorScore))) relem.appendChild(color) total = d.createElement("total") total.appendChild(d.createTextNode(str(result.totalScore))) relem.appendChild(total) suite.appendChild(relem) d.documentElement.appendChild(suite) d.documentElement.setAttribute("timestamp", str(int(time.time() * 1000))) open(file_name, 'w').write(d.toprettyxml(' '))
def exportMessages(self) -> str: 'Exports the messages as xml.' doc = dom.getDOMImplementation().createDocument( None, 'messages', dom.DocumentType('messages')) doc.documentElement.setAttribute("room", self._roomName) for message in self._messages: elem = doc.createElement("message") elem.setAttribute("user", message.user) elem.setAttribute("timestamp", str(message.timestamp)) elem.appendChild(doc.createTextNode(message.message)) doc.documentElement.appendChild(elem) return doc.toprettyxml(indent=' ')
def toXml(self, filename): doc = dom.Document() dt = dom.DocumentType("model") dt.systemId = "embsysregview.dtd" doc.appendChild(dt) model = addChild(doc, 'model', chipname=self.chipname) import datetime descr = '''\n--------------\nThis file was generated by regViewGen v%d (%s) %s''' % ( __version__, __author__, str(datetime.datetime.now())) addChild(model, 'chip_description', self.description + descr) for group in self.groups: group.toXml(model) with open(filename, 'w') as f: doc.writexml(f, indent='\t', addindent='\t', newl='\n', encoding='UTF-8')
def create_base_xhtml(self): doc_type = minidom.DocumentType("html") if self.add_mathml: doc_type.publicId = "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" doc_type.systemId = "http://www.w3.org/TR/MathML2/dtd/xhtml-math11-f.dtd" self.doc.appendChild(doc_type)
def make_post_xml(id): sql = "SELECT * FROM Post WHERE PostNo = %s;" % id cursor.execute(sql) target_post = cursor.fetchall()[0] sql = "SELECT * FROM Reply WHERE PostNo = %s ORDER BY ReplyNo Asc;" % id cursor.execute(sql) replies = cursor.fetchall() replyNum = cursor.rowcount doc = mdom.Document() doctype = mdom.DocumentType('Post') doctype.systemId = "postprofile.dtd" doctype.publicId = None doc.appendChild(doctype) root = doc.createElement('Post') doc.appendChild(root) node_No = doc.createElement('No') node_No.appendChild(doc.createTextNode(str(target_post[0]))) node_postuser = doc.createElement('PostUser') sql = "SELECT UserName FROM User WHERE UserNo = %s" % target_post[1] cursor.execute(sql) username = cursor.fetchall() node_block = doc.createElement('Block') sql = "select SectionName from Section where SectionNo = %s" % target_post[2] cursor.execute(sql) node_block.appendChild(doc.createTextNode((str(cursor.fetchall()[0][0])))) node_postuser.appendChild(doc.createTextNode(str(username[0][0]))) node_title = doc.createElement('Title') node_title.appendChild(doc.createTextNode(str(target_post[3]))) node_content = doc.createElement('Content') node_content.appendChild(doc.createTextNode(str(target_post[4]))) node_posttime = doc.createElement('PostTime') node_posttime.appendChild(doc.createTextNode(str(target_post[6]))) node_clicks = doc.createElement('Clicks') node_clicks.appendChild(doc.createTextNode(str(target_post[5]))) node_replynum = doc.createElement('ReplyNum') node_replynum.appendChild(doc.createTextNode(str(replyNum))) node_replies = doc.createElement('Replies') for y in replies: node_reply = doc.createElement('Reply') node_floor = doc.createElement('Floor') node_floor.appendChild(doc.createTextNode(str(y[1]))) node_orino = doc.createElement('OriginalNo') node_orino.appendChild(doc.createTextNode(str(y[0]))) node_replyuser = doc.createElement('ReplyUser') sql = "SELECT UserName FROM User WHERE UserNo = %s" % y[3] cursor.execute(sql) replyuser = cursor.fetchall() node_replyuser.appendChild(doc.createTextNode(str(replyuser[0][0]))) node_replycontent = doc.createElement('ReplyContent') original_content = str(y[4]) reply_content = original_content[0:80] + '...' if len(original_content) > 80 else original_content node_replycontent.appendChild(doc.createTextNode(reply_content)) node_replytime = doc.createElement('ReplyTime') node_replytime.appendChild(doc.createTextNode(str(y[5]))) node_praisenum = doc.createElement('PraiseNum') node_praisenum.appendChild(doc.createTextNode(str(y[6]))) node_reply.appendChild(node_floor) node_reply.appendChild(node_orino) node_reply.appendChild(node_replyuser) node_reply.appendChild(node_replycontent) node_reply.appendChild(node_replytime) node_reply.appendChild(node_praisenum) node_replies.appendChild(node_reply) root.appendChild(node_No) root.appendChild(node_postuser) root.appendChild(node_block) root.appendChild(node_title) root.appendChild(node_content) root.appendChild(node_posttime) root.appendChild(node_clicks) root.appendChild(node_replynum) root.appendChild(node_replies) filepath = './static/xml/post_%s.xml' % target_post[0] xmlfile = open(filepath, 'w', encoding='utf-8') doc.writexml(xmlfile, indent='\t', addindent='\t', newl='\n', encoding='UTF-8') xmlfile.close()
def make_section_xml(id): sql = "SELECT * FROM Section WHERE SectionNo = %s" % id cursor.execute(sql) section = cursor.fetchall() sql = "SELECT * FROM Post WHERE SectionNo = %s ORDER BY PostTime Desc" % id cursor.execute(sql) posts = cursor.fetchall() postNum = cursor.rowcount doc = mdom.Document() doctype = mdom.DocumentType('Section') doctype.systemId = "secprofile.dtd" doctype.publicId = None doc.appendChild(doctype) root = doc.createElement('Section') doc.appendChild(root) node_info = doc.createElement('Info') node_secid = doc.createElement('SecId') node_secid.appendChild(doc.createTextNode(str(id))) node_secname = doc.createElement('SecName') node_secname.appendChild(doc.createTextNode(str(section[0][1]))) node_master = doc.createElement('Master') sql = "SELECT UserName FROM User WHERE UserNo = %s" % section[0][3] cursor.execute(sql) master_name = cursor.fetchall() node_master.appendChild(doc.createTextNode(str(master_name[0][0]))) node_postnum = doc.createElement('PostNum') node_postnum.appendChild(doc.createTextNode(str(postNum))) node_desc = doc.createElement('Description') node_desc.appendChild(doc.createTextNode(str(section[0][2]))) node_info.appendChild(node_secid) node_info.appendChild(node_secname) node_info.appendChild(node_master) node_info.appendChild(node_postnum) node_info.appendChild(node_desc) root.appendChild(node_info) node_posts = doc.createElement('Posts') for x in posts: node_post = doc.createElement('Post') node_No = doc.createElement('No') node_No.appendChild(doc.createTextNode(str(x[0]))) node_postuser = doc.createElement('PostUser') sql = "SELECT UserName FROM User WHERE UserNo = %s" % x[1] cursor.execute(sql) username = cursor.fetchall() node_postuser.appendChild(doc.createTextNode(str(username[0][0]))) node_title = doc.createElement('Title') node_title.appendChild(doc.createTextNode(str(x[3]))) #print(node_title.getElementsByTagName('Title')[0]) node_content = doc.createElement('Content') node_content.appendChild(doc.createTextNode(str(x[4]))) node_posttime = doc.createElement('PostTime') node_posttime.appendChild(doc.createTextNode(str(x[6]))) node_clicks = doc.createElement('Clicks') node_clicks.appendChild(doc.createTextNode(str(x[5]))) node_replies = doc.createElement('Replies') sql = "SELECT * FROM Reply WHERE PostNo = %s ORDER BY ReplyTime Desc LIMIT 0, 3" % x[0] cursor.execute(sql) replies = cursor.fetchall() for y in replies: node_reply = doc.createElement('Reply') node_floor = doc.createElement('Floor') node_floor.appendChild(doc.createTextNode(str(y[1]))) node_replyuser = doc.createElement('ReplyUser') sql = "SELECT UserName FROM User WHERE UserNo = %s" % y[3] cursor.execute(sql) replyuser = cursor.fetchall() node_replyuser.appendChild(doc.createTextNode(str(replyuser[0][0]))) node_replycontent = doc.createElement('ReplyContent') original_content = str(y[4]) reply_content = original_content[0:80]+'...' if len(original_content) > 80 else original_content node_replycontent.appendChild(doc.createTextNode(reply_content)) node_replytime = doc.createElement('ReplyTime') node_replytime.appendChild(doc.createTextNode(str(y[5]))) node_praisenum = doc.createElement('PraiseNum') node_praisenum.appendChild(doc.createTextNode(str(y[6]))) node_reply.appendChild(node_floor) node_reply.appendChild(node_replyuser) node_reply.appendChild(node_replycontent) node_reply.appendChild(node_replytime) node_reply.appendChild(node_praisenum) node_replies.appendChild(node_reply) node_post.appendChild(node_No) node_post.appendChild(node_postuser) node_post.appendChild(node_title) node_post.appendChild(node_content) node_post.appendChild(node_posttime) node_post.appendChild(node_clicks) node_post.appendChild(node_replies) node_posts.appendChild(node_post) root.appendChild(node_posts) filepath = './static/xml/sec_%s.xml' % section[0][0] xmlfile = open(filepath, 'w', encoding='utf-8') doc.writexml(xmlfile, indent='\t', addindent='\t', newl='\n', encoding='UTF-8') xmlfile.close()
def make_user_xml(id): sql = "SELECT * FROM User WHERE UserNo = %s" % id cursor.execute(sql) user = cursor.fetchall() sql = "SELECT * FROM Post WHERE UserNo = %s ORDER BY PostTime Desc" % id cursor.execute(sql) posts = cursor.fetchall() sql = "SELECT * FROM Reply WHERE UserNo = %s ORDER BY ReplyTime Desc" % id cursor.execute(sql) replies = cursor.fetchall() doc = mdom.Document() doctype = mdom.DocumentType('User') doctype.systemId = "userprofile.dtd" doctype.publicId = None doc.appendChild(doctype) root = doc.createElement('User') doc.appendChild(root) node_username = doc.createElement('UserName') node_username.appendChild(doc.createTextNode(str(user[0][1]))) root.appendChild(node_username) node_info = doc.createElement('Info') node_basicinfo = doc.createElement('BasicInfo') node_gender = doc.createElement('Gender') node_gender.appendChild(doc.createTextNode(str(user[0][2]))) node_age = doc.createElement('Age') node_age.appendChild(doc.createTextNode(calculate_age(user[0][3]))) node_level = doc.createElement('Level') node_level.appendChild(doc.createTextNode(calculate_level(user[0][6]))) node_birth = doc.createElement('Birthday') node_birth.appendChild(doc.createTextNode(str(user[0][3]))) node_email = doc.createElement('Email') node_email.appendChild(doc.createTextNode(str(user[0][5]))) node_basicinfo.appendChild(node_gender) node_basicinfo.appendChild(node_age) node_basicinfo.appendChild(node_level) node_basicinfo.appendChild(node_birth) node_basicinfo.appendChild(node_email) node_info.appendChild(node_basicinfo) node_otherinfo = doc.createElement('OtherInfo') node_posts = doc.createElement('Posts') node_replies = doc.createElement('Replies') for x in posts: node_post = doc.createElement('Post') node_No = doc.createElement('No') node_No.appendChild(doc.createTextNode(str(x[0]))) node_block = doc.createElement('Block') sql = "select SectionName from Section where SectionNo = %s" % x[2] cursor.execute(sql) sec = cursor.fetchall() node_block.appendChild(doc.createTextNode(str(sec[0][0]))) node_postuser = doc.createElement('PostUser') node_postuser.appendChild(doc.createTextNode(str(user[0][1]))) node_title = doc.createElement('Title') node_title.appendChild(doc.createTextNode(str(x[3]))) node_content = doc.createElement('Content') node_content.appendChild(doc.createTextNode(str(x[4]))) node_posttime = doc.createElement('PostTime') node_posttime.appendChild(doc.createTextNode(str(x[6]))) node_clicks = doc.createElement('Clicks') node_clicks.appendChild(doc.createTextNode(str(x[5]))) node_replynum = doc.createElement('ReplyNum') node_replynum.appendChild(doc.createTextNode(str(x[7]))) node_post.appendChild(node_No) node_post.appendChild(node_block) node_post.appendChild(node_postuser) node_post.appendChild(node_title) node_post.appendChild(node_content) node_post.appendChild(node_posttime) node_post.appendChild(node_clicks) node_post.appendChild(node_replynum) node_posts.appendChild(node_post) for x in replies: node_reply = doc.createElement('Reply') sql = "select * from Post where PostNo = %s" % x[2] cursor.execute(sql) temp_post = cursor.fetchall() node_title = doc.createElement('Title') node_title.appendChild(doc.createTextNode(str(temp_post[0][3]))) sql = "select UserName from User where UserNo = %s" % temp_post[0][1] cursor.execute(sql) temp_user = cursor.fetchall() node_postuser = doc.createElement('PostUser') node_postuser.appendChild(doc.createTextNode(str(temp_user[0][0]))) node_postid = doc.createElement('PostID') node_postid.appendChild(doc.createTextNode(str(x[2]))) node_original = doc.createElement('OriginalNo') node_original.appendChild(doc.createTextNode(str(x[0]))) node_floor = doc.createElement('Floor') node_floor.appendChild(doc.createTextNode(str(x[1]))) node_replyuser = doc.createElement('ReplyUser') node_replyuser.appendChild(doc.createTextNode(str(user[0][1]))) node_replycontent = doc.createElement('ReplyContent') node_replycontent.appendChild(doc.createTextNode(str(x[4]))) node_replytime = doc.createElement('ReplyTime') node_replytime.appendChild(doc.createTextNode(str(x[5]))) node_praisenum = doc.createElement('PraiseNum') node_praisenum.appendChild(doc.createTextNode(str(x[6]))) node_reply.appendChild(node_title) node_reply.appendChild(node_postuser) node_reply.appendChild(node_postid) node_reply.appendChild(node_original) node_reply.appendChild(node_floor) node_reply.appendChild(node_replyuser) node_reply.appendChild(node_replycontent) node_reply.appendChild(node_replytime) node_reply.appendChild(node_praisenum) node_replies.appendChild(node_reply) node_otherinfo.appendChild(node_posts) node_otherinfo.appendChild(node_replies) node_info.appendChild(node_otherinfo) root.appendChild(node_info) filepath = './static/xml/user_%s.xml' % user[0][1] xmlfile = open(filepath, 'w', encoding='utf-8') doc.writexml(xmlfile, indent='\t', addindent='\t', newl='\n', encoding='UTF-8') xmlfile.close()