# Introduction to XML

Extensible Markup Language (XML) is a way to describe structured data in text. Web services exchange messages in XML. Of the many roles XML plays in web services, perhaps the most visible to you as a developer is providing the format for the messages a web service sends and receives. A web service receives method calls, returns values, and sends notifications as XML messages. As you test and debug your service, for example, you might construct XML documents that represent sample messages your service could receive, then pass these documents to your service and view the results.

For more information about the XML standard, see Extensible Markup Language (XML) 1.0 (Second Edition) at the web site of the W3C. The W3C also provides a page with useful links to more information about XML at Extensible Markup Language (XML).

Note: WebLogic Workshop provides easy programmatic access to XML through the XMLBeans API. For more information, see Getting Started with XMLBeans.

## Anatomy of an XML Document

Let's start with a sample XML document. Imagine that you are writing a web service to return information about employees from a database. Based on first and last name values sent to your service, your code searches the employee database and returns contact information for any matches. The following example is XML that might be returned by your service. Even if you have no experience with XML, you can probably see structure in the example.

    <!-- Information about the employee record(s) returned from search. -->
    <employees>
        <employee>
            <id ssn="123-45-6789"/>
            <name>
                <first_name>Gladys</first_name>
                <last_name>Cravits</last_name>
            </name>
            <title>Busybody</title>
            <address location="home">
                <street>1313 Mockingbird Lane</street>
                <city>Anytown</city>
                <state>IL</state>
                <zip>12345</zip>
            </address>
        </employee>
    </employees>

### Elements

In XML, text bracketed by < and > symbols is known as an element. Elements in this example are delimited by tags such as <employees> and </employees>, <name> and </name>, and so on. Note that most elements in this example have a start tag (beginning with <) and an end tag (beginning with </). Because it has no content the <id> element ends with a /> symbol. XML rules also allow empty elements to be expressed with start and end tags, like this: <id ssn="123-45-6789"></id>.

### Root Elements

Every XML document has a root element that has no parent and contains the other elements. In this example, the <employees> element is the root. The name of the root element is generally based on the context of the document. For example, if your service is designed to return merely an address—an <address> element and its contents—then the root element is likely to be <address>.

### Parent/Child Relationships

Elements that contain other elements are said to be parent elements; the elements that parent elements contain are known as child elements. In this example, <employees>, <employee>, <name>, and <address> are parent elements. Elements they contain—including <employee>, <id>, <name>, and <city>—are children. (Note that an element can be both a parent and a child.)

Note: This example uses indentation to accentuate the hierarchical relationship, but indentation is not necessary.

### Attributes

Attributes are name/value pairs attached to an element (and appearing in its start tag), and intended to describe the element. The <id> element in this example has an ssn attribute whose value is 123-45-6789.

### Content

The content in XML is the text between element tags. Content and attribute values represent the data described by an XML document. An element can also be empty. In the example above, "Gladys" is the content (or value) of the <first_name> element.

### Comments

You can add comments to XML just as you would with HTML, Java, or other languages. In XML, comments are bracketed with <!-- and --> symbols (the same symbols used in HTML). The first line of the example above is a comment.

### Namespaces

A namespace in XML serves to define the scope in which tag names should be unique. This is important because XML's wide use and textual nature make it likely that you will see occasions where element names with different meanings occur in the same document. For example, namespaces and prefixes are used in XML maps, where a prefix differentiates the tags that are needed for mapping from those associated with the mapped method's XML message.

Related Topics

Getting Started with XMLBeans

Introduction to XQuery Maps