tidy xml

I found myself needing a simple way to format some xml.  The xml I had was all shoved onto one line which is pretty impossible to read.

I thought, nxml-mode should be able to do this, but checking the function names provided with nxml didn’tyield anything useful as far as I could tell.  So I thought tidy should be able to do this.

For example, given this input xml, from a maven pom.xml where I purposefully de-formatted the xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent><groupId>dmb</groupId><artifactId>dev</artifactId><version>{dev.version}</version></parent><modelVersion>4.0.0</modelVersion><groupId>dmb</groupId><artifactId>misc</artifactId>
<packaging>jar</packaging><name>miscellaneous</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>org.jboss.cache</groupId><artifactId>jbosscache-core</artifactId><version>2.1.0.CR4</version></dependency><dependency><groupId>joda-time</groupId><artifactId>joda-time</artifactId><version>1.5.2</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>{spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>{spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>{spring.version}</version></dependency><dependency><groupId>commons-cli</groupId><artifactId>commons-cli</artifactId><version>1.1</version></dependency><dependency><groupId>google.com</groupId><artifactId>collections</artifactId><version>20071022</version><scope>compile</scope></dependency><dependency><groupId>org.apache.lucene</groupId><artifactId>lucene-core</artifactId><version>2.0.0</version></dependency><dependency><groupId>com.sleepycat</groupId><artifactId>je</artifactId><version>3.3.62</version></dependency></dependencies><build>
<plugins>
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><executions><execution>
<phase>package</phase><goals><goal>shade</goal></goals><configuration><artifactSet><excludes><exclude>classworlds:classworlds</exclude><exclude>junit:junit</exclude><exclude>jmock:jmock</exclude><exclude>xml-apis:xml-apis</exclude><exclude>javax.jms:jms</exclude><exclude>org.testng:testng</exclude></excludes></artifactSet>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer"/></transformers></configuration></execution></executions></plugin>
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><configuration><archive><manifestEntries><mainClass>dmb.Main</mainClass></manifestEntries></archive></configuration></plugin></plugins></build>
<profiles>
<profile><id>unit-test</id><activation><activeByDefault>false</activeByDefault></activation><build>
<plugins>
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.4</version><configuration><systemProperties><systemProperty><name>base.directory</name><value>xbasedir</value></systemProperty></systemProperties><useFile>false</useFile>
<trimStackTrace>false</trimStackTrace><useSystemClassLoader>true</useSystemClassLoader><groups>unit</groups></configuration></plugin></plugins></build></profile>
<profile><id>system-test</id><activation><activeByDefault>false</activeByDefault></activation><build>
<plugins>
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.4</version><configuration><skip>false</skip><systemProperties><systemProperty><name>base.directory</name><value>xbasedir</value></systemProperty></systemProperties><useFile>false</useFile>
<trimStackTrace>false</trimStackTrace><useSystemClassLoader>true</useSystemClassLoader><groups>system</groups></configuration></plugin></plugins></build></profile>
<profile><id>default-tests</id><activation><activeByDefault>true</activeByDefault></activation><build>
<plugins>
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.4</version><configuration><skip>false</skip><systemProperties><systemProperty><name>base.directory</name><value>xbasedir</value></systemProperty></systemProperties><useFile>false</useFile>
<trimStackTrace>false</trimStackTrace><useSystemClassLoader>true</useSystemClassLoader></configuration></plugin></plugins></build></profile></profiles><repositories><repository><id>repository.jboss.org</id><url>http://repository.jboss.org/maven2</url></repository><repository><id>snapshots.jboss.org</id><url>http://snapshots.jboss.org/maven2</url></repository><repository><id>java.net</id><url>http://download.java.net/maven/1</url><layout>legacy</layout></repository><repository><releases><enabled>true</enabled><updatePolicy>always</updatePolicy><checksumPolicy>warn</checksumPolicy></releases><snapshots><enabled>true</enabled><updatePolicy>never</updatePolicy><checksumPolicy>fail</checksumPolicy></snapshots><id>oracleReleases</id><name>Oracle Released Java Packages</name><url>http://download.oracle.com/maven</url><layout>default</layout></repository></repositories>
<properties><spring.version>2.5.1</spring.version></properties></project>

I ran tidy like this:


$ tidy -q -i -xml << EOF
</repositories>
<properties><spring.version>2.5.1</spring.version></properties></project>
> EOF
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>dmb</groupId>
<artifactId>dev</artifactId>
<version>{dev.version}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>dmb</groupId>
<artifactId>misc</artifactId>
<packaging>jar</packaging>
<name>miscellaneous</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.jboss.cache</groupId>
<artifactId>jbosscache-core</artifactId>
<version>2.1.0.CR4</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>{spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>{spring.version}</version>
</dependency>

This works even better directly from within emacs by running shell-command-on-region(M-|), tidy -q -i -xml :

About these ads

About this entry