Over a million developers have joined DZone.

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

DZone's Guide to

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

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

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"?>

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" + //

  private OrderPizza instance;

  public void setUp() throws Exception {

    instance = new OrderPizza();

  public void readOrderFromXML() {


    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">

...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"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

...or this:

<?xml version="1.0" encoding="UTF-8"?>
<sitemap:urlset xmlns:sitemap="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">

...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


From http://www.captaindebug.com/2012/01/approaches-to-xml-part-1-xml-is-not.html

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}