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";
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.
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>
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; }
As of WebLogic Platform 8.1, an XML variable represents the root of the XML assigned to it. Compare the following two lines of code, which could be used to access the XML above.
/* This is how it's done by default. */ var johnFirstName = myXML.employee.firstname; /* * You can use an environment variable to specify that access * should work this way. */ var johnFirstName = myXML.employees.employee.firstname;
You declare a namespace variable when you want to refer to a namespace in code that accesses XML. For example, you can declare a namespace variable in this way:
var myNS = new Namespace("http://openuri.org/bea/examples");
This code associates the myNS variable with the namespace URI. Code to access XML belonging to this namespace could look like this:
var firstName = myXml.myNS::employee.myNS::firstname;
For more information, see Filtering By Namespace. The Namespace class also provides two methods you might find useful, as described in Functions for Manipulating XML.
When your code retrieve an attribute, the type returned is an XMLAttribute. For the most part, you use the XMLAttribute type implicitly, rather than by declaring it.
However, the XMLAttribute class provides several methods that you might find useful, as described in Functions for Manipulating XML.