SetXML

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

 


Welcome

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.


Package Contents

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.

Syntax

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:


Return Codes (ERRORLEVEL)

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.

Description Language (SDL)

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

 

ADD

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.

ADD CHILD

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.

GET

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.

REMOVE

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.

SELECT

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.

SET

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.

General commands

 

Comments

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.

 

Arguments

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";

 

Variables

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"

 

Functions

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.


SDL Examples

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.

CreatePAD

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

UpdatePAD

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

FileInfo

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

WinMeta

This example uses a larger and more advanced description file generated by Xml2Set.

setxml.exe winmeta.xml winmeta.txt /batch /create

History

August 9, 2013 - Version 1.4 (build 35.5)

  • Maintenance build.

February 25, 2013 - Version 1.3 (build 32.1)

  • adds a @Hash function that calculates the MD5 or SHA1 hash of a file pointed by the specified path.
  • renames @FileSize as @Size for consistency with other file functions.

August 27, 2012 - Version 1.2 (build 31.1)

  • adds the /nodeclaration parameter that allows the omission of the XML declaration header in the output file.
  • removes MSXML 6.0-only dependency. Now both version 3.0 and 6.0 of the MSXML component are supported though 6.0 is still the recommended version.
  • allows indenting and/or re-encoding of an XML document without any other processing.

December 15, 2009 - Version 1.1 (build 22.2)

  • adds <precision> parameter to the @FileSize function.

April 4, 2008 - Version 1.0 (build 18.5)

  • First release.

Latest Version

The latest demo version of SetXML is always available for download from this address:

http://noeld.com/programs.asp?cat=dev#setxml


Registration

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).


Redistribution

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).


Contact Details

E-mail: webmaster@noeld.com

Website: noeld.com