[英]replace tree elements in xml with python
#!/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])
Here I want to replace the xml elements with the specified solver Name, I tried in the following way mentoined below but nothing is working out. 在这里,我想用指定的求解器名称替换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)
Here is the Sample of my Xml file here using this xml file i want to replace the solver TFGMR with the specified solver in my python code. 这里是我的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>
You can remove from the parent and then replace using xml.ElementTree : 您可以从父项中删除,然后使用xml.ElementTree替换:
from xml.etree import ElementTree as et
tree = et.parse("your.xml")
x = tree.getroot()
for s in x.findall(".//solver"):
ch = s.find("./TFQMR")
if ch is not None:
s.remove(ch)
s.append(et.SubElement(s, "CG"))
print(et.tostring(x))
using your snippet: 使用您的代码段:
In [55]: from xml.etree import ElementTree as et
In [56]: tree = et.parse("test.xml")
In [57]: x = tree.getroot()
In [58]: for s in x.findall(".//solver"):
....: ch = s.find("./TFQMR")
....: if ch is not None:
....: s.remove(ch)
....: s.append(et.SubElement(s, "CG"))
....:print(et.tostring(x))
....:
<system>
<solutionStrategy>
<standard>
<matrix storage="sparseNonSym" />
</standard>
</solutionStrategy>
<solverList>
<lis id="default">
<precond>
<saamg />
</precond>
<solver>
<CG /><CG /></solver>
<maxIter>10000</maxIter>
<tolerance>1.0E-10</tolerance>
<logging>false</logging>
</lis>
</solverList>
</system>
Using your file, you need to register the namespace: 使用您的文件,您需要注册命名空间:
from xml.etree import ElementTree as et
tree = et.parse(.xml")
x = tree.getroot()
namespaces = {'cfsSimulation': "http://www.cfs++.org"}
solvers = x.findall(
"cfsSimulation:sequenceStep/cfsSimulation:linearSystems/cfsSimulation:system"
"//cfsSimulation:solverList/cfsSimulation:lis/cfsSimulation:solver",namespaces)
for s in solvers:
ch = s.find("./cfsSimulation:TFQMR", namespaces)
if ch is not None:
s.remove(ch)
s.append(et.SubElement(s, "CG"))
print(et.tostring(x))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.