See: Description
| Interface | Description |
|---|---|
| LocationAware |
Implemented by XML element classes that need to know which file the element was read from, and
where in that file (line / column) the element was read.
|
| Class | Description |
|---|---|
| XmlFile |
An XML attribute value that corresponds to an XML file that can be parsed.
|
| XmlGenerator |
A utility class for generating XML documents.
|
| XmlGenerator.AttributesBuilder | |
| XmlParser<Root> |
Class for parsing an XML document into an object graph.
|
| Enum | Description |
|---|---|
| Comment |
Marker, used together with the
@Child(Comment.class) annotation to mark that the annotated text method
accepts comments rather than regular text elements. |
| Comment.Header | |
| XmlParser.Option |
Options that can be passed to an XML parser.
|
| Annotation Type | Description |
|---|---|
| Attribute |
Handle an XML attribute.
|
| Child |
Handle a nested XML element.
|
| Element |
Defines that a class corresponds to a particular XML element.
|
In order to parse XML into you object graph, you need to create an XmlParser for
your root object type:
XmlParser<MyRoot> PARSER = XmlParser.xmlParser( MyRoot.class );
Your class MyRoot and the child node types should be annotated with
@Element and have fields annotated with
@Attribute to map the attributes of the xml element, and methods
annotated with @Child to map the child elements of the xml element.
Accepted types for fields annotated with @Attribute are:
Stringint or Integerboolean or Booleanlong or Longdouble or Doubleenum typeXmlFilem - in order to reference another XML file (typically for
inclusion).AttributeHandler for details)
It is also possible to use the @Attribute annotation on a method that
accepts a single argument (and returns void). The accepted argument types are the same as for fields annotated with
@Attribute.
Methods annotated with @Child may have any name, should return void
and have one of the following parameters lists:
@Element
@Child
void add( SomeChildType child ) {
// do what you want with the child object here
}
@Element(uri=YOUR_NAMESPACE_URI, name="some-child")
class SomeChildType {
}
@Child annotation. Each of the types in the arguments list of the
@Child annotation should be annotated with
@Element, but the actual parameter type of the method does not need that
annotation.
@Child(ActualChildOne.class, ActualChildTwo.class)
void add( SomeChildInterface child ) {
// do what you want with the child object here
}
interface SomeChildInterface {
}
@Element(uri=YOUR_NAMESPACE_URI, name="one")
class ActualChildOne implements SomeChildInterface {
}
@Element(uri=YOUR_NAMESPACE_URI, name="two")
class ActualChildTwo implements SomeChildInterface {
}
String parameter. This handles text within the XML element.
Alternatively, if the @Child annotation is given an argument of
Comment.class the method handles XML comments within the XML element
instead. For the root element type, it is also possible to give the @Child
annotation an argument of Comment.Header.class, which makes the
method handle XML comments from before the start of the first XML element.
@Child
void text( String text ) {
// handle embedded text
}
@Child( Comment.class )
void comment( String comment ) {
// handle embedded comments
}
@Child( Comment.Header.class )
void header( String headerComment ) {
// handle embedded the header comment(s)
}
char[] text, int start, int length. This works in the same way as if the method had a
single String parameter, but uses the raw char[] from the parser, with a start offset and
a length of the string found at that offset and does not have to instantiate a new string. This might be preferable
if further parsing is to be made on the characters in the embedded text. As with String-methods,
Comment.class and
Comment.Header.class can be given as arguments to the
@Child annotation in order to handle embedded comments or the header
comment(s) of the file instead of embedded text.
@Child
void text( char[] text, int start, int length ) {
// handle embedded text
}
@Child( Comment.class )
void comment( char[] comment, int start, int length ) {
// handle embedded comments
}
@Child( Comment.Header.class )
void header( char[] headerComment, int start, int length ) {
// handle embedded the header comment(s)
}
parse-methods:
XmlParser.parse(java.nio.file.Path, org.opencypher.tools.xml.XmlParser.Option...)XmlParser.parse(java.io.Reader, org.opencypher.tools.xml.XmlParser.Option...)XmlParser.parse(java.io.InputStream, org.opencypher.tools.xml.XmlParser.Option...)@Element) implement the
LocationAware interface.uri-parameter of the @Attribute
annotation.Structure. This class is responsible for creating
NodeBuilder objects that knows how to instantiate and build objects for the various
XML entities that the parser encounters.
The actual parsing, and keeping track of which NodeBuilder maps to what part of the
XML file is handled by ParserStateMachine.
Converting XML attributes into values that can be assigned to fields, or passed into methods annotated with
@Attribute is handled by
AttributeHandler, which also handles the actual field assignment or invocation.
For creating XmlFile attribute values, a Resolver
instance is used for finding the xml file in question.Copyright © 2019. All rights reserved.