1

I saw this XML:

<amount type="downpayment"></amount>

I'm wondering why not do it like this:

<amount>
       <type>downpayment</type>
</amount>

1.Is there any particular reason or meaning why it's written in approach 1 instead of 2?

2.how will those two XMLs be translated to POJO? what would be the difference.

3.any reserved word in XML that must not be used?

Java Developer
  • 95
  • 1
  • 1
  • 6

2 Answers2

1

In a lot of cases it can be clarifying to have attributes used for properties of an object, and child elements for contained objects.

One example would be in "document style" XML applications, such as XHTML. In:

<p class="warning"><a href="/helpAccountExpire">Your account has expired</a></p>

Then the relationship between p and a is that the former represents a document element that the latter is part of, while class gives more information about the nature of p and href about a.

This can also make sense in non-"document style" applications:

 <list presorted="true">
   <item>4</item><item>7</item><item>23</item>
 </list>

Here the list contains the numbers 4, 7 and 23 while the presorted attribute tells use the list is already sorted (and hence we can forgo the expense of sorting it ourselves if we need it sorted).

There are plenty of cases where one could reasonably use either, and personal preference will often have an influence upon an XML application design. Of course, anything that can't be done with a simple string or where there can be more than possible value (in your example in your question, amount can have only zero or one type in the first case, but could have any number in the second.

How it will be translated into a POJO depends on what is doing the translation, you'll have to specify.

As for reserved words, a name of either an element or an attribute can start with any character that matches:

":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]

and the rest of the name can be any character that matches that or also:

"-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]

Note that this contains a few Unicode non-characters, that while not banned by XML standard, can't be in XML since XML builds on Unicode, so they're banned in practice too.

There is a further rule that names beginning with xml in any capitalisation (XML, xMl etc) are reserved for use in core XML technologies like XML itself, XML namespaces, etc. (xml:space, xml:lang, xmlns and the xmlns: prefix are examples of this being used).

(This is in XML1.0, XML1.1 has slightly different rules that clarify a few strange cases, but is mostly only needed by people who really need those clarifying rules).

An XML namespace-using application further restricts the above in that there can be only one colon (:) in a name, in which case the part to the left is the namespace-prefix identifying the namespace used (or the default namespace if none is present) and the bit to the right (or the whole thing if in the default namespaec) is the local name. By implication, this bans starting a name with a colon.

Even if you aren't using namespaces, its a good idea to follow those restrictions so that you will be compatible with technologies that do.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
0

The first one the XML Attribute and second one is XML Child Element. Following are some links which talks about their comparison:

On w3schools.com

On IBM developerworks

On Stackoverflow

Community
  • 1
  • 1
Kuldeep Jain
  • 8,409
  • 8
  • 48
  • 73