How would you design an XML file to store key-value pairs?

R

Ramon F Herrera

(newbie alert)

I have a bunch of key-value pairs (both of which with spaces) and have
been trying different ways to encode them in XML.

For instance,

"Name of Client" => "John Smith"

I am not posting my versions, intentionally. Would like to see yours
first. :)

TIA!

-Ramon
 
J

Joe Kesselman

That's essentially the standard "would you rather use attributes or
children values" question, since you don't care about the ordering of
the pairs and both are simple strings.

If you think either will eventually want to be divided into
sub-structures, or if you think that other values you add later may want
to consider their order relative to these meaningful, child elements are
probably the way to go. If not, attributes or child text may be a trifle
more human-readable, and are a trifle more compact in the XML text form.

It really boils down to picking the one that reflects how you want code
developers to think about the data. All of these are entirely reasonable:

<pair key="string1" value="string2"/>
<pair><key>string1</key><value>string2</value></pair>
<pair key="string1"><value>string2</value></pair>
<pair key="string1">string2</pair>

(The fact that you need to allow spaces eliminates the option of having
the key be the element name rather than "pair", which I'd otherwise
include in this list.)

Note that this is exactly the same set of decisions you have to make
when designing data structures in a programming languages. Is a given
value a named data field of the object or struct, or is it contained in
another object or struct owned by this one? Or, for that matter, is it
stored somewhere else entirely and just pointed to? All are defensible
decisions, depending on how you want to model the data now, how much you
want that model to agree with other models, and what your plans are for
the future.

--
Joe Kesselman,
http://www.love-song-productions.com/people/keshlam/index.html

{} ASCII Ribbon Campaign | "may'ron DaroQbe'chugh vaj bIrIQbej" --
/\ Stamp out HTML mail! | "Put down the squeezebox & nobody gets hurt."
 
J

Joe Kesselman

Note that if the keys are unique, there *may* be an advantage to making
them attributes and declaring them in the DTD or schema as ID
attributes. Some systems may build ID lookup tables to expedite
retrieving things by ID. (On the other hand, others may build lookup
tables for other things too; depends on how much they know about what
you will want to do and how clever they are and how much time and space
they can afford to spend on speculative/cached computation.)
 
C

Cambridge Ray

That's essentially the standard "would you rather use attributes or
children values" question, since you don't care about the ordering of
the pairs and both are simple strings.

If you think either will eventually want to be divided into
sub-structures, or if you think that other values you add later may want
to consider their order relative to these meaningful, child elements are
probably the way to go. If not, attributes or child text may be a trifle
more human-readable, and are a trifle more compact in the XML text form.

It really boils down to picking the one that reflects how you want code
developers to think about the data. All of these are entirely reasonable:

<pair key="string1" value="string2"/>
<pair><key>string1</key><value>string2</value></pair>
<pair key="string1"><value>string2</value></pair>
<pair key="string1">string2</pair>

(The fact that you need to allow spaces eliminates the option of having
the key be the element name rather than "pair", which I'd otherwise
include in this list.)

Note that this is exactly the same set of decisions you have to make
when designing data structures in a programming languages. Is a given
value a named data field of the object or struct, or is it contained in
another object or struct owned by this one? Or, for that matter, is it
stored somewhere else entirely and just pointed to? All are defensible
decisions, depending on how you want to model the data now, how much you
want that model to agree with other models, and what your plans are for
the future.

--
Joe Kesselman,http://www.love-song-productions.com/people/keshlam/index.html

{} ASCII Ribbon Campaign | "may'ron DaroQbe'chugh vaj bIrIQbej" --
/\ Stamp out HTML mail!  | "Put down the squeezebox & nobody gets hurt."

Thanks, Joe!

It was highly instructive.

-Ramon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top