[리눅스] XML 노드 편집

2023. 4. 6. 02:17리눅스 실제 사용 팁

작성일 : 2019. 1. 30. 15:52


xmlstarlet 유틸리티를 활용해서 XML 파일에 특정 노드를 삽입하거나 삭제할 수 있다.

 

xmlstarlet ed --subnode "root/customer" --type elem -n name -v "Testing Guy" WeightLossResponse_Low.xml > WeightLossResponse_Low.xml.out

 

(※ 유의할 점 : 리다이렉션 연산자를 써서 내보내는 파일명은 원본 파일과 다르게 해야 함! 그렇게 하지 않으면 원본 파일의 내용이 날아간다.

"0 바이트로")

 

원본 파일 내용 :

<root>

..

<customer>

<id>A00000000</id>

<!-- 이 안에 name 요소를 추가하는 명령이다 -->

</customer>

..

</root>

 

 

실행 결과 :

<root>

..

<customer>

<id>A00000000</id>

<name>Testing Guy</name>

</customer>

..

</root>

 

참고 문서 :

https://www.technomancy.org/xml/add-a-subnode-command-line-xmlstarlet/