JRun includes XMLScript, an XPath-based XML scripting utility that lets you execute a limited set of XPath statements on XML files.
With XMLScript, you can search an XML file for an expression, and then append or substitute values in nodes, or delete a node. You can also just view the resulting nodes of an XPath expression. You can save the results to a new XML file or replace the original XML file.
XMLScript syntax is as follows:
> java -classpath classpath XMLScript [-i input-file] [-o output-file] [-f script-file] -[a|d|s|v statement ...]
XMLScript is in the jrun_root/lib/jrun.jar file. You must include jrun.jar in your classpath to use XMLScript. On Windows platforms, you can launch XMLScript by double-clicking the XMLScript.exe file in the jrun_root/bin directory.
The input-file argument defaults to the standard input.
The output-file argument defaults to the value of input-file. If you specify a dash, it defaults to the standard output.
The script-file argument specifies the path to a file containing a list of XPath statements. If you do not specify a script file, XMLScript executes any XPath statements you pass on the command line. For more information on using a script file with XMLScript, see "Using script files with XMLScript".
XPath statements take the following forms:
[-a expression value]
[-d expression] [-s expression value] [-v expression]
<attribute name="targetHost" />
If you append the value "localhost" to the node matching this example, the resulting line would be as follows:
<attribute name="targetHost">localhost</attribute>
You can pass XPath commands to XMLScript using the command line or a script file. You can also include XMLScript in your Java class files to execute XPath statements programmatically. This section provides examples of using the XMLScript utility.
You can execute XMLScript on the command line by passing the expression and value of a statement as arguments, as shown in the following example:
>java -classpath c:/jrun4b2/lib/jrun.jar jrunx.xml.XMLScript -i c:/jrun4b2/servers/default/server-inf/jrun.xml -s //service[@name='WebService']/attribute[@name='port']/text() 8842
This example finds a service element with the name WebService and replaces the service's port attribute with 8842. The resulting XML appears as follows:
<service class="jrun.servlet.http.WebService" name="WebService">
��<attribute name="port">8842</attribute> </service>
Note: When using XMLScript statements on the command-line, use single quote characters around arguments.
You can pass any number of statements on the command line. The following command executes two statements:
>java -classpath c:/jrun4b2/lib/jrun.jar jrunx.xml.XMLScript -i c:/jrun4b2/servers/default/server-inf/jrun.xml -s //service[@name='ClusterManager']/attribute[@name='enabled']/text() true -s //service[@name='ClusterManager']/ attribute[@name='clusterDomain']/text() newserver
You can run XMLScript and pass in a script file containing the XPath commands, expressions, and values. This lets you to execute any number of XPath statements at one time. The following example extracts XPath statements from the scriptfile.txt file and executes them on the inputfile.xml file:
> java -classpath c:/jrun4/lib/jrun.jar jrunx.xml.XMLScript -i inputfile.xml -f scriptfile.txt
The following is an example of an XMLScript script file that contains XPath statements:
-s //service[@name=\"WebService\"]/attribute[@name=\"port\"]/text() 8142
-s //service[@name='WebService']/attribute[@name='port']/text() 8842 -s //service[@name='ClusterManager']/attribute[@name='enabled']/text() true -a //service[@name=\"LauncherInfo\"]/attribute[@name=\"vmArgs\"] -Xms128
When you execute XMLScript with a script file, the hyphen (-) before the a or s command is optional.
XMLScript exposes the following method:
command(Array)
The Array object contains each command, expression, and value of the XPath statements in a separate element. To use XMLScript programmatically, you must import the jrunx.xml.XMLScript package. To compile your Java class, you must include the jrun.jar file in your classpath.
The following code shows how to use XMLScript in a Java class:
import java.util.*;
import java.io.*; import jrunx.xml.XMLScript;
public class XMLScriptTest {
�public static void main() throws Exception {
��String xmlinfile = "c:/jrun4/servers/default/SERVER-INF/jrun.xml";
��String xmloutfile = "c:/jrun4/servers/newserver/SERVER-INF/jrun.xml";
��ArrayList args = new ArrayList();
��args.add("-i");
��args.add(xmlinfile);
��args.add("-o");
��args.add(xmloutfile);
��String port = "8142";
��args.add("-s");
��args.add("//service[@name=\"WebService\"]/attribute[@name=\"port\"]/text()");
��args.add(port);
��String vmarg = "-Xms128";
��args.add("-a");
��args.add("//service[@name=\"LauncherInfo\"]/attribute[@name=\"vmArgs\"]");
��args.add(vmarg);
��//executes XMLScript with the args
��String[] scriptArgs = new String[args.size()];
��args.toArray(scriptArgs);
��try {
���XMLScript xs = new XMLScript();
���xs.command(scriptArgs);
��} catch (Exception e) {
��}
�}
}
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/jrun/4/Programmers_Guide/servletxml7.htm