University of Cincinnati logo and link  
More about XML Elements
 
  UC ingot The top-level XML element is called the root element.  It can contain other elements.
  • Elements can contain child elements, text, or both.  But it's customary to contain either one or the other, not both.
    • A child element.
        <security-role-mapping>
         <group name="admins" />
        </security-role-mapping>
    • Text.

    •         <url-pattern>
                  /servletToJsp
              </url-pattern>
    • A mix of each.  Note that there are elements with child elements, and elements with text. This is perfectly fine.  What you don't see are elements with both text and children in the same element.

    •     <servlet-mapping>
              <servlet-name>
                  servletToJsp
              </servlet-name>
              <url-pattern>
                  /servletToJsp
              </url-pattern>
          </servlet-mapping>
  • An element can contain attributes.

  • <group name="admins" />
    • Which raises the question... when should you use attributes, and when should you use child elements instead?  Well, attributes are slightly easier and take up less space, but they are also less flexible.  Use attributes when you want to modify the definition of a value, not specify a different value. When in doubt, use elements.
    • I often see cases where developers use either one exclusively or another exclusively.  This is probably not optimal, but is perfectly fine as well.  Use whatever you want.
      • Note that the default web.xml file from Apache uses elements exclusively.
 Other Markup Instructions