Hybrid SAX-DOM XML parser for Java
Java
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
solna-java-example
solna-java
.gitignore
README.md

README.md

Solna

Hybrid SAX-DOM XML parser for Java.

With Solna you can:

  • Parse file of any size. Memory usage stays low.
  • Use your favorite DOM library: native, jdom, dom4j or xom.
  • Avoid writing boilerplate DOM construction code.

Download

Get Solna from maven central repository

<dependency>
    <groupId>com.vseravno.solna</groupId>
    <artifactId>solna</artifactId>
    <version>0.5.1</version>
</dependency>

Usage

Suppose you have an XML file

<cities>
    <city id="1">
        <name>Stockholm</name>
    </city>
    <city id="2">
        <name>Gothenburg</name>
    </city>
    ...
</cities>

Let's parse it

SolnaParser parser = new SolnaParser();

parser.addHandler("/cities/city", new SolnaHandler<Element>() {
    public void handle(Element elem) {
        // Here goes your code
        // parse, check, save to database etc
    }
});

InputStream is = ...
parser.parse(is);

Element can be any of these:

org.w3c.dom.Element
org.jdom.Element
org.jdom2.Element
org.dom4j.Element
nu.xom.Element

Be sure to add it to your classpath.

Why Solna?

###Solna vs DOM

  • Solna has low memory usage. If you can fit the whole DOM tree into your RAM, Solna has not much to offer compared to traditional DOM parsing.
  • You can't use document-wide XPath in Solna.
  • Solna can be slightly slower, because it builds separate DOM trees for each element. Although it can perform better, if you want to skip some parts of the document.
  • Also you can use Solna to gracefully incapsulate DOM construction logic.

###Solna vs SAX/StAX

  • SAX/StAX require you to write unintuitive and tricky handling code. Solna uses DOM.
  • Solna is slower than SAX/StAX, because of DOM construction.

###Solna vs JAXB

  • Solna will not bind values to your POJOs, but it gives you more control on parsing process through DOM manipulation.
  • Solna is slower than JAXB, because of DOM construction.

Behind the scenes

Solna uses StAX to scan XML. When it finds an element you provided handler for, it builds DOM tree for that element. After handling is done DOM tree is thrown away thus freeing the memory.