XML command line tools in Mac OS X (1)

xmllint

xmllint is a parser for XML files that allows you to check for the well-formed state of the input.

xmllint can re-format the output, taking one long line of unreadable input and indenting it in a more readable form:


diciu$ cat test.xml
<?xml version="1.0" encoding="utf-8"?>

<a> <m> <p>one, two, three</p> </m> <m> <p>three, four</p> </m> <m> <p>five</p> </m> </a>

cristi:~ diciu$ xmllint --format test.xml
<?xml version="1.0" encoding="utf-8"?>
<a>
<m>
<p>one, two, three</p>
</m>
<m>
<p>three, four</p>
</m>
<m>
<p>five</p>
</m>
</a>



In the Leopard versions, xmlllint and most of the other XML tools have a built in XPath browser implemented through a shell, allowing you to browse an XML's file hierarchy of nodes just like you would browse a filesystem:


diciu$ cat test.xml
<?xml version="1.0" encoding="utf-8"?>

<a>
<m>
<p>one, two, three</p>
</m>

<m>
<p>three, four</p>
</m>

<m>
<p>five</p>
</m>
</a>


The built in commands (viewable with the help command), include cat, cd, ls, pwd that work in similar ways to their Unix cousins:

diciu$ xmllint --shell test.xml
/ > cat a
-------
<a>
<m>
<p>one, two, three</p>
</m>

<m>
<p>three, four</p>
</m>

<m>
<p>five</p>
</m>
</a>
/ > cd a
a > pwd
/a
a > cd m[position()=1]
m > pwd
/a/m[1]
m > ls
t-- 2
--- 1 p
t-- 1