Skip navigation links

Package org.wamblee.xml

Utilities for XML processing.

See: Description

Package org.wamblee.xml Description

Utilities for XML processing. The aim of this package is to simplify the common tasks of parsing, validating, and transforming XML files and to provide support for XPath. The utlities use the standard Java SE APIs but are much easier to use. For cases where more advanced functionality is required, the classes provide access to the underlying Java SE types. The implementation is based on DOM level 3 parsing.

Classes XMLDocument, XMLSchema, and XSLTransformation provide parsing, validation, and transformation of XML files. The XMLDocument class provides various static methods as entry point for this functionality, simplifying the code a lot when static imports are used. The API design uses a fluent interfaces style. For instance, to parse, validate, and transform an XML file, one can write:

 import static org.wamblee.xml.XMLDocument.*;
 ...
 XMLDocument doc = xmldocument(new File("x.xml").toURI()).validate(new File("x.xsd").toURI()).transform(new File("x.xsl").toURI());
 

In addition, a URI resolver ClasspathUriResolver is provided to allow resolution of documents on the classpath.

For XPath the following classes are provided:

For instance to apply an XPath expression to an XML document:

      NamespaceContext context = new XPathContext(new SimpleNamespaceContext()
            .addPrefix("n", "http://example.com/1")
            .addPrefix("m", "http://example.com/2"));
      XMLDocument doc = new XMLDocument(new File("xpathexample.xml").toURI());
      String value = context.createExpression("/n:root/m:x").stringEval(doc);
 

Or, using static imports:

      NamespaceContext context = xpathcontext(namespaces()
            .addPrefix("n", "http://example.com/1")
            .addPrefix("m", "http://example.com/2"));
      XMLDocument doc = xmldocument(new File("xpathexample.xml").toURI());
      String value = context.createExpression("/n:root/m:x").stringEval(doc);
 
Skip navigation links

Copyright © 2022. All rights reserved.