A Message DOM Object is created from the XML String.
Document Object Model is a platform- and language-neutral interface that will allow programs and scripts to dynamically access and update the content, structure and style of documents. The document can be further processed and the results of that processing can be incorporated back into the presented page.
public Message() {
doc = new DocumentImpl();
elementMessage = doc.createElement("message");
elementHeader = doc.createElement("header");
elementStatus = doc.createElement("status");
elementHeader.appendChild(elementStatus);
elementCommand = doc.createElement("command");
elementHeader.appendChild(elementCommand);
elementQueueName = doc.createElement("queueName");
elementHeader.appendChild(elementQueueName);
elementMessageId = doc.createElement("messageId");
elementHeader.appendChild(elementMessageId);
elementProcessingRule = doc.createElement("processingRule");
elementHeader.appendChild(elementProcessingRule);
elementMessage.appendChild(elementHeader);
elementPayload = doc.createElement("payload");
elementMessage.appendChild(elementPayload);
doc.appendChild(elementMessage);
}
public Message(String msg) {
InputSource in = new InputSource(new StringReader(msg));
DOMParser parser = new DOMParser();
try {
parser.parse(in);
} catch (Exception e) {
System.out.println("Message:Message:1:Unable to
create DOM object");
}
doc = parser.getDocument();
}
public String getStatus() {
NodeList nl = doc.getElementsByTagName("status");
Node n = nl.item(0).getFirstChild();
if (n == null) {
return "";
} else {
return n.getNodeValue();
}
}
public void setStatus(String s) {
setHeaderElement("status", s);
}
public void setHeaderElement(String elementName, String elementValue) {
Node elementHeader = getXMLElement("header");
Element oldElement = getXMLChildElement("header", elementName);
elementHeader.removeChild(oldElement);
Element newElement = doc.createElement(elementName);
elementHeader.appendChild(newElement);
newElement.appendChild(doc.createTextNode(elementValue));
}