python做xml文件的解析式非常方便的。一般用python解析XML有两种方式dom模式和sax模式,这里我只介绍一下dom模式,因为一般简单的解析dom模式还是比较适合的,他语法简单,并且执行速度也不会慢。
来看个例子,这个是我从notepad++导出来的一个语言设置模板。大概长这个样子。
<NotepadPlus>
<UserLang name="testlist" ext="" udlVersion="2.1">
<Settings>
<Global caseIgnored="no" allowFoldOfComments="no" foldCompact="no" forcePureLC="0" decimalSeparator="0" />
<Prefix Keywords1="no" Keywords2="no" Keywords3="no" Keywords4="no" Keywords5="no" Keywords6="no" Keywords7="no" Keywords8="no" />
</Settings>
<KeywordLists>
<Keywords name="Comments"></Keywords>
<Keywords name="Numbers, prefix1"></Keywords>
<Keywords name="Numbers, prefix2"></Keywords>
<Keywords name="Numbers, extras1"></Keywords>
<Keywords name="Numbers, extras2"></Keywords>
<Keywords name="Numbers, suffix1"></Keywords>
<Keywords name="Numbers, suffix2"></Keywords>
<Keywords name="Numbers, range"></Keywords>
<Keywords name="Operators1"></Keywords>
<Keywords name="Operators2"></Keywords>
<Keywords name="Folders in code1, open"></Keywords>
<Keywords name="Folders in code1, middle"></Keywords>
<Keywords name="Folders in code1, close"></Keywords>
<Keywords name="Folders in code2, open"></Keywords>
<Keywords name="Folders in code2, middle"></Keywords>
<Keywords name="Folders in code2, close"></Keywords>
<Keywords name="Folders in comment, open"></Keywords>
<Keywords name="Folders in comment, middle"></Keywords>
<Keywords name="Folders in comment, close"></Keywords>
<Keywords name="Keywords1"></Keywords>
<Keywords name="Keywords2"></Keywords>
<Keywords name="Keywords3"></Keywords>
<Keywords name="Keywords4"></Keywords>
<Keywords name="Keywords5"></Keywords>
<Keywords name="Keywords6"></Keywords>
<Keywords name="Keywords7"></Keywords>
<Keywords name="Keywords8"></Keywords>
<Keywords name="Delimiters"></Keywords>
</KeywordLists>
<Styles>
<WordsStyle name="DEFAULT" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="COMMENTS" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="LINE COMMENTS" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="NUMBERS" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="KEYWORDS1" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="KEYWORDS2" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="KEYWORDS3" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="KEYWORDS4" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="KEYWORDS5" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="KEYWORDS6" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="KEYWORDS7" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="KEYWORDS8" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="OPERATORS" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="FOLDER IN CODE1" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="FOLDER IN CODE2" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="FOLDER IN COMMENT" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="DELIMITERS1" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="DELIMITERS2" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="DELIMITERS3" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="DELIMITERS4" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="DELIMITERS5" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="DELIMITERS6" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="DELIMITERS7" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="DELIMITERS8" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
</Styles>
</UserLang>
</NotepadPlus>
我现在的目的是要改变keyword1的关键字,和前景色设置,注意标红的部分
源码是这样的
import xml.etree.ElementTree as ET
tree = ET.parse('testlanguage')
keyWordList = []
keyWordList.append('a ')
keyWordList.append('b ')
keyWordList.append('c ')
def setKeywords(keywordlist,color,no):
root = tree.getroot()
keywordNo = 'Keywords'+no
for child in root.iter('Keywords'):
if child.attrib['name'] == keywordNo:
wordstring = ''
for words in keyWordList:
wordstring += words
child.text = wordstring
keywordNo = 'KEYWORDS'+no
for child in root.iter('WordsStyle'):
if child.attrib['name'] == keywordNo:
child.attrib['fgColor'] = color
return
setKeywords(keyWordList,'ff1100','1') #set words
tree.write('output.xml')
执行后的结果:
<NotepadPlus>
<UserLang ext="" name="testlist" udlVersion="2.1">
<Settings>
<Global allowFoldOfComments="no" caseIgnored="no" decimalSeparator="0" foldCompact="no" forcePureLC="0" />
<Prefix Keywords1="no" Keywords2="no" Keywords3="no" Keywords4="no" Keywords5="no" Keywords6="no" Keywords7="no" Keywords8="no" />
</Settings>
<KeywordLists>
<Keywords name="Comments" />
<Keywords name="Numbers, prefix1" />
<Keywords name="Numbers, prefix2" />
<Keywords name="Numbers, extras1" />
<Keywords name="Numbers, extras2" />
<Keywords name="Numbers, suffix1" />
<Keywords name="Numbers, suffix2" />
<Keywords name="Numbers, range" />
<Keywords name="Operators1" />
<Keywords name="Operators2" />
<Keywords name="Folders in code1, open" />
<Keywords name="Folders in code1, middle" />
<Keywords name="Folders in code1, close" />
<Keywords name="Folders in code2, open" />
<Keywords name="Folders in code2, middle" />
<Keywords name="Folders in code2, close" />
<Keywords name="Folders in comment, open" />
<Keywords name="Folders in comment, middle" />
<Keywords name="Folders in comment, close" />
<Keywords name="Keywords1">a b c </Keywords>
<Keywords name="Keywords2" />
<Keywords name="Keywords3" />
<Keywords name="Keywords4" />
<Keywords name="Keywords5" />
<Keywords name="Keywords6" />
<Keywords name="Keywords7" />
<Keywords name="Keywords8" />
<Keywords name="Delimiters" />
</KeywordLists>
<Styles>
<WordsStyle bgColor="FFFFFF" fgColor="000000" fontStyle="0" name="DEFAULT" nesting="0" />
<WordsStyle bgColor="FFFFFF" fgColor="000000" fontStyle="0" name="COMMENTS" nesting="0" />
<WordsStyle bgColor="FFFFFF" fgColor="000000" fontStyle="0" name="LINE COMMENTS" nesting="0" />
<WordsStyle bgColor="FFFFFF" fgColor="000000" fontStyle="0" name="NUMBERS" nesting="0" />
<WordsStyle bgColor="FFFFFF" fgColor="ff1100" fontStyle="0" name="KEYWORDS1" nesting="0" />
<WordsStyle bgColor="FFFFFF" fgColor="000000" fontStyle="0" name="KEYWORDS2" nesting="0" />
<WordsStyle bgColor="FFFFFF" fgColor="000000" fontStyle="0" name="KEYWORDS3" nesting="0" />
<WordsStyle bgColor="FFFFFF" fgColor="000000" fontStyle="0" name="KEYWORDS4" nesting="0" />
<WordsStyle bgColor="FFFFFF" fgColor="000000" fontStyle="0" name="KEYWORDS5" nesting="0" />
<WordsStyle bgColor="FFFFFF" fgColor="000000" fontStyle="0" name="KEYWORDS6" nesting="0" />
<WordsStyle bgColor="FFFFFF" fgColor="000000" fontStyle="0" name="KEYWORDS7" nesting="0" />
<WordsStyle bgColor="FFFFFF" fgColor="000000" fontStyle="0" name="KEYWORDS8" nesting="0" />
<WordsStyle bgColor="FFFFFF" fgColor="000000" fontStyle="0" name="OPERATORS" nesting="0" />
<WordsStyle bgColor="FFFFFF" fgColor="000000" fontStyle="0" name="FOLDER IN CODE1" nesting="0" />
<WordsStyle bgColor="FFFFFF" fgColor="000000" fontStyle="0" name="FOLDER IN CODE2" nesting="0" />
<WordsStyle bgColor="FFFFFF" fgColor="000000" fontStyle="0" name="FOLDER IN COMMENT" nesting="0" />
<WordsStyle bgColor="FFFFFF" fgColor="000000" fontStyle="0" name="DELIMITERS1" nesting="0" />
<WordsStyle bgColor="FFFFFF" fgColor="000000" fontStyle="0" name="DELIMITERS2" nesting="0" />
<WordsStyle bgColor="FFFFFF" fgColor="000000" fontStyle="0" name="DELIMITERS3" nesting="0" />
<WordsStyle bgColor="FFFFFF" fgColor="000000" fontStyle="0" name="DELIMITERS4" nesting="0" />
<WordsStyle bgColor="FFFFFF" fgColor="000000" fontStyle="0" name="DELIMITERS5" nesting="0" />
<WordsStyle bgColor="FFFFFF" fgColor="000000" fontStyle="0" name="DELIMITERS6" nesting="0" />
<WordsStyle bgColor="FFFFFF" fgColor="000000" fontStyle="0" name="DELIMITERS7" nesting="0" />
<WordsStyle bgColor="FFFFFF" fgColor="000000" fontStyle="0" name="DELIMITERS8" nesting="0" />
</Styles>
</UserLang>
</NotepadPlus>