Thursday, May 4, 2017

Check if an XML file is well-formed in Linux

Do you want to know if a particular XML file is well-formed or malformed in Linux? Consider using xmllint.

If the return result is 0, then the XML file is well-formed:
oracle@soahost1:/tmp> xmllint --noout config.xml; echo $? 
0

If the return result is 1, then the XML is malformed:
oracle@soahost1:/tmp> xmllint --noout config.xml; echo $? 
config.xml:7: parser error : Opening and ending tag mismatch: domain line 2 and configuration-property 
  </configuration-property> 
                           ^ 
config.xml:8: parser error : Extra content at the end of the document 
  <domain-version>12.2.1.1.0</domain-version> 
  ^ 
1

If you want to recursively check all XML files, this command will do it for you:

for i in `find . -name "*.xml"`; do echo $i; xmllint --noout $i; echo $?; done




No comments: