After a number of bad experiences with XML parsing in Java I decided to write my own parser.
This project has the following characteristics; correct XML parsing, small, few dependencies, thread-safe, user friendly API, secure.
Version 3.x depends on Java 19. For Java 8 support you have to use 2.x versions.
The unit tests in src/test/java/unittests can give you a good idea of the capabilities of SimpleXml. There are also a number of small example programs in src/test/java/example that showcase various features.
- @XmlAbstractClass
- @XmlAttribute
- @XmlEnumValue
- @XmlFieldDeserializer
- @XmlMapTagIsKey
- @XmlMapWithAttributes
- @XmlMapWithChildNodes
- @XmlName
- @XmlNameFromClass
- @XmlNoExport
- @XmlNoImport
- @XmlObjectValidator
- @XmlPath
- @XmlTextNode
- @XmlWrapperTag
It's in maven central.
<dependency>
<groupId>com.github.codemonstur</groupId>
<artifactId>simplexml</artifactId>
<version>3.2.0</version>
</dependency>
The simplest case possible:
import xmlparser.XmlParser;
public class MyObject {
String name;
}
MyObject object = new MyObject();
object.name = "test";
final XmlParser parser = new XmlParser();
System.out.println(parser.toXml(object));
This code will output:
<myobject>
<name>test</name>
</myobject>
There are more serialization options
- Renaming fields
- Fields as attributes
- Field as text node
- Skipping fields
- etc...
For more documentation on serializing look in src/docs/serialize.md.
The simplest case possible:
import xmlparser.XmlParser;
public class MyObject {
String name;
}
final XmlParser parser = new XmlParser();
final MyObject object = parser.fromXml("<myobject><name>test</name></myobject>", MyObject.class);
System.out.println(object.name);
This code will output:
test
The deserializer will respect the same annotations as the serializer
- Renamed fields
- Attributes
- Text nodes
- Skipped fields
- etc...
If there is a feature you would like to have, but doesn't exist, please let me know.
The MIT license. And I left out the copyright notice everywhere because I just don't care.