Tuesday, 3 January 2012

Approaches to XML - Part 1 - XML is not a String...

XML has been around a long time: from memory, I’d guess that it’s about 12-14 years old by now; it’s a mature product with, in computing terms, a long history. So, is there anything new that I could possibly add to this subject? How you approach XML is really down to you and your situation, with the emphasis being on situation. At one extreme, you may be involved in a cosy ‘in-house’ project, where one in-house system has to talk to another. At the other extreme, you’re in a situation where you’re handed an XML schema that will, one day, comprise the conversation between your system and some other system in a far away country written by a different set of developers.

As usual, to demonstrate different approaches to XML, I’m going to take a fully contrived and outrageous scenario and in this scenario, you’re working for Pete’s Perfect Pizza1, which is a little take away shop in the high street, but Pete has big ideas and the first thing he wants to do is to automatically send orders from the front desk to the kitchen and he asks you to write some code. Your big idea is to use XML for this and you hastily scribble down your idea on a scrap of paper and agree it with Pete...


<?xml version="1.0" encoding="UTF-8"?>
<pizza>
    <name>Capricciosa</name>
    <base>thin</base>
    <quantity>2</quantity>
</pizza>

Pete’s is a very small company and you end up coding both the front desk message builder and the kitchen XML parser code. It’s your first attempt at XML and for the kitchen parser code you come up with:

public class OrderPizza {

 
private String pizzaName;
 
private String base;
 
private String quantity;

 
public void order(String xmlOrder) {

   
pizzaName = xmlOrder.substring(57, xmlOrder.indexOf("</", 58));
   
int index = xmlOrder.indexOf("<base>", 58);
   
int index2 = xmlOrder.indexOf("</", index);
    base = xmlOrder.substring
(index + 6, index2);
    index = xmlOrder.indexOf
("<quantity>", index2);
    index2 = xmlOrder.indexOf
("</", index);
    quantity = xmlOrder.substring
(index + 10, index2);
 
}

 
public String getPizzaName() {
   
return pizzaName;
 
}

 
public String getBase() {
   
return base;
 
}

 
public String getQuantity() {
   
return quantity;
 
}
}

...which works really well in your unit tests:

public class OrderPizzaTest {

 
private static final String ORDER_XML = //
 
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + //
     
"<pizza>\n" + //
     
"    <name>Capricciosa</name>\n" + //
     
"    <base>thin</base>\n" + //
     
"    <quantity>2</quantity>\n" + //
     
"</pizza>\n";

 
private OrderPizza instance;

 
@Before
 
public void setUp() throws Exception {

   
instance = new OrderPizza();
 
}

 
@Test
 
public void readOrderFromXML() {

   
instance.order(ORDER_XML);

    assertEquals
("Capricciosa", instance.getPizzaName());
    assertEquals
("thin", instance.getBase());
    assertEquals
("2", instance.getQuantity());
 
}
}

It goes live and all is well....

Time goes on and Pete’s Perfect Pizza begins to expand. The front desk application needs a few tweaks and Pete gets in another developer. The first thing the new developer does is decide that it would be more efficient to get rid of the white-space characters in the XML message and sends the kitchen code something like this:

<?xml version="1.0" encoding="UTF-8"?><pizza><name>Capricciosa</name><base>thin</base><quantity>2</quantity></pizza>

The kitchen code falls over in an instant and you realise that you made the first and most fundamental mistake when dealing with XML, which is XML is NOT A STRING. XML is an object oriented document model that can be displayed using a string representation - a lesson that a lot of people learn the hard way. And it’s not just the pretty printing of the string that can trip you up, there’s a whole bunch of document formatting shenanigans that go on. Take for example www.sitemaps.org. This website defines an XML schema that is supported by Google, Yahoo!, and Microsoft and allows webmasters to inform search engines about pages on their sites that are available for crawling by using an XML document. Given the use of namespaces in the schema, a web site can come return something like this when asked for its sitemap:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
   <url>
      <loc>http://www.example.com/</loc>
      <lastmod>2005-01-01</lastmod>
      <changefreq>monthly</changefreq>
      <priority>0.8</priority>
   </url>
   <url>
      <loc>http://www.example.com/page1/</loc>
      <lastmod>2006-01-02</lastmod>
      <changefreq>weekly</changefreq>
      <priority>0.8</priority>
   </url>
</urlset> 

...but it could equally return this:

<?xml version="1.0" encoding="UTF-8"?>
<sm:urlset xmlns:sm="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
   <sm:url>
      <sm:loc>http://www.example.com/</sm:loc>
      <sm:lastmod>2005-01-01</sm:lastmod>
      <sm:changefreq>monthly</sm:changefreq>
      <sm:priority>0.8</sm:priority>
   </sm:url>
   <sm:url>
      <sm:loc>http://www.example.com/page1/</sm:loc>
      <sm:lastmod>2006-01-02</sm:lastmod>
      <sm:changefreq>weekly</sm:changefreq>
      <sm:priority>0.8</sm:priority>
   </sm:url>
</sm:urlset>

...or this:

<?xml version="1.0" encoding="UTF-8"?>
<sitemap:urlset xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
   <sitemap:url>
      <sitemap:loc>http://www.example.com/</sitemap:loc>
      <sitemap:lastmod>2005-01-01</sitemap:lastmod>
      <sitemap:changefreq>monthly</sitemap:changefreq>
      <sitemap:priority>0.8</sitemap:priority>
   </sitemap:url>
   <sitemap:url>
      <sitemap:loc>http://www.example.com/page1/</sitemap:loc>
      <sitemap:lastmod>2006-01-02</sitemap:lastmod>
      <sitemap:changefreq>weekly</sitemap:changefreq>
      <sitemap:priority>0.8</sitemap:priority>
   </sitemap:url>
</sitemap:urlset> 

...and they’re all valid, equivalent and contain the same information. It’s only their string representation that differs and taken together, they underline the first rule of XML, which is XML is NOT A STRING.

Back to Pete’s Perfect Pizza and you’re wondering how you can fix your code when Pete comes in and asks for an enhancement. He wants you to improve the system so that your XML document can order more than one pizza at a time. You realise that string manipulation is out and come across SAX parsing on the Internet but, more on that another time.


1Using Google, you’ll discover that there are four pages worth of Pete’s Perfect Pizza from all over the globe. This story is fictitious and any resemblance to any of them is purely coincidence.
2From reading the orders XML you may guess that my favourite pizza is Capricciosa and the best ever is available from Pizza Margherita



Other blogs in this series...
  1. XML is not a String
  2. What about Sax
  3. JAXB
  4. XMLBeans

The source code is available from GitHub at:

git://github.com/roghughe/captaindebug.git


No comments: