Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.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
在没有语法错误的XML文件中添加元素_Xml_Bash_Shell_Awk_Sed - Fatal编程技术网

在没有语法错误的XML文件中添加元素

在没有语法错误的XML文件中添加元素,xml,bash,shell,awk,sed,Xml,Bash,Shell,Awk,Sed,我有一个xml文件,如下所示 <Rows> <row> <name>peter</name> <id>345</id> </row> <row> <name>matt</name> <id>567</id> </row> </Rows>

我有一个xml文件,如下所示

<Rows>
    <row>
        <name>peter</name>
        <id>345</id>
    </row>
    <row>
        <name>matt</name>
        <id>567</id>
    </row>
</Rows>

i have achieved adding a sub element to this file like below:


<Rows>
    <row>
        <name>peter</name>
        <id>345</id>
    </row>
    <row>
        <name>matt</name>
        <id>567</id>
    </row>
<row>
<name>mano</name>
<id>897</id>
</row>
</Rows>

彼得
345
马特
567
我已实现向该文件添加一个子元素,如下所示:
彼得
345
马特
567
马诺
897
问题是添加的元素与其余元素不完全同步。我期待和其他人一样的语法(对齐)

已尝试的代码:

element="\t<row>\n\t <name>NewName</name>\n\t <id>NewID</id>\n\t</row>"
C=$(echo $element | sed 's/\//\\\//g')
echo "after C"
sed "/<\/rows>/ s/.*/${C}\n&/" filename
element=“\t\n\t NewName\n\t NewID\n\t”
C=$(echo$element | sed's/\/\\\//g')
回声“在C之后”
sed”//s/*/${C}\n&/“文件名

请协助。

最直接的答案可能是插入无缩进的记录,并使用xmllint--format在事后清理。建议的解决方案还可以提供对您非常有用的错误检查


哎呀……这是一个错误更正:)