# Using XMLBeans Types in Script Functions

You can use XMLBeans schema types, such as those generated by compiling schema, in an ECMAScript function.

You create an instance of an XMLBeans type by casting an XML value, as shown here:

    var poXml = (PurchaseOrder)<purchase-order xmlns="http://openuri.org/easypo">
        <customer>
            <name>Gladys Kravitz</name>
            <address>Anytown, PA</address>
        </customer>
        <date>2001-12-17T09:30:47-05:00</date>
        <line-item>
            <description>Burnham's Celestial Handbook, Vol 1</description>
            <per-unit-ounces>5</per-unit-ounces>
            <price>21.79</price>
            <quantity>2</quantity>
        </line-item>
        <shipper>
            <name>UPS</name>
            <per-ounce-rate>0.74</per-ounce-rate>
        </shipper>
    </purchase-order>;

## Field-Style Accessors

When you use XMLBeans types in script, the script interpreter supports field-style syntax for accessing child elements. This is an alternative to using the get and set methods exposed by these types. For example, a schema that describes the following XML would provide a PurchaseOrder object you could use to access the purchase-order element's customer, date, line-item, and shipper elements.

In Java code, you would get the value of the customer element with the PurchaseOrder object's getLineItem method. In script, you could do it in this way (note that JavaBeans casing rules apply):

    var lineItemXml = poXml.lineItem;
    var description = lineItemXml.description;

You can set XML values with the same field-style syntax.

    poXml.customer.name = "Darren Stevens";

Related Topics

Getting Started with XMLBeans

Importing Java Classes to ECMAScript with the import Statement