structure of xml document.

Hi. Just wanted some feedback on the structure of an xml document.
Here is a hypothetical example.

I have store inventory of items. Each item has a PLU and a name.
I want to build a shopping list built from elements in inventory.

<?xml version="1.0" encoding="UTF-8"?> <PLUDictionary> <Item name="Apple" PLU="111"></Item> <Item name="Orange" PLU="112"></Item> <Item name="Banana" PLU="113"></Item> <Item name="Pine Apple" PLU="114"></Item> </PLUDictionary> <ShoppingList> <Item name="Apple"></Item> <item name="Banana"></item> </ShoppingList>

Does this seem like a sensible representation of my data?

I’m curious about representing the inventory as a dictionary as one would think that there would be a key and a value.
I suspect that PLU is the Key and name is the value.

So should my Shopping list be a list of items and if so should each element have both the name and the plu?

I know I can name my elements anything I like but is it sensible to assume that if I want to implement the object in code that it’s name reflect the class that will be used to manipulate it and its members to correspond to the class… i.e. key and value.

You’re using attributes for the values whereas the more typical approach might be to use children. For example:

<?xml version="1.0" encoding="UTF-8"?>
<PLUDictionary>
   <item>
      <key>Apple</key>
      <PLU>111</PLU>
   </item
</PLUDictionary>

Having said that, have you considered using JSON instead of XML (if you have a choice)? A JSONItem can work as either an array or as a case-sensitive Dictionary, so it seems like it might be a better fit for what you’re trying to do. A similar JSON would look like this:

{
  "PLUDictionary" : {
    "Apple" : "111",
    "Orange" : "112"
    },
  "ShoppingList" : [
    "Apple",
    "Banana"
  ]
}

The curly brackets signify a Dictionary-type JSON object, and the square brackets means an array. You can add as many children as you’d like, of course, I just simplified it.

XML is a format.
The only rules are about general structure
There is no rule to use children or attributes as “fields”. You have to define you “language”

Nodes can contains other nodes.
So a node contents (value) can be an elementNode, a TextNode, a CommentNode etc.
You can set as many attributes to a node as long this has sense with your “language”, that’s to say it depends on how you read and write the structure.

The only rule that’s broken by Brian’s xml doc is that there is no a unique document node: the document has two nodes.

JSON is another format.
It’s more compact (it don’t repeat the node name to close it) and it’s more clear about it’s structure so any parser can read it without knowing anything about your “rules”

Said that, you can use XML or JSON. if you have to transform your data to something else, XML is a better choose since with XSLT you can transform easily to anything. If you have to send it to a browser or a server, JSON is a better choose since it’s shorter.

Apple’s PLISTS went the way of child nodes, actually siblings, for key / value pairs and quite honestly while its XML its a crappy structure.

I’d design the XML depending on whether its for machines reading or human readability.

If this is to send to another program attributes are dead easy to work with and require a less work than child nodes.

If you want it to be readable by humans you might use child nodes.

There are some good XML design documents on the web
http://www.ibm.com/developerworks/library/x-tipflaw/
http://www.ibm.com/developerworks/library/x-eleatt/
http://www.xfront.com/BestPracticesHomepage.html

Thanks for the tips.

  1. No root object
  2. Children
  3. json

This is meant to be a quick config file.