#!/usr/bin/env python
import os, sys
import re
def replaceXmlEncoding(filepath, oldEncoding='gb2312', newEncoding='utf-8'):
f = open(filepath, mode='r')
content = f.read()
content = re.sub(oldEncoding, newEncoding, content)
f.close()
f = open(filepath, mode='w')
f.write(content)
f.close()
if __name__ == "__main__":
replaceXmlEncoding('./ActivateAccount.xml')
#!/usr/bin/env python
import os, re
import xml.etree.ElementTree as etree
Locale_Path = "./locale.txt"
class xmlExtractor(object):
def __init__(self):
pass
def __call__(self, filepath):
retDict = {}
f = open(filepath, 'r')
Line = len(open(filepath, 'r').readlines())
retDict['Line'] = Line
tree = etree.parse(f)
root = tree.find("ResItem")
Id = root.get("ID")
retDict['Title'] = Id
resItemCnt = len(list(root.findall("ResItem"))) + 1
retDict['ResItemCount'] = resItemCnt
retDict['ChineseTip'] = 'None'
for child in root:
attrDict = child.attrib
keyword = "Name"
if(keyword in attrDict.keys() and attrDict['Name'] == "Caption"):
if len(child.attrib['Value']) > 1:
if child.attrib['Value'][0] == '~':
title = child.attrib['Value'][1:]
else:
title = child.attrib['Value'][0:]
#print(title)
chs = open(Locale_Path).read()
pattern = '<String id="' + title + '">[^>]+>'
m = re.search(pattern, chs)
if m != None:
realTitle = re.sub('<[^>]+>', '', m.group(0))
retDict['ChineseTip'] = realTitle
f.close()
return retDict
if __name__ == "__main__":
fo = xmlExtractor()
d = fo('./ActivateAccount.xml')
print(d)
#!/usr/bin/env python
from xmlExtractor import *
from replaceXmlEncoding import *
from xml.dom import minidom,Node
doc = minidom.Document()
extractor = xmlExtractor()
totalLines = 0
totalResItemCnt = 0
totalXmlFileCnt = 0
totalErrorCnt = 0
errorFileList = []
xmlRoot = doc.createElement("XmlResourceFile")
doc.appendChild(xmlRoot)
def myWalkDir(level, path):
global doc, extractor, totalLines, totalResItemCnt, totalXmlFileCnt
global totalErrorCnt, errorFileList
global xmlRoot
for i in os.listdir(path):
if i[-3:] == 'xml':
totalXmlFileCnt += 1
try:
#先把xml的encoding由gb2312转换为utf-8
replaceXmlEncoding(path + '\\' + i)
#再提取xml文档中需要的信息
info = extractor(path + '\\' + i)
#在上述两行代码没有出现异常的基础上再创建节点
#print(info)
#print(type(i))
xmlNode = doc.createElement("XmlFile")
xmlRoot.appendChild(xmlNode)
xmlName = doc.createElement("Filename")
xmlName.setAttribute('Value', i)
#xmlName.appendChild(doc.createTextNode(i))
xmlNode.appendChild(xmlName)
filePath = doc.createElement("Filepath")
filePath.setAttribute('Value', path[34:])
#filePath.appendChild(doc.createTextNode(path[1:]))
xmlNode.appendChild(filePath)
titleNode = doc.createElement("Title")
titleNode.setAttribute('Value', str(info['Title']))
#titleNode.appendChild(doc.createTextNode(str(info['Title'])))
xmlNode.appendChild(titleNode)
chsNode = doc.createElement("ChineseTip")
chsNode.setAttribute('Value', str(info['ChineseTip']))
#chsNode.appendChild(doc.createTextNode(str(info['Chinese'])))
xmlNode.appendChild(chsNode)
resItemNode = doc.createElement("ResItemCount")
resItemNode.setAttribute('Value', str(info['ResItemCount']))
#resItemNode.appendChild(doc.createTextNode(str(info['ResItemCount'])))
xmlNode.appendChild(resItemNode)
lineNode = doc.createElement("LineCount")
lineNode.setAttribute('Value', str(info['Line']))
#lineNode.appendChild(doc.createTextNode(str(info['Line'])))
xmlNode.appendChild(lineNode)
descNode = doc.createElement("Description")
descNode.setAttribute('Value', '')
#descNode.appendChild(doc.createTextNode(''))
xmlNode.appendChild(descNode)
except Exception as errorDetail:
totalErrorCnt += 1
errorFileList.append(path + '\\' + i)
print(path + '\\' + i, errorDetail)
if os.path.isdir(path + '\\' + i):
myWalkDir(level+1, path + '\\' + i)
if __name__ == "__main__":
path = os.getcwd() + '\\themes'
myWalkDir(0, path)
print(totalXmlFileCnt, totalErrorCnt)
#print(doc.toprettyxml(indent = " "))
resultXml = open("./xmlResourceList.xml", "w")
resultXml.write(doc.toprettyxml(indent = " "))
resultXml.close()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有