I was trying to propagate the following xml message to the File Output Node in IBM Integration Bus.
<Resto xmlns = 'https://stackoverflow.com'><Location network_id="5dfweg68h"><Category>Continental</Category></Location></Resto>
Following was my associated Java code to construct the above xml message:
MbElement xmlBody = outMessage.getRootElement().createElementAsLastChild(MbXMLNSC.PARSER_NAME);
MbElement xmlParent = xmlBody.createElementAsLastChild(MbElement.TYPE_NAME, "Resto", null);
xmlParent.createElementAsLastChild(MbXMLNSC.ATTRIBUTE, "xmlns", "https://stackoverflow.com");
MbElement locationParser = xmlParent.createElementAsLastChild(MbElement.TYPE_NAME, "Location", null);
locationParser.createElementAsLastChild(MbXMLNSC.ATTRIBUTE, "network_id", "5dfweg68h");
locationParser.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,"Category","Continental");
On debugging my application, I got XML write errors. I pondered a bit for the necessary solution, and found that adding namespace prefix would solve the problem. Can't it be done without the addition of a prefix? If no, can anyone guide me, in assigning the necessary value by suggesting the necessary changes to be done in Java compute node?
Note: The string assigned to xmlns should be within single quotes in the xml message.
Use constant MbXMLNSC.NAMESPACE_DECLARATION
to declare the namespace:
MbElement xmlBody = outMessage.getRootElement().createElementAsLastChild(MbXMLNSC.PARSER_NAME);
MbElement xmlParent = xmlBody.createElementAsLastChild(MbElement.TYPE_NAME, "Resto", null);
xmlParent.createElementAsFirstChild(MbXMLNSC.NAMESPACE_DECLARATION, "xmlns", "https://stackoverflow.com");
If your XSD defines that the XML elements are qualified, you have to set the namespace explicitly. Examples:
xmlParent.setNamespace("https://stackoverflow.com");
locationParser.setNamespace("https://stackoverflow.com");
locationParser.createElementAsLastChild(MbElement.TYPE_NAME_VALUE,
"Category", "Continental").setNamespace("https://stackoverflow.com");