# Monday, June 18, 2007

OK, let me post the XML that was given to me so people have a better idea of the problem with the XML file which I was talking about in the previous post and why it is wrong. I posted the original post 5 mins after getting the file from them so I was in the middle of work and couldn't post the chunk.. so.. here is what he sent me.

<ShopDetails>
     <ShopName>A Shop Somewhere</ShopName>
     <City>PJ</City>
     <Branch>1 Utama</Branch>
     <Products>
          <Name>Product A</Name>
          <Price>56.99</Price>
          <Description>This is a product</Description>
          <Name>Product B</Name>
          <Price>99.34</Price>
          <Description>This is another product</Description>
          <Name>Product C</Name>
          <Price>112.98</Price>
          <Description>This is yet another product</Description>
     </Products>
     <Branch>Ikea</Branch>
     <Products>
          <Name>Product A</Name>
          <Price>56.99</Price>
          <Description>This is a product</Description>
          <Name>Product B</Name>
          <Price>99.34</Price>
          <Description>This is another product</Description>
          <Name>Product C</Name>
          <Price>112.98</Price>
          <Description>This is yet another product</Description>
          <Name>Product D</Name>
          <Price>500.98</Price>
          <Description>Yup, here's another product</Description>
     </Products>
</ShopDetails>

So there's a sample of the XML that they gave me, can you see what's wrong with it? No? ok.. here.. try to answer the following questions while referring to the XML above.

  • How many products does the Ikea branch has?
  • How many branches does PJ have?
  • How much is Product C in the Ikea branch? How did you come up with the answer?

The main problem is that the data is not hierarchical (thank goodness for spell checking in Live writer!), if you wanted to know how many products there was in one node you'd have to count the number of occurances of a Name element inside the Products element that immediately follows a Branch element. If they gave this to me saying that it's a flat file and here are the processing instructions I'd grit my teeth and start writing a file processor, but they call this an XML file and thus they should organize their data in a way that benefits being represented in XML.

One of the ways this XML could have been written properly is.

<ShopDetails>
     <ShopName>A Shop Somewhere</ShopName>
     <City>    
        <CityName>PJ</CityName>
        <Branch>
          <BranchName>1 Utama</BranchName>
          <Products>
               <Product>
                  <Name>Product A</Name>
                  <Price>56.99</Price>
                  <Description>This is a product</Description>
               </Product>
               <Product>
                  <Name>Product B</Name>
                  <Price>77.99</Price>
                  <Description>This is another product</Description>
               </Product>
               <Product>
                  <Name>Product C</Name>
                  <Price>88.99</Price>
                  <Description>This is yet another product</Description>
               </Product>
           </Products>
          </Branch>
         <Branch>
          <BranchName>Ikea</BranchName>
          <Products>
               <Product>
                  <Name>Product A</Name>
                  <Price>56.99</Price>
                  <Description>This is a product</Description>
               </Product>
               <Product>
                  <Name>Product B</Name>
                  <Price>77.99</Price>
                  <Description>This is another product</Description>
               </Product>
           </Products>
          </Branch>
     </City>
</ShopDetails>

Now try to answer the questions above again (yes the values are different, I got lazy typing but the idea is there!!) Isn't this much better organized and give a better relation of the data contained in the file?


Note that you can Post As GUEST as well.
blog comments powered by Disqus
XML
Monday, June 18, 2007 3:44:38 PM (Malay Peninsula Standard Time, UTC+08:00)  #    Comments [1]  |  Related posts:
XML <> FLAT FILE