用python替换xml中的树元素

时间:2016-05-12 10:52:31

标签: python xml

#!/usr/bin/env python

import libxml2

import os.path

from cfs_utils import *

solvers = ["CG","CGS","BICGSTABL"]
precond = ["saamg","iluk","ssor"]
for s in range(3):
for p in range(3):
   print "s " + str(s)
   problem = "eval_" + str(solvers[s]) + "_" + str(precond[p]) + "_default"

#if os.path.exists(problem + ".info.xml"):
#  continue

print "execute " + problem + "\n"

doc = libxml2.parseFile("simp_lis_solver_3D.xml")
xml = doc.xpathNewContext()
solver = xml.xpathEval('//solverList')
xml.setContextNode(solvers)
solver_name = xml.xpathEval(solvers[s])

在这里,我想用指定的求解器名称替换xml元素,我尝试以下方式使用以下方法,但没有任何问题。

  #[0].getContent()
  #  xml.xpathRegisterNs('cfs', 'http://www.cfs++.org')
  #replace(xml, "//cfs:volume/@value", str(vol))
  # replace("pardiso", "CG", "CGS")
for line in doc:
   solvers[s].replace( 'TFQMR', 'CG')
   doc.saveFile(problem + ".xml")
   execute("cfs.rel -m bulk3d_10.mesh " + problem)

这里是我的Xml文件的示例,这里使用这个xml文件我想用我的python代码中的指定解算器替换解算器TFGMR。

    

  <system>
    <solutionStrategy>
      <standard>
        <matrix storage="sparseNonSym"/>
      </standard>
    </solutionStrategy>
    <solverList>
      <lis id="default">
        <precond><saamg/>
        </precond>
        <solver>
        <TFQMR></TFQMR>
    </solver>
        <maxIter>10000</maxIter>
        <tolerance>1.0E-10</tolerance>
        <logging>false</logging>
      </lis>
    </solverList>
  </system>
</linearSystems>

0 个答案:

没有答案