Click here to Skip to main content
14,641,662 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi!
I’m trying to make a XML file, but the now the problem comes. I can now see that some are using Elements like: <name>John</name> and someone likes to do it this way between quotation marks <name =”John”>.
What is the different between this and that way of doing these two coding and what are they called?
Is one way better than the other?

[edit] Unnecessary code tags removed - nelek [/edit]
Posted
Updated 19-Apr-12 11:43am
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

In XML <name =”John”> is not syntactically correct. It should be something like this
<person name="John"></person>

Then person is called element and name is called the attribute of the element person. As said in the question the other way of doing this is
<person>
    <name>John</name>
</person>

Here, name is the child element of the element person.
Both, give the information. There are no hard and fast rules regarding when to use as an attribute and when to use as child element. But, as a general the attribute can be used when the information concerned describes something about the element concerned, like an adjective describes about a noun. On the other hand an element can be used when the information concerned is like data.
The other difference is that if it is represented as an element then it can nest other elements and also can have attributes. But, if it is given as an attribute then it cannot have child elements and other attributes.
An explanation is given here
http://www.w3schools.com/dtd/dtd_el_vs_attr.asp[^]
http://msdn.microsoft.com/en-us/library/7f0tkwcx(v=vs.80).aspx[^]
I think it may be helpful.
   
v3
Comments
meaeg 19-Apr-12 21:00pm
   
I know this ”John” would make a syntax error sinse I wrote it in word. Thank you for the answer it was helpful.
VJ Reddy 19-Apr-12 21:08pm
   
If the answer is helpful, then you can consider to vote and accept the solution.
Thank you for the response :)
Sergey Alexandrovich Kryukov 22-Apr-12 21:00pm
   
All correct, my 5 (in my edition, I only removed invalid '.' after </pre>)
--SA
VJ Reddy 22-Apr-12 21:08pm
   
Thank you very much, SA.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100