Search code examples
javaxmlibm-integration-bus

IBM Integration Bus not generating xml attribute in JavaCompute node


I'm creating a new XML document in a JavaCompute node following the instructions at IBM Knowledge Center. The problem is the output is nothing similiar to the documentation

My code is

MbElement itemMaintance = body.createElementAsLastChild(MbElement.TYPE_NAME, "ItemMaintance", null);

MbElement item = itemMaintance.createElementAsLastChild(MbElement.TYPE_NAME, "ItemMaintance", null);
MbElement action = item.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Action", "ACTION");
MbElement serialNumberFlag = item.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "serialNumberFlag", "0123456789");

And the output is

<ItemMaintance>
    <Item>
        <Action>ACTION</Action>
        <SerialNumberFlag>0123456789</SerialNumberFlag>
    </Item>
</ItemMaintance>

It should be

<ItemMaintance>
    <Item Action="ACTION" SerialNumberFlag="0123456789"/>
</ItemMaintance>

What am I missing?


Solution

  • You should use MbXMLNSC instead of MbElement as element type for XML attributes:

    MbElement root = outMessage.getRootElement();
    MbElement body = root.createElementAsLastChild( MbXMLNSC.PARSER_NAME);  
    MbElement itemMaintance = body.createElementAsLastChild( MbElement.TYPE_NAME, "ItemMaintance", null);
    
    MbElement item = itemMaintance.createElementAsLastChild( MbElement.TYPE_NAME, "Item", null);
    MbElement action = item.createElementAsLastChild( MbXMLNSC.ATTRIBUTE, "Action", "ACTION");
    MbElement serialNumberFlag = item.createElementAsLastChild( MbXMLNSC.ATTRIBUTE, "serialNumberFlag", "0123456789");
    

    This page explains how to do it: https://www.ibm.com/support/knowledgecenter/en/SSMKHH_10.0.0/com.ibm.etools.mft.doc/ac67180_.htm