Splitting XML file into multiple at given tags

后端 未结 3 1553
孤城傲影
孤城傲影 2021-01-15 11:24

I want to split a XML file into multiple files. My workstation is very limited to Eclipse Mars with Xalan 2.7.1.

I can also use Python, but never used it before.

3条回答
  •  滥情空心
    2021-01-15 11:58

    There's an excellent tool http://xmlstar.sourceforge.net/docs.php which can do a lot with xml (however it's not pythonic).

    Given you have a 1.xml file with the data as above. And you need to split it to separate files with names NNN.xml with element /root/row.

    Just call in shell:

        $ for ((i=1; i<=`xmlstarlet sel -t -v 'count(/root/row)'  1.xml`; i++)); do \
              echo '' > NAME.xml;
              NAME=$(xmlstarlet sel -t -m '/root/row[position()='$i']' -v './NAME' 1.xml); \
              xmlstarlet sel -t -m '/root/row[position()='$i']' -c . -n 1.xml >> $NAME.xml; \
              echo '' >> NAME.xml
           done
    

    Now you have a bunch of xml files like Joe.xml

提交回复
热议问题