谢谢你的帮助。我不是程序员,但我懂基本原则。我需要在一堆xml文件中这样做。我确信xpath或xtask或使用regex的某种组合可以实现这一点,但我迷路了。有人有什么想法吗?谢谢!
以下是范围:
将“scc_title”元素复制到“scc_comments”元素。scc_comments元素通常是空的。如果不是,我仍然需要它把它附加到当前的内容中。
<property name="scc_title" type="s">NEED TO COPY THIS TEXT</property>
<property name="scc_comments" type="s">AND PASTE IT HERE</property>
发布于 2014-02-25 15:37:57
使用python
和ElementTree
的另一种方法:
from __future__ import print_function
import sys
import xml.etree.ElementTree as ET
def main():
if len(sys.argv) < 3:
print("usage:", sys.argv[0], "input", "output")
sys.exit(1)
tree = ET.parse(sys.argv[1])
root = tree.getroot();
src = root.find(".//*[@name='scc_title']")
dst = root.find(".//*[@name='scc_comments']")
if src is not None and dst is not None:
dst.text += src.text
tree.write(sys.argv[2])
else:
if src is None:
print("Failed to find 'scc_title' attribute", file=sys.stderr)
if dst is None:
print("Failed to find 'scc_comments' attribute", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()
发布于 2014-02-25 15:32:54
Pythonic的非xml方法假设scc_title在scc_comments之前,每个标记都有自己的行,并且所有的XML文件都在同一个目录中,我没有测试这个,但这是基本思想。另外,我不确定是否有快速的GUI方式,我也不是程序员,所以可能有更好的方法来处理xml模块:
#put this in the directory with the xml files
import re
import os
#for file_name in current directory "."
for file_name in os.listdir("."):
if ".xml" in file_name:
outfile = open("edited_"+file_name,"w+")
with open(file_name,'r') as f:
for line in f:
if "scc_title" in line:
#split the string by two delimeters "<" and ">" and get the 3rd element starts at 0
scc_title_value = re.split('<|>',line)[2]
if "scc_comments" in line:
scc_comments_value = re.split('<|>',line)[2]
#replace scc_comments_value with scc_title_value
line = line.replace(scc_comments_value,scc_title_value)
outfile.write(line)
https://askubuntu.com/questions/426279
复制