All Projects → MarcGiffing → Easyxml

MarcGiffing / Easyxml

Licence: apache-2.0
Simplifies parsing and modifying of (huge) XML streams (files) based on the StAX parser with combination of JAXB or JDom2

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Easyxml

Posthtml
PostHTML is a tool to transform HTML/XML with JS plugins
Stars: ✭ 2,737 (+45516.67%)
Mutual labels:  xml, xml-parser
Dasel
Query, update and convert data structures from the command line. Comparable to jq/yq but supports JSON, TOML, YAML, XML and CSV with zero runtime dependencies.
Stars: ✭ 759 (+12550%)
Mutual labels:  xml, xml-parser
xgen
XSD (XML Schema Definition) parser and Go/C/Java/Rust/TypeScript code generator
Stars: ✭ 153 (+2450%)
Mutual labels:  xml, xml-parser
Saxy
A fast, easy-to-use and XML 1.0 compliant XML SAX parser in Elixir
Stars: ✭ 192 (+3100%)
Mutual labels:  xml, xml-parser
Node Xml2js
XML to JavaScript object converter.
Stars: ✭ 4,402 (+73266.67%)
Mutual labels:  xml, xml-parser
Xmlquery
xmlquery is Golang XPath package for XML query.
Stars: ✭ 209 (+3383.33%)
Mutual labels:  xml, xml-parser
Hquery.php
An extremely fast web scraper that parses megabytes of invalid HTML in a blink of an eye. PHP5.3+, no dependencies.
Stars: ✭ 295 (+4816.67%)
Mutual labels:  xml, xml-parser
Xmlbuilder2
An XML builder for node.js
Stars: ✭ 143 (+2283.33%)
Mutual labels:  xml, xml-parser
Camaro
camaro is an utility to transform XML to JSON, using Node.js binding to native XML parser pugixml, one of the fastest XML parser around.
Stars: ✭ 438 (+7200%)
Mutual labels:  xml, xml-parser
Tikxml
Modern XML Parser for Android
Stars: ✭ 370 (+6066.67%)
Mutual labels:  xml, xml-parser
Parse Xml
A fast, safe, compliant XML parser for Node.js and browsers.
Stars: ✭ 184 (+2966.67%)
Mutual labels:  xml, xml-parser
Quick Xml
Rust high performance xml reader and writer
Stars: ✭ 480 (+7900%)
Mutual labels:  xml, xml-parser
Xml2lua
XML Parser written entirely in Lua that works for Lua 5.1+. Convert XML to and from Lua Tables 🌖💱
Stars: ✭ 150 (+2400%)
Mutual labels:  xml, xml-parser
Pugixml
Light-weight, simple and fast XML parser for C++ with XPath support
Stars: ✭ 2,809 (+46716.67%)
Mutual labels:  xml, xml-parser
Woodstox
The gold standard Stax XML API implementation. Now at Github.
Stars: ✭ 145 (+2316.67%)
Mutual labels:  xml, xml-parser
fox
A Fortran XML library
Stars: ✭ 51 (+750%)
Mutual labels:  xml, xml-parser
Sax Wasm
The first streamable, fixed memory XML, HTML, and JSX parser for WebAssembly.
Stars: ✭ 89 (+1383.33%)
Mutual labels:  xml, xml-parser
Dart Xml
Lightweight library for parsing, traversing, and transforming XML in Dart.
Stars: ✭ 139 (+2216.67%)
Mutual labels:  xml, xml-parser
Node Rest Client
REST API client from node.js
Stars: ✭ 365 (+5983.33%)
Mutual labels:  xml, xml-parser
Xmlcoder
Easy XML parsing using Codable protocols in Swift
Stars: ✭ 460 (+7566.67%)
Mutual labels:  xml, xml-parser

= EASYXML

Easyxml an approach for handling large XML files with low memory footprint. The idea is to use the StAX reader and convert only a specific XML nodes to JDom2 or JAXB (at the moment). With this combination you get a very low memory footprint with the luxury of using high level XML parser.

The project requires Java 8.

NOTE: The project is still heavily under development. It would be nice if you have some tips to improve the API.

== Example XML

For the next description of parsing XML files we are using the following XML as an basis for the further examples.

[source,xml]

1 first note 2 second note ... 10000 xth note ---------------------------------------------------------------------

== JDom2

The first example shows the handling of XML files with JDOM2.

[source,java]

public class Note { private Long id; private String content; }

[source,java]

try(InputStream inputStream = new FileInputStream(new File("note.xml"))){

Parser<Element, Note> parser = JDom2ReaderBuilder . reader() .setInputStream(inputStream) .addItemReader( JDom2ItemReaderBuilder . itemReader() .shouldHandle("notes/note") .handle((e) -> { Note note = new Note(); note.setId(Long.valueOf(e.getChildTextTrim("id"))); note.setContent(e.getChildTextTrim("content")); return note; }).build() ) .build();

for (Note note : parser) {
	System.out.println(note);
}

} }

The noticeable parts will we explained in the folowing

The shouldHandle method will be called for each XML start element which is found in the document while parsing it with StAX. With the shouldHandle method you can consume the StAX stream to convert it to an JDom2 element. The shouldHandle method expects the path in the DOM tree.

[source,java]

.shouldHandle("notes/note")

When the path in the shouldHandle method matches the current path then the code within the handle method is called. The handle function expects an Java 8 Function<Element,Boolean>. The current element is automatically converted into an JDom2 element and can be accessed as usual.

Inside the handle a new instance of the Note class is created and returnd.

[source,java]

.handle((e) -> { Note note = new Note(); note.setId(Long.valueOf(e.getChildTextTrim("id"))); note.setContent(e.getChildTextTrim("content")); return note; }

[source,java]

for(Note note : parser) { System.out.println(note); }

== JAXB

The use of JAXB is similar to the JDom2 approach. You have two possibilities to use JAXB

  • via the ObjectFactory which is generated from a XSD
  • via a self generated "Note" Class and an jaxb.index file.

The JaxbReaderBuilder needs the path of the of the ObjectFactory or the jaxb.index file. With the withJaxbClass

[source,java]

try (InputStream inputStream = new FileInputStream(new File("note.xml"))) {

Parser<JAXBElement, Note> parser = JaxbReaderBuilder . reader(ObjectFactory.class.getPackage().getName()) .setInputStream(inputStream) .addItemReader( new JaxbItemReaderBuilder() .withJaxbClass(Note.class) .shouldHandle("notes/note") .build() ) .build();

for (Note note : parser) { System.out.println(note); } }

== Update with JDom2

With the help of the Jdom2WriterBuilder you can modify existing XML files without knowing the hole structure of the document. The approach is the same as with the JDom2Reader, with it you can modify the document within the handle and the changes are automatically written back to the output stream.

We can also remove specific parts of the document with the remove method instead of using the handle method.

[source,java]

try ( InputStream inputStream = new FileInputStream(new File("note.xml")); OutputStream outputStream = new FileOutputStream(new File("note_modified.xml"))) {

Writer writer = Jdom2WriterBuilder.writer() .setInputStream(inputStream) .setOutputStream(outputStream) .addItemWriter( new Jdom2ItemWriterBuilder() .shouldHandle(p -> { NoteContext noteContext = (NoteContext) p; return p.getPath().equals("notes/group/note") && noteContext.latestGroupId == 2; }) .remove() .build()) .addItemWriter( new Jdom2ItemWriterBuilder() .shouldHandle("notes/note") .handle((c) -> {

			Element contentElement = c.getElement().getChild("content");
			contentElement.setText("content: " + contentElement.getText());
		})
		.build())

.build();

writer.writeAll();

}

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].