Using Aliases to refactor XML
Posted by andy in : Agile, Refactoring, Software on February 23, 2004I have been playing with XStream for converting java classes into xml.
We can convert a class such as this:
Error: Could not open NotVeryUsefulBean.java
into xml that looks like this.
Error: Could not open NotVeryUsefulBean1.xml
The xml is nice and clean. Like most implementations, XStream embeds the name of the class in the xml. This can cause problems when you rename a class, or move it to a different package. The xml could be on a customers’ machine, well away from my refactoring IDE. I have been bitten by this lately.
Then I realised that XStream has aliases. Joe added aliases to make it easier to read the xml. You have to map an alias to a class in your code. Here is an example of using a “MyAlias” for the “NotVeryUsefulBean” class.
Error: Could not open XstreamDemoUsingAnAlias.java
And the xml now looks like this
Error: Could not open NotVeryUsefulBean2.xml
Not a class name in sight. When I rename classes and move them into different packages, the refactoring tool will update the alias without breaking the xml. Nice!

