Using Acrobat JavaScript in Forms
Forms Essentials
6
In this first example, the usage of the
parse
method is illustrated below:
// Create the XML stream
var parseString = "<purchase>";
parseString += "<product>";
parseString += "<price>299.00</price>";
parseString += "<name>iPod</name>";
parseString += "</product>";
parseString += "<product>";
parseString += "<price>49.95</price>";
parseString += "<name>case</name>";
parseString += "</product>";
parseString += "</purchase>";
// Now create the DOM:
var x = XML.parse(parseString);
// Use the XFA API to obtain the XFA tree list for products:
var products = x.resolveNodes("product");
// Obtain the iPod product:
var iPod = products.item(0);
// Obtain the iPod price:
var price = iPod.getElement("price").value;
// Convert the price to a string:
var priceString = price.saveXML();
This next example accomplishes the same task through the usage of the
applyXPath
method:
// Create the XML stream
var parseString = "<purchase>";
parseString += "<product>";
parseString += "<price>299.00</price>";
parseString += "<name>iPod</name>";
parseString += "</product>";
parseString += "<product>";
parseString += "<price>49.95</price>";
parseString += "<name>case</name>";
parseString += "</product>";
parseString += "</purchase>";
// Now create the DOM:
var x = XMLData.parse(parseString,false);
// Set up the XPath expression:
var xPathExpr = "//purchase/product/[name='iPod']/price";
// Now get the iPod price:
var price = XMLData.applyXPath(x, xPathExpr);
Acrobat JavaScript Scripting Guide
113