Version 1.3 (August 2013)
© 2008-2013, Noël DANJOU
All rights reserved.
Table of Contents | |
---|---|
Welcome | |
Package Contents | |
Syntax | |
Return Codes (ERRORLEVEL) | |
Description Language (SDL) | |
Commands | |
Comments | |
Arguments | |
Variables | |
Functions | |
SDL Examples | |
History | |
Latest Version | |
Registration | |
Redistribution | |
Contact Details |
SetXML is a small command-line tool that allows you to easily create or update XML files from a Command window, a script, a batch file or the Task Scheduler, etc. The program was especially designed to update PAD files but it can update XML files for any other purpose. The program features its own XML description language and implements different file-related functions to easily insert path, version, size and time information.
SetXML can also be used to re-indent and/or re-encode an XML file without any other processing.
The SetXML package is provided as a compressed (zip) archive that holds a few files. The included files are described in the table below.
File | Description |
---|---|
setxml.exe | The SetXML program. |
xml2set.exe | Program only included with the full version. Converts an XML file to a SetXML description file. |
readme.htm | This file. |
license.htm | File only included with the full version. |
createpad.txt | Sample description file that creates a demo PAD file. |
updatepad.txt | Sample description file that updates an existing PAD file. |
fileinfo.txt | Sample description file that creates a simple XML file. |
winmeta.txt | Sample description file generated by Xml2Set that creates an XML file with a more complex layout. |
SetXML features many parameters, for a whole list of the supported parameters and a description, type the following command in a Command window:
setxml /?
The simplest syntax only requires the XML file to update, an XPath query and the value to assign to the node selected by the XPath query, e.g.:
setxml pad.xml ?//File_Info/File_Size_Bytes 228348
The command line above tells SetXML to open the existing pad.xml file, to select the node that matches the //File_Info/File_Size_Byte query and to give this node the 228348 value. The pad.xml file is then saved and the program exits.
If you want to make multiple changes at once on the same XML file, you will have to use the batch mode. The batch mode takes a description file (a text file, see Description Language) as a parameter. The syntax for the batch mode is like this:
setxml pad.xml updatepad.txt /batch
The command line above tells SetXML to open the existing pad.xml file, to process all the commands from the updatepad.txt file, to save the XML file once the updates are applied and to exit. If the pad.xml file does not exist before the command is issued, you may append the /create parameter, to tell the program to create the XML file.
Example:
setxml pad.xml createpad.txt /batch /create
If you only want to (re)format an XML file (e.g. indent it or fix a broken indenting) without applying any other changes, you can issue a command like the one below. The optional /output parameter is used to specify the name of the formatted file so that the pad.xml file remains unmodified.
setxml pad.xml /indent /output:indented.xml
Notes:
When SetXML is used in a batch file, you can use ERRORLEVEL to check the completion code returned by the program. The possible return codes are listed in the table below.
Code | Description |
---|---|
0 | Success. |
1 | Syntax error or help/registration information displayed. |
2 | Initialization error. |
3 | MSXML 3.0 (or 6.0) component not installed. |
4 | XML document could not be loaded. |
5 | XML document could not be parsed. |
6 | Description file processing failed (when used with /batch parameter). |
7 | XML update failed. |
8 | XML document could not be saved. |
In batch mode, SetXML takes a text file as a parameter in addition to the XML document. That text file is composed of one or more commands written in a simple language called the SetXML Description Language (SDL). The commands are especially designed to describe and to manage PAD files and any other XML documents in a simple way. The SetXML Description Language is described in this paragraph.
A command is composed of a command tag optionally followed by a qualifier and zero or more parameters depending on the command itself. The command always ends by a semicolon. A command may span multiple lines.
<command> [<qualifier>] [<parameter 1> ... <parameter n>] ;
Supported commands are described in the Commands paragraph below.
Parameters are usually text strings but they can also be variables , arguments, functions or a concatenation of two or more of these types, e.g.:
select node "//applications/application[name=\"" + @FileName($3) + "\"]";
Please note that if you have to place a quoted text inside a parameter that is already quoted, you'll have to escape the insider quotes with backslash characters as shown in the example above.
For this reason, if you have to use the backslash character, you'll have to escape it as well. This is done by writing the backslash character twice, e.g.:
set node "C:\\Windows\\notepad.exe";
Commands | |
---|---|
add | |
add child | |
get | |
remove | |
select | |
set | |
General commands | |
Comments | |
Arguments | |
Variables | |
Functions |
This set of commands is used to add new elements or nodes to the XML document or to add attributes to a selected node.
add attribute <name> <value>;
Adds an attribute to the current node. If the attribute already exists it is replaced.
add cdatasection <data>;
Adds a CDATA section to the current node.
add comment <data>;
Adds a comment to the current node.
add node <name> <value>;
Adds a text node to the current node. The new node becomes the current node.
add root <name>;
Adds a root node to the document. This will clear the current document. The root node becomes the current node.
add text <data>;
Adds a text element to the current node.
This set of commands is used to add new nodes to the XML document. The current node is not changed so as opposed to the ADD command set, you don't have to select the parent node again to add a sibling node.
add child cdatasection <name> <data>;
Adds a child node with a single CDATA section to the current node.
add child element <name>;
Adds an empty child node to the current node.
add child text <name> <data>;
Adds a child text node to the current node.
This set of commands is used to read text nodes or attributes from a selected node. The result is placed in a variable. The variable does not have to be declared beforehand.
get attribute <name> <variable>;
Gets the value of the named attribute from a selected node and puts this value in the specified variable.
get child <node> <variable>;
Gets the value of the specified child node and puts this value in the specified variable. <node> is either a 0-based index or a XPath query string.
get node <variable>;
Gets the value of the current node and puts this value in the specified variable.
This set of commands is used to remove specific nodes or to remove attributes from a selected node.
remove attribute <name>;
Removes the specified named attribute from the current node.
remove child <node>;
Removes a child node of the current node. <node> is either a 0-based index or a XPath query string. The current node remains unchanged.
remove node;
Removes the current node. The parent node of the removed node becomes the current node.
This set of commands is used to select a specific node. The selected node automatically becomes the current node.
select child <node>;
Selects a child node of the current node. <node> is either a 0-based index or a XPath query string.
select document;
Selects the document node.
select next;
Selects the next sibling of the current node.
select node <query>;
Selects the first node that matches the specified XPath query string in the whole document.
select parent;
Selects the parent node of the current node.
select previous;
Selects the previous sibling of the current node.
select root;
Selects the root node of the document.
This set of commands is used to assign a new value to an attribute or to a text node or to set second-level properties.
set attribute <name> <value>;
Adds or replaces the named attribute and gives it the specified value.
set child <node> <value>;
Sets the value of a child node of the current node. <node> is either a 0-based index or a XPath query string. The child node must exist.
set node <value>;
Changes the text of the current node.
set property <name> <value>;
Sets a second-level property on the document. This is mostly useful to set the SelectionNamespaces property before selecting a node that belongs to a specific namespace. Some other property may not work.
clear;
Empties the current document but leaves the variables and arguments untouched.
dec <variable>;
Decrements an integer variable. The variable must have been assigned an integer value first. See var.
inc <variable>;
Increments an integer variable. The variable must have been assigned an integer value first. See var.
load <path>;
Loads a new XML document. Any previously loaded document is discarded but the variables and arguments are untouched. This allows you to pick some data from one document and to put them in another one. The root node of the new document becomes the current node.
var <variable> = <value>;
Defines a variable and give it an initial value. A variable cannot be defined without being assigned a value. The variable may either be an integer or a text string. Arguments, variables and functions are valid values as well.
You can put comments anywhere in the description file. The comment tags are the same as in the C++ language, e.g.:
set node $3; // Set value of 3rd command-line parameter.
or
select node /* we use a user-defined argument */ $3;
The /* ... */ notation may span multiple lines if needed, while the // notation only defines a comment up to the end of the current line.
The command-line arguments passed to setxml.exe may be retrieved using $0, $1, etc. and used as a command or function parameters or assigned to a variable. $0 returns the setxml.exe path, $1 returns the XML document path and $2 returns the description file. $3 and later are user-defined and may be used to pass values to the description file.
Example:
Let's say setxml.exe is called from the Command Prompt using the syntax just below.
setxml.exe pad.xml batch.txt nodeToSelect /batch
You can retrieve the user-defined argument (nodeToSelect) like this:
var nodeName = $3; select node nodeName;
or like this:
select node $3;
Both commands above would give the same result as the command below:
select node "nodeToSelect";
Integer and text variables are supported. They are either defined using the var command, or auto-defined by one of the get commands.
Examples:
var myquery = "//channels/channel";
get node nodeValue; // nodeValue is a variable assigned by the get command
Text, integer values, arguments, functions and other variables are valid values for variables.
Variables can be used as command and function parameters. Integer variables may be used as indexes for the select child, set child or remove child commands.
Example:
var index = 1; set child index "text of child node #1" inc index; set child index "text of child node #2"
The description language features a set of functions that can be used as command parameters. Functions begin with a @ character followed by the function name. Function parameters are placed between parenthesis and are separated by commas.
@<function> ( <parameter 1> [, [... <parameter n>]] )
Text, variables, arguments and functions themselves are valid function parameters.
Supported functions are listed and described below.
@CreationTime(<path>, [<datefmt>], [<timefmt>])
Returns the creation time of the file pointed by path.
The second parameter is optional and describes the output format of the date. The third parameter is optional as well and describes the output format of the time. The syntax of both format parameters are identical to the formats used in the Customize Regional Optionsof the Control Panel.
Example:
@CreationTime("setxml.exe", "MM/dd/yyyy", )
Output:
04/03/2008
@Directory(<path>)
Returns the directory from the provided path.
Example:
@Directory("C:\\Windows\\notepad.exe")
Output:
C:\Windows
@FileHash(<path>, <MD5|SHA1>)
Returns the hash of the file pointed by path. SHA1 is used by default if no algorithm is not specified.
Example:
@FileHash("setxml.exe", )
Output:
36a1cb1c7b0c3b83246527f349d548d8d230dd2a
@FileName(<path>)
Returns the filename from the provided path.
Example:
@FileName("C:\\Windows\\notepad.exe")
Output:
notepad.exe
@FileSize(<path>, <format>, [<precision>])
Returns the size of the file pointed by path. Format can be "B", "KB", "MB" or "GB" to get the size as bytes, kilo-bytes, mega-bytes or giga-bytes respectively. The optional precision parameter specifies the number of decimal to print (if any). If precision is blank, the default precision is used.
Example:
@FileSize("setxml.exe", "B", )
Output:
78456
@FullPath(<path>)
Returns the fully qualified path from the provided path.
Example:
@FullPath(".\\notepad.exe")
Output:
C:\Windows\notepad.exe
@LastAccessTime(<path>, [<datefmt>], [<timefmt>])
Returns the last access time of the file pointed by path. Parameters are the same as @CreationTime.
@LastWriteTime(<path>, [<datefmt>], [<timefmt>])
Returns the last write time of the file pointed by path. Parameters are the same as @CreationTime.
@LowerCase(<text>)
Returns the text in lowercase.
Example:
@LowerCase("TEST")
Output:
test
@UpperCase(<text>)
Returns the text in uppercase.
Example:
@UpperCase("test")
Output:
TEST
@Version(<path>, <format>)
Returns the version of the executable pointed by path (if available). The second parameter controls the format of the returned string; # characters are respectively replaced by major, minor, build and qfe values of the version, any other text is copied as-is.
Example:
@Version("setxml.exe", "Version #.## (build #.#)")
Output:
Version 1.3 (build 19.4)
@VersionInfo(<path>, <information>)
Returns the value of the string named <information> in the version block of the executable pointed by path (if available). <information> can be Comments, CompanyName, FileDescription, etc.
Example:
@VersionInfo("setxml.exe", "LegalCopyright")
Output:
© 2008-2013, Noël Danjou. All rights reserved.
The SetXML package includes some example files to help you to get started with the description language. The provided files may serve as a basis to your own developments.
This example shows you how to create a basic PAD file in batch mode. This also shows the description language as it is generated by the Xml2Set application.
setxml.exe pad.xml createpad.txt /batch /create
This example shows you how to possibly update an existing PAD file using the file functions and arguments in batch mode. This example assumes that all the files including the executable (yourapp.exe) and the archive (yourapp.zip) of your own program are in the same folder. To run the test you can use setxml.exe and setxml.zip instead.
setxml.exe pad.xml updatepad.txt yourapp.exe yourapp.zip /batch
This example shows you how to create a new XML file and to use some file functions in batch mode.
setxml.exe fileinfo.xml fileinfo.txt readme.htm /batch /create
This example uses a larger and more advanced description file generated by Xml2Set.
setxml.exe winmeta.xml winmeta.txt /batch /create
The latest demo version of SetXML is always available for download from this address:
The demo version of this package includes a fully functional version of SetXML but does not include the Xml2Set converter.
To register, click the link just below and follow the instructions.
https://shopper.mycommerce.com/checkout/cart/add/4076-12
Alternatively, you can type the following command in a Command window to open the registration page in the default browser:
setxml /register
Orders and delivery are handled by MyCommerce (a Digital River Company).
Computer magazine publishers are welcome to redistribute the demo application as-is on their complimentary or monthly CDs. Any other redistribution of the application with commercial products is strictly forbidden without my written permission. Please contact me for a license agreement (see Contact Details).
E-mail: webmaster@noeld.com
Website: noeld.com