Piccolo parser performance Java vs SAX

Getting Started with Piccolo XML Parser for Java: Setup and Examples

Piccolo is a small, efficient XML parser for Java that focuses on fast, low-overhead parsing with a simple API reminiscent of SAX. This guide shows how to set up Piccolo in a Java project and provides practical examples for common parsing tasks.

1. Prerequisites

  • Java 8+ installed.
  • A build tool (Maven or Gradle). Examples below use Maven, with a quick note for Gradle.
  • Basic familiarity with Java I/O and XML structure.

2. Add Piccolo to your project

Piccolo is not part of the standard Java library, so add it as a dependency. If a hosted Maven artifact is available, use it; otherwise download the JAR and add it to your project classpath.

Maven (example coordinates—replace with actual groupId/artifactId/version if different):

xml
 net.sourceforge.piccolo piccolo 2.0

Gradle (if published):

groovy
implementation ‘net.sourceforge.piccolo:piccolo:2.0’

If no repository artifact exists, download piccolo.jar and place it under your project’s libs/ then add it to the classpath.

3. Basic parsing model

Piccolo offers a streaming, event-driven API similar to SAX: you create a handler that receives events (startElement, endElement, text) while the parser reads the document. That makes it memory-efficient for large files.

4. Simple example — print element names and text

This example demonstrates reading an XML file and printing element start/end events and text content.

java
import net.sf.piccolo.Piccolo;import net.sf.piccolo.DocumentHandler;import java.io.FileInputStream;import java.io.InputStream; public class PiccoloExample { public static void main(String[] args) throws Exception { InputStream in = new FileInputStream(“example.xml”); Piccolo parser = new Piccolo(); parser.setDocumentHandler(new DocumentHandler() { public void startDocument() {} public void endDocument() {} public void startElement(String name, int attsLen) { System.out.println(“Start: ” + name); // attributes can be read with parser.getAttributeName(i), getAttributeValue(i) } public void endElement(String name) { System.out.println(“End: ” + name); } public void text(char[] ch, int start, int length) { String txt = new String(ch, start, length).trim(); if (!txt.isEmpty()) System.out.println(“Text: ” + txt); } }); parser.parse(in); in.close(); }}

Notes:

  • Attribute access: within startElement you can query parser.getAttributeCount(), parser.getAttributeName(i), and parser.getAttributeValue(i) to iterate attributes.
  • Piccolo uses char[] buffers for text events for performance; convert to String only when needed.

5. Example — extracting data into objects

Parse a list of elements into Java objects.

java
import net.sf.piccolo.Piccolo;import net.sf.piccolo.DocumentHandler;import java.io.FileInputStream;import java.io.InputStream;import java.util.ArrayList;import java.util.List; class Book { String id; String title; String author; public String toString(){ return id + “: ” + title + “ by ” + author; }} public class BooksParser { public static void main(String[] args) throws Exception { InputStream in = new FileInputStream(“books.xml”); Piccolo parser = new Piccolo(); List books = new ArrayList<>(); Book current = null; String currentElement = null; StringBuilder textBuffer = new StringBuilder(); parser.setDocumentHandler(new DocumentHandler() { public void startDocument() {} public void endDocument() {} public void startElement(String name, int attsLen) { currentElement = name; textBuffer.setLength(0); if (“book”.equals(name)) { current = new

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *