OpenXES
Spex

The Spex library provides a complete Java™ library for the serialization of XML documents. Spex is very lightweight, optimized for high serialization performance, and easy to use. We provide Spex under the GNU LGPL license as open source / free software. If you are using Spex in your application, or have comments to share, we would love to hear from you.

decoy
  download

download
Spex version 1.0
released on February 10, 2009

(source code and binaries in compressed tar.gz archive)
36 kB

 
decoy

 

Hello world

Here is a simple example, which shows how you can compose XML documents in Java using Spex. This example uses the complete set of capabilities provided by the Spex library.

x
 

// The file we want to write our XML document to
File file = new File("SpexHelloWorld.xml");

// Now we create a Spex document, using the file for output.
SXDocument doc = new SXDocument(file);

// Add the root node to our new document.
SXTag root = doc.addNode("HelloWorld");

// The SXTag handle allows us to add attributes to our
// newly created node, until we add child nodes.

root.addAttribute("createdBy", "Spex");
root.addAttribute("coolness", "extreme");

// We can add XML comments anywhere in the document.
root.addComment("Now for some child nodes...");

// Adding child nodes works in the same way as adding
// the root node.

for(int i=0; i<3; i++) {

    SXTag child = root.addChildNode("Howdy");
    child.addAttribute("childNumber", Integer.toString(i));

    // Of course, any node can have child nodes of their own.
    SXTag grandChild = child.addChildNode("Aloha");
    
    // Adding an XML text node goes like this.
    grandChild.addTextNode("This is child number " + i + ".");
    
    // Note that we do not have to close any nodes. Nodes are
    // implicitly closed when we add another child node to
    // their parent.

}

// Do not forget to close the document when you are finished!
// Otherwise, your resulting document may be incomplete.

doc.close();

 
x

Executing the above code will result in the following XML document.

x
 

<?xml version="1.0" encoding="UTF-8" ?>
<HelloWorld createdBy="SpeX" coolness="extreme">
    <!-- Now for some child nodes... -->
    <Howdy childNumber="0">
        <Aloha>This is child number 0.</Aloha>
    </Howdy>
    <Howdy childNumber="1">
        <Aloha>This is child number 1.</Aloha>
    </Howdy>
    <Howdy childNumber="2">
        <Aloha>This is child number 2.</Aloha>
    </Howdy>
</HelloWorld>

 
x

And that's it. No special features for edge cases, just no-frills text-based XML document serialization made easy. If you have the need in your application to write XML documents in a simply and easy-to-read manner, with high performance, Spex is for you.

For a more detailed overview, you can browse the Javadoc API documentation of Spex here.


code.deckfour.org © 2008 by deckfour.org / Christian W. G√ľnther
All rights reserved.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.