Using Java"s XMLStreamReader PULL XML Parser

By dave on February 17, 2014

In this entry I show how to use the inbuilt Java XMLStreamReader PULL parser class to read an XML file. The XML stream libraries are PULL based XML parsers that do not load the whole document into a memory structure, so therefore are more suited to large volumes of XML.

Below is an example XML file for a zoo, it contains Animal data types that have both attributes and data. It is kept simple for the sake of example. To run the example, copy this XML into the ROOT of your classpath.

Generate an ATOM feed with GroovyBuilder

By dave on July 1, 2012

Lastly, we look at building an ATOM document using the same principles. ATOM provides a means for site owners to provide updates to site users. It works by providing an XML document showing recent changes to the site. Browsers that support RSS and RSS readers can then highlight these changes to users.

An atom formatted document contains two main sections, the header which describes the feed, and then a list of elements, that describe the content. This is not intended as a full introduction to ATOM, but rather as an example of how to implement it. For the sake of this example we have a class Entry that represents a page on the site, for we want to provide a list of changed entries via ATOM. We use the class AtomGenerator to generate the xml, and at the bottom of the script is the test harness that will fire it up.

Reading XML in groovy using XmlParser

By dave on July 1, 2012

Groovy has great inbuilt xml support, and allows you to treat xml paths like objects. Reading elements and attributes is so straightforward that it was one of the factors that got me started with Groovy. So to build an object tree from xml, we just use the XmlParser class.

To dereference an element we use normal dot syntax, for an attribute, simply add the at symbol (@) before the name, see the example below.

Write XML with GroovyBuilder

By dave on July 1, 2012

Groovy supports the concept of builders, which provide an abstraction between the required output content and the representation of it. Groovy supports this by providing a tree like structure in groovy code that represents the required HTML or XML:

import groovy.xml.MarkupBuilder

// create a builder to generate xml like content from a
// builder structure, in this case we choose
// StringWriter as the output, but it could be any writer.
def writer = new StringWriter();
def builder = new MarkupBuilder(writer);

builder.html {
    head {
        title "Hello world"
    }
    body {
        h1 "My Hello world page"
        p "This is the content"
    }
}

println(writer);

So what have we done?

We generated some HTML, in this case we just printed it to the console, but we could have sent this back to a web browser for example, or saved it to disk. The XML structure that we generated from above looked as follows:

tcMenu for Arduino

This site uses cookies to analyse traffic, and to record consent. We also embed Twitter, Youtube and Disqus content on some pages, these companies have their own privacy policies.

Our privacy policy applies to all pages on our site

Should you need further guidance on how to proceed: External link for information about cookie management.

Send a message
X

Please use the forum for help with UI & libraries.

This message will be securely transmitted to our servers.