public abstract class XmlProcessor extends Object
The XmlProcessor class provides a framework for processing an XML Document to
create a Java Object representation of the document or to perform other
actions on the contents of the document. The XmlProcessor uses the STAX API
to read the XML document using the XMLStreamReader, by using the
streaming API the XML parser will not load the XML document into memory
(although the Java Object representation created by subclasses of
XmlProcessor would likely be in memory at all times).
Users of a XmlProcessor implementation construct a new instance of the
subclass and invoke the process(XMLStreamReader) method to process
the document and return the result object generated by the processor.
To process a document using the XmlProcessor a subclass of XmlProcessor must
be created for each XML namespace that needs to be processed. For each XML
element defined for the XML namespace a process method must be defined in the
subclass. The name of the process method must have the "process" prefix
followed bfollowed by the XML element name, have only an
XMLStreamReader parameter and return an Object (any subclass of
Object can be returned).
For example the process method for the XML element BankAccount would have the following signature.
public BankAccount processBankAccount(XMLStreamReader parser);
For example the process method for the XML element firstName would have the following signature. Note that the part of the method name for the XML element must have the same case as the XML element name.
public String processfirstName(XMLStreamReader parser);
The process methods read the attributes from the element and can either
create an Object for the XML element or perform other processing on the
element. This object can be returned from the process method so that the
calling method can access the object. If an XML element has child elements it
can either ignore them using the
StaxUtils.skipSubTree(XMLStreamReader) method or process the child
element using the process(XMLStreamReader) method that will invoke
the appropriate process method for that element.
The following example shows the implementation of a process method for a
Person element that has a firstName and lastName element. As no children are
expected the StaxUtils.skipSubTree(XMLStreamReader) method is used to
skip to the end of the element.
public Person processPerson(final XMLStreamReader parser)
throws XMLStreamException, IOException {
String firstName = parser.getFieldValue(null, "firstName");
String lastName = parser.getFieldValue(null, "lastName");
Person person = new Person(firstName, lastName);
StaxUtils.skipSubTree(parser);
return person;
}
The following example shows the implementation of a process method for a Family element that has one or more Person or Pet child elements. If an unexpected element occurs and error will be recorded.
public Family processFamily(final XMLStreamReader parser)
throws XMLStreamException, IOException {
Family family = new Family();
while (parser.nextTag() == XMLStreamReader.START_ELEMENT) {
Object object = process(parser);
if (object instanceof Person) {
config.addPerson((Person)object);
} else if (object instanceof Pet) {
config.addPet((Pet)object);
} else {
context.addError("Unexpected Element:" + object, null,
parser.getLocation());
}
}
return family;
}
| Modifier | Constructor and Description |
|---|---|
protected |
XmlProcessor(String namespaceUri)
Construct a new XmlProcessor for the XML Namespace URI.
|
|
XmlProcessor(String namespaceUri,
Map<String,Class<?>> tagNameClassMap) |
| Modifier and Type | Method and Description |
|---|---|
XmlProcessorContext |
getContext()
Get the context for processing the XML Document.
|
String |
getNamespaceUri() |
<T> T |
parseObject(XMLStreamReader parser,
Class<? extends T> objectClass) |
<T> T |
process(org.springframework.core.io.Resource resource) |
<T> T |
process(String xml) |
<T> T |
process(XMLStreamReader parser)
The process method is used to return an object representation of the
current XML element and subtree from the
XMLStreamReader. |
static void |
registerEnumConverter(Class<? extends Enum> enumClass) |
void |
setContext(XmlProcessorContext context)
Set the context for processing the XML Document.
|
protected XmlProcessor(String namespaceUri)
namespaceUri - The XML Namespace URI.public final XmlProcessorContext getContext()
public String getNamespaceUri()
public <T> T parseObject(XMLStreamReader parser, Class<? extends T> objectClass) throws XMLStreamException, IOException
XMLStreamExceptionIOExceptionpublic <T> T process(org.springframework.core.io.Resource resource)
public <T> T process(String xml)
public <T> T process(XMLStreamReader parser) throws XMLStreamException, IOException
The process method is used to return an object representation of the
current XML element and subtree from the XMLStreamReader. The
method finds the process method in the subclass that has the method name
with the prefix "process" followed by the XML element name, have only an
XMLStreamReader parameter and return an Object (any subclass of
Object can be returned).
For example the process method for the XML element BankAccount would have the following signature.
public BankAccount processBankAccount(XMLStreamReader parser);
For example the process method for the XML element firstName would have the following signature. Note that the part of the method name for the XML element must have the same case as the XML element name.
public String processfirstName(XMLStreamReader parser);
parser - The STAX XML parser.IOException - If an I/O exception occurs.XMLStreamException - If an exception processing the XML occurs.public final void setContext(XmlProcessorContext context)
context - The context for processing the XML Document.Copyright © 2015 Revolution Systems Inc.. All rights reserved.