Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用shell脚本在xml文件中添加xml元素_Xml_Shell_Xml Parsing - Fatal编程技术网

使用shell脚本在xml文件中添加xml元素

使用shell脚本在xml文件中添加xml元素,xml,shell,xml-parsing,Xml,Shell,Xml Parsing,我有一个需要添加数据的xml文件,我尝试使用sed。谁能帮我找出哪里做错了 我有这样一个xml文件: <!DOCTYPE sf-migration PUBLIC "-//VA Software, Inc.//DTD SourceForge Migration 1.0//EN" "/opt/add-ons/import-6.1/dtd/sf-migration_1_0.dtd"> <sf-migration toVersion="6.1.0.0" fromVersion="5.0

我有一个需要添加数据的xml文件,我尝试使用sed。谁能帮我找出哪里做错了

我有这样一个xml文件:

<!DOCTYPE sf-migration
PUBLIC "-//VA Software, Inc.//DTD SourceForge Migration 1.0//EN"
"/opt/add-ons/import-6.1/dtd/sf-migration_1_0.dtd">
<sf-migration toVersion="6.1.0.0" fromVersion="5.0.0.0">
<users></users>
</sf-migration>

现在我需要在用户中添加以下文件:

<sfuser xid="user1000">
           <username>beq03838</username>
           <email>noreply@nxp.com</email>
           <alternateEmail1></alternateEmail1>
           <dateCreated>2016-12-20 14:02:23 CET</dateCreated>
           <dateLastModified>2016-12-20 14:02:23 CET</dateLastModified>
           <detail></detail>
           <monitoringPreference>email</monitoringPreference>
           <lastLogin></lastLogin>
      </sfuser>

beq03838
noreply@nxp.com
2016-12-20 14:02:23 CET
2016-12-20 14:02:23 CET
电子邮件
我的输出应如下所示:

enter <!DOCTYPE sf-migration
PUBLIC "-//VA Software, Inc.//DTD SourceForge Migration 1.0//EN"
"/opt/add-ons/import-6.1/dtd/sf-migration_1_0.dtd">
<sf-migration toVersion="6.1.0.0" fromVersion="5.0.0.0">
<users>
<sfuser xid="user1000">
<username>beq03838</username>
<email>noreply@nxp.com</email>
<alternateEmail1></alternateEmail1>
<dateCreated>2016-12-20 14:02:23 CET</dateCreated>
<dateLastModified>2016-12-20 14:02:23 CET</dateLastModified>
<lastLogin></lastLogin>
</sfuser>
</users>
</sf-migration>
enter
beq03838
noreply@nxp.com
2016-12-20 14:02:23 CET
2016-12-20 14:02:23 CET
我使用下面的shell脚本附加文本:

CONTENT=give the suer content
C=$(echo $CONTENT | sed 's/\//\\\//g')
sed "/<\/users>/ s/.*/${C}\n&/" /tmptest/user1.xml
CONTENT=给出suer内容
C=$(echo$CONTENT | sed's/\/\\\//g')
sed”//s/*/${C}\n&/“/tmptest/user1.xml
我做错了什么?有没有其他简单的方法在xml中添加元素?
我需要在shell中完成这项工作,因为我的完整代码在shell中处理多个文件,并且在命令行上转义非常容易出错,而且非常粗糙。在大多数语言中,用一个非常小的脚本就可以实现同样的效果。下面是Python 2中的一个示例:

template = open("user.xml").read()
snippet = open("user_data.xml").read()
result = template.replace("</users>", snippet + "</users>")
open("user.xml", "w").write(result)
用法:

./prepend.py template.xml snippet.xml "</users>"
/prepend.py template.xml snippet.xml“”

您遇到的错误是什么?“输入”作为输出的第一个字是否是有意的?谢谢,。但是在第一个选项中,只打印输出。但是我需要将结果的输出保存在源文件(模板)中。将输出重定向到该文件或使用Python:
open(“template.xml”,“w”)。写入(结果)
您能给我格式吗?#/usr/bin/python template=open(“user.xml”,“rw”).read()snippet=open(“user_data.xml”).read()打印模板。替换(“,snippet+”),它应该使用打印提供的所需输出将输出保存回“user.xml”
#!/usr/bin/python

import sys
if len(sys.argv) != 4:
  print "This script prepends the given pattern in a template with a snippet. Both snippet and template are files."
  print "Usage:"
  print sys.argv[0] + " template snippet replacementpattern"
  sys.exit(1)

template = open(sys.argv[1]).read()
snippet = open(sys.argv[2]).read()
pattern = sys.argv[3]

if not pattern in template:
  print "Template doesn't contain the pattern."
  sys.exit(2)

print template.replace(pattern, snippet + pattern)
./prepend.py template.xml snippet.xml "</users>"