File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Attributes and elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Attributes and elements" Watch "Attributes and elements" New topic
Author

Attributes and elements

Poonam Kadu
Ranch Hand

Joined: Jul 21, 2006
Posts: 49
Hi Friends ,
I have the following doubt:

<notes>
<note date="23/09/07">
<to>jane</to>
<from>Anne</from>
</note>
</notes>

in the above note element I've date as an attribute.
But date can also be defined as a child element like <to>and <from> of note

What difference does it make having date declared as a child element or as an attribute of note element.

cheers ,
Poonam K.
Biswajit Paria
Ranch Hand

Joined: Feb 02, 2003
Posts: 46
<notes>
<note date="23/09/07">
<to>jane</to>
<from>Anne</from>
</note>
</notes>

in the above note element I've date as an attribute.
But date can also be defined as a child element like <to>and <from> of note

What difference does it make having date declared as a child element or as an attribute of note element.

Then date would be an attribute for note and would be child for notes
It can be possible if you define your DTD/Schema accordingly...
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12755
    
    5
What difference does it make having date declared as a child element or as an attribute of note element.


To some extent this is a author style thing with no hard and fast rule. I have seen plenty of XML extremes with no attributes or all attributes. As you gain more experience with the various XML toolkits I'm sure you will develop your own style.

Mainly you should be aware of the limitations on the character content in attributes before you try to use nothing but attributes.

Bill
Dhanasekar Thangavelu
Greenhorn

Joined: Jul 05, 2007
Posts: 3
Please find the below limitations of using the attributes rather elements

attributes cannot contain multiple values (child elements can)
attributes are not easily expandable (for future changes)
attributes cannot describe structures (child elements can)
attributes are more difficult to manipulate by program code
attribute values are not easy to test against a Document Type Definition (DTD) - which is used to define the legal elements of an XML document


-Dhanasekar.T
Manish Shah
Greenhorn

Joined: Jun 09, 2002
Posts: 22
Adding as a node is more preferable as it is easily readable.
You can consider putting as an attribute if that attribute is short & constant.
What if in the future there is a change expected in date to specify date format information ..?
In this case you cannot go on adding attributes.
It is rather better to have date as node so that it can be open for having child attributes.


SCBCD5, SCWCD, SCJP, MCP
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Attributes and elements
 
Similar Threads
element content
Doubt on XML Schema validity
how comws varaible in the tag
xsd:all element
DTD