Java Architecture for XML Binding (JAXB)
- enables Java applications to work with a model of message data as Java object classes.
- This Java object model can be accessed and manipulated by using getter and setter methods.
Use 4th option while generating java classes
Process via Java Architecture for XML Binding(JAXB) class
---------------------------------------------------------------------------------
The user wants to process the message data using Java object classes that were generated by a Java Architecture for XML Binding (JAXB) binding compiler.
The user can access the Java objects containing the unmarshalled input message and update them or create new Java objects to be marshalled into the output message.
Generated code should be adapted to use specific Java object JAXB classes compiled by binding the message schema.
Methods to initialize the JAX context and optionally copy the message headers are also generated.
generates classes for --
1. one class for data manipulation(main class as starting point).
2. one class for each complex type defined in the schema file.
3. one Object Factory class containing Factory methods for each type definition
Generates getters and setters for each element and attribute defined in the schema.
1. class for data manipulation(main class as starting point) :
contains methods -
a) public void onInitialize() throws MbException {
try {
// Initialize JAXB context with com.mss
// Java object classes that were generated by a Java Architecture for XML
// Binding (JAXB) binding compiler
jaxbContext = JAXBContext.newInstance("com.mss");
} catch (JAXBException e) {
// This exception will cause the deploy of this Java compute node to fail
// Typical cause is the JAXB package above is not available
throw new MbUserException(this, "onInitialize()", "", "",
e.toString(), null);
}
}
b) public void evaluate(MbMessageAssembly inAssembly) throws MbException {
MbOutputTerminal out = getOutputTerminal("out");
MbOutputTerminal alt = getOutputTerminal("alternate");
// obtain the input message data
MbMessage inMessage = inAssembly.getMessage();
// create a new empty output message
MbMessage outMessage = new MbMessage();
MbMessageAssembly outAssembly = new MbMessageAssembly(inAssembly,
outMessage);
// optionally copy input message headers to the new output
copyMessageHeaders(inMessage, outMessage);
try {
// unmarshal the input message data from the Broker tree into your Java object classes
Object inMsgJavaObj = jaxbContext.createUnmarshaller().unmarshal(
inMessage.getDOMDocument());
// ----------------------------------------------------------
// Add user code below to build the new output data by updating
// your Java objects or building new Java objects
// TODO - Replace or modify following which simply copies input to output message
Object outMsgJavaObj = inMsgJavaObj;
// End of user Java object processing
// ----------------------------------------------------------
// TODO set the required Broker domain to for the output message, eg XMLNSC
Document outDocument = outMessage
.createDOMDocument(MbXMLNSC.PARSER_NAME);
// marshal the new or updated output Java object class into the Broker tree
jaxbContext.createMarshaller().marshal(outMsgJavaObj, outDocument);
// The following should only be changed if not propagating message to
// the node's 'out' terminal
out.propagate(outAssembly);
} catch (JAXBException e) {
// Example Exception handling
throw new MbUserException(this, "evaluate()", "", "", e.toString(),
null);
}
}
c) public void copyMessageHeaders(MbMessage inMessage, MbMessage outMessage)
throws MbException {
MbElement outRoot = outMessage.getRootElement();
// iterate though the headers starting with the first child of the root
// element and stopping before the last child (message body)
MbElement header = inMessage.getRootElement().getFirstChild();
while (header != null && header.getNextSibling() != null) {
// copy the header and add it to the out message
outRoot.addAsLastChild(header.copy());
// move along to next header
header = header.getNextSibling();
}
}
Some points to be aware of ---
1. JAXBContext
No comments:
Post a Comment