Creating and Using XML Variables

Using the ECMAScript extensions included with WebLogic Workshop, creating a variable for handling XML is as simple as the following:

    var myXML = <employees>
            <employee id="111111111">
                <firstname>John</firstname>
                <lastname>Walton</lastname>
                <age>25</age>
            </employee>
            <employee id="222222222">
                <firstname>Sue</firstname>
                <lastname>Day</lastname>
                <age>32</age>
            </employee>
        </employees>;

When you create a variable and assign it a value that begins with a < symbol, the value is automatically interpreted as XML.

In other words, it is not necessary to create an instance of an XML parser or to create a document instance conforming to a Document Object Model (DOM). These extra steps, common to other XML programming models, are unnecessary. Instead, you create a variable either by assigning a literal XML value (as above) or by creating an XML variable using the new operator:

    var myXMLVariable = new XML("<employee id='111111111'><firstname>John</firstname></employee>");

You can use a variable containing XML to access and manipulate the XML as you would other types that contain hierarchical or listed data, such as collections or arrays. For example, the following code uses the myXML variable to change John's name to Roger:

    /* Change the <firstname> value of the first employee to Roger. */
    myXML.employees.employee[0].firstname = "Roger";

# Data Types for XML

Under the covers, there are actually two new data types at work: XML and XMLList. As with many other types in ECMAScript, you do not need to explicitly declare these types. But code you write will create and manipulate them implicitly depending on the code.

## XML Data Type

In general, an XML variable represents XML that has a root, such as the preceding myXML variable. The root element of this variable is the <employees> element — it contains the rest of the XML. Querying the myXML variable for one of the employees also returns an XML variable, as in the following example:

    /* Create an XML variable from the first employee element. */
    var anEmployee = myXML.employees.employee[0];

The content of the resulting anEmployee variable looks like this:

    <employee id="111111111">
        <firstname>John</firstname>
        <lastname>Walton</lastname>
        <age>25</age>
    </employee>

## XMLList Data Type

An XMLList variable, on the other hand, generally represents XML that has no actual root. Without the <employees> tags, for example, myXML would be an XMLList. If you wanted to create an XMLList variable, you could do it as follows:

    var myXMLList = <>
            <employee id="111111111">
                <firstname>John</firstname>
                <lastname>Walton</lastname>
                <age>25</age>
            </employee>
            <employee id="222222222">
                <firstname>Sue</firstname>
                <lastname>Day</lastname>
                <age>32</age>
            </employee>
    </>;

Note that the value assigned to the variable here doesn't have an actual root, but an anonymous root expressed as <> and </>. Because it has no actual root, the XML in myXMLList is known as an XML fragment.

Just as a query can return an XML type, a query for a particular piece of XML can also return an XMLList. By not specifying an index number for an <employee>, the following line of code returns all of the <employee> elements:

    var listOfEmployees = myXML.employees.employee;

The resulting XML looks like the following (note the absence of a single containing element):

    <employee id="111111111">
        <firstname>John</firstname>
        <lastname>Walton</lastname>
        <age>25</age>
    </employee>
    <employee id="222222222">
        <firstname>Sue</firstname>
        <lastname>Day</lastname>
        <age>32</age>
    </employee>

One of the most useful aspects of the XMLList type is the ability to access its contents iteratively. The XMLList type is returned when you query for particular parts of the XML using the . (dot) operator. For example, using the myXML variable in the first example, you could write code such as the following:

    /* Create an XMLList containing the <employee> elements. */
    var listOfEmployees = myXML.employees.employee;
    for (var e in listOfEmployees) {
        // Create a new variable with the content of the <age> element converted to a number.
        var age = new Number(e.age);
        // Raise everyone's age by 1 year.
        age += 1;
        // Assign the new age value to the content of the element.
        e.age = age;
    }

# Notes About Using the Data Types for XML

- The variable name corresponds to the container for the XML, not to the XML's root.

When accessing XML through a variable, remember that the highest-level element in the XML itself is just beneath the variable in the hierarchy. Compare the following two lines of code:

    /* This is how it's done! */
    var johnFirstName = myXML.employees.employee.firstname;
    /* This won't return the correct result! */
    var johnFirstName = myXML.employee.firstname;

- The script interpreter will read PIs and comments, but does not preserve them in the XML value — they are silently ignored.

- The script interpreter does not support literal XML values that do not contain at least one element.

Related Topics

Accessing Element Children With the . Operator

Using Script Functions From XML Maps