3

I have a very basic question on XML design. I have XML which is used for generating UI. Its format is somewhat like below:

<Input>
  <Label>Enter Machine Name</Label>
  <ToolTip> Please enter the correct machine name</ToolTip>
  <Type>TextField</Type>
</Input>

My question is whether writing the XML in above format is better or in the below format for better processing time results.

<Input Label="Enter Machine Name" 
       ToolTip="Please enter machine name"
       Type="TextField">

Which type of XML will be parsed in lesser time?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Katiyman
  • 827
  • 2
  • 12
  • 32

2 Answers2

5

While attributes will take a bit less space, which is an advantage for performance, and a SAX parse will generate less events for the attribute-based design, which can save a minute amount of time, any attribute vs element performance difference is simply not going to matter in the vast majority of cases.

As with most a priori performance worries, it's better to build, measure, and adjust, provided your basic design is sound. The soundness of your basic design is not going to be driven by elements vs attributes.

You should try to use an industry standard schema for your XML and follow what they do regarding elements vs attributes. If you're designing on your own from scratch, see XML Element vs XML Attribute and the other sibling answers there.

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • I'm really curious about your statement that "a SAX parse will generate less events for the attribute-based design". Can you please help me understand how this is the case? – Dmytro Aug 09 '16 at 21:59
  • @Dmitry: Implementations can vary, but SAX will commonly generate 0 or 1 events per attribute and 2 events per element. (Attributes can be retrieved from the start element event or via a separate event; elements typically have a start and an end event.) *But I really wanted to emphasis in this answer that such differences nearly never matter (and are implementation dependent anyway).* There are more important design considerations in choosing between elements and attributes, and there are many other performance areas that'll overwhelm any differences between attributes and elements. – kjhughes Aug 09 '16 at 22:19
  • so SAX parser would note the attribute name and attribute value in one event(startElement), and this event is the first relevant event being sent, and self closing avoids checking for end tag; While to get the element whose innerHTML denote the value and tag name denote key, it needs to finish attribute parsing, then the startElement of the child to find the key, and only then get the value, and waste time afterwards(internally) with a useless endElement event, and another one after that? – Dmytro Aug 09 '16 at 23:43
-3

You should use elements since it's data, see:
- XML best practices: attributes vs additional elements
- http://www.w3schools.com/dtd/dtd_el_vs_attr.asp

Elements and attributes are both common in XML, so expect that parsers are optimized to handle both well. I guess elements are found a bit faster, but I only looked at jQuery Sizzle results:
jQuery select by class VS select by attribute

Community
  • 1
  • 1
CodeManX
  • 11,159
  • 5
  • 49
  • 70