编辑多个 XML 文件中的多个 XML 节点
Edit Multiple XML Nodes in Multiple XML files
我有多个 XML 文件(60+),我需要编辑多个文本节点(我认为它被称为)。
我熟悉 Java、JavaScript、Python、JQuery、PHP、HTML.
我可以用什么语言完成这个?
这是我当前的示例 XML 文档:
<?xml version="1.0" encoding="utf-8"?><bookstore>
<book category="cooking">
<title lang="en">Chinese</title>
<author>chinese author</author>
<year>2015</year>
<price>fourth</price>
</book>
<book category="cooking">
<title lang="en">All American</title>
<author>American Author</author>
<year>2015</year>
<price>6.00</price>
</book>
</bookstore>
例如,我想一次更改多个元素的作者和年份!
这是我的 python 代码,它将一次编辑一个节点。我需要一个循环或其他东西来一次编辑更多。
from xml.dom.minidom import parse
import os
# create a backup of original file
new_file_name = 'dom.xml'
old_file_name = new_file_name + "~"
os.rename(new_file_name, old_file_name)
# change text value of element
doc = parse(old_file_name)
node = doc.getElementsByTagName('author')
node[0].firstChild.nodeValue = 'new author'
# persist changes to new file
xml_file = open(new_file_name, "w")
doc.writexml(xml_file, encoding="utf-8")
xml_file.close()
如有任何帮助,我们将不胜感激。新手程序员在这里!
谢谢! :D
创建函数:
def create_backup(new_file_name):
""" create a backup of original file """
old_file_name = new_file_name + "~"
os.rename(new_file_name, old_file_name)
return old_file_name
def change_author(doc, new_author)
""" change text value of 'author' """
node = doc.getElementsByTagName('author')
node[0].firstChild.nodeValue = new_author
def save_changes(new_file_name, doc):
""" persist changes to new file """
xml_file = open(new_file_name, "w")
doc.writexml(xml_file, encoding="utf-8")
xml_file.close()
现在可以轻松创建循环了:
file_names = ['dom.xml', ...]
for new_file_name in file_names:
old_file_name = create_backup(new_file_name)
doc = parse(old_file_name)
change_author(doc, 'new author')
save_changes(new_file_name, doc)
就我个人而言,我会使用 shell 脚本和 XMLStarlet。
for f in *.xml; do
xmlstarlet ed \
-u '//author' -v 'new author' \
<"$f" >"$f.new" && mv "$f.new" "$f"
done
如果您只想更改图书 "All American" 的作者,同时更改同一本书的价格,则可以改为:
for f in *.xml; do
xmlstarlet ed \
-u '//book[title="All American"]/author' -v 'new author' \
-u '//book[title="All American"]/price' -v 12.34 \
<"$f" >"$f.new" && mv "$f.new" "$f"
done
请注意,如果任何其他用户都可以写入您正在使用的目录,则使用硬编码的临时文件名是一种不好的做法;如果是这样的话,使用mktemp
生成一个唯一的临时文件名会更合适。
我有多个 XML 文件(60+),我需要编辑多个文本节点(我认为它被称为)。 我熟悉 Java、JavaScript、Python、JQuery、PHP、HTML.
我可以用什么语言完成这个?
这是我当前的示例 XML 文档:
<?xml version="1.0" encoding="utf-8"?><bookstore>
<book category="cooking">
<title lang="en">Chinese</title>
<author>chinese author</author>
<year>2015</year>
<price>fourth</price>
</book>
<book category="cooking">
<title lang="en">All American</title>
<author>American Author</author>
<year>2015</year>
<price>6.00</price>
</book>
</bookstore>
例如,我想一次更改多个元素的作者和年份!
这是我的 python 代码,它将一次编辑一个节点。我需要一个循环或其他东西来一次编辑更多。
from xml.dom.minidom import parse
import os
# create a backup of original file
new_file_name = 'dom.xml'
old_file_name = new_file_name + "~"
os.rename(new_file_name, old_file_name)
# change text value of element
doc = parse(old_file_name)
node = doc.getElementsByTagName('author')
node[0].firstChild.nodeValue = 'new author'
# persist changes to new file
xml_file = open(new_file_name, "w")
doc.writexml(xml_file, encoding="utf-8")
xml_file.close()
如有任何帮助,我们将不胜感激。新手程序员在这里!
谢谢! :D
创建函数:
def create_backup(new_file_name):
""" create a backup of original file """
old_file_name = new_file_name + "~"
os.rename(new_file_name, old_file_name)
return old_file_name
def change_author(doc, new_author)
""" change text value of 'author' """
node = doc.getElementsByTagName('author')
node[0].firstChild.nodeValue = new_author
def save_changes(new_file_name, doc):
""" persist changes to new file """
xml_file = open(new_file_name, "w")
doc.writexml(xml_file, encoding="utf-8")
xml_file.close()
现在可以轻松创建循环了:
file_names = ['dom.xml', ...]
for new_file_name in file_names:
old_file_name = create_backup(new_file_name)
doc = parse(old_file_name)
change_author(doc, 'new author')
save_changes(new_file_name, doc)
就我个人而言,我会使用 shell 脚本和 XMLStarlet。
for f in *.xml; do
xmlstarlet ed \
-u '//author' -v 'new author' \
<"$f" >"$f.new" && mv "$f.new" "$f"
done
如果您只想更改图书 "All American" 的作者,同时更改同一本书的价格,则可以改为:
for f in *.xml; do
xmlstarlet ed \
-u '//book[title="All American"]/author' -v 'new author' \
-u '//book[title="All American"]/price' -v 12.34 \
<"$f" >"$f.new" && mv "$f.new" "$f"
done
请注意,如果任何其他用户都可以写入您正在使用的目录,则使用硬编码的临时文件名是一种不好的做法;如果是这样的话,使用mktemp
生成一个唯一的临时文件名会更合适。