Creating new elements by using a JavaCompute node

You can use a JavaCompute node to create new elements.

About this task

Use the following methods in a JavaCompute node to create new elements in a message tree:
  • createElementAsFirstChild()
  • createElementAsLastChild()
  • createElementBefore()
  • createElementAfter()
The method returns a reference to the newly-created element. Each method has three overloaded forms:
createElement...(int type)
Creates a blank element of the specified type. Valid generic types are:
  • MbElement.TYPE_NAME. This type of element has only a name, for example an XML element.
  • MbElement.TYPE_VALUE. This type of element has only a value, for example XML text that is not contained in an XML element.
  • MbElement.TYPE_NAME_VALUE. This type of element has both a name and a value, for example an XML attribute.
Specific type values can also be assigned. The meaning of this type information is dependent on the parser. Element name and value information must be assigned by using the setName() and setValue() methods.
createElement...(int type, String name, Object value)
Method for setting the name and value of the element at creation time.
createElement...(String parserName)
A special form of createElement...() that is only used to create top-level parser elements.
This example Java™ code adds a new chapter element to the XML example given in Accessing elements in a message tree from a node:
MbElement root = outMessage.getRootElement();
MbElement document = root.getLastChild().getFirstChild();
MbElement chapter2 = document.createElementAsLastChild(MbElement.TYPE_NAME,"Chapter",null);

// add title attribute
MbElement title2 = chapter2.createElementAsFirstChild(MbElement.TYPE_NAME_VALUE, 
	"title",	"Message Flows");
This produces the following XML output:
<document>
	<chapter title="Introduction">
		Some text.   
	</chapter>
	<chapter title="Message Flows"/> 
</document>