WARNING: This server provides a static reference view of the NetKernel documentation. Links to dynamic content do not work. For the best experience we recommend you install NetKernel and view the documentation in the live system .

Description:initialise a bean based on an XML definition

The XMLToBean is a transreptor. The following table shows the transreption permutations that it will perform:

From (any of)To (any of)
Import Requirements

To use XMLToBean transreptor you must import the module urn:org:netkernel:xml:core:


The XMLToBean transreptor provides a mechanism for instantiating custom Java beans based upon XML data. This can be useful where a custom Java object is easier to manipulate in code than a general purpose XML object model.


To use the XMLToBean transreptor you will need to create your application specific Java bean. It is important that this class implements the org.netkernel.xml.representation.IBeanFromXML interface. Each property of the bean must be well defined with a getter and setter. Properties of types java.lang String, Integer, Long, Double, Float and Boolean are supported. In this case we will use:

package test.bean;
import org.netkernel.layer0.nkf.NKFException;
import org.netkernel.xml.representation.IBeanFromXML;

public class MyRep implements IBeanFromXML
    private String mValue;
    public String getValue()
    {   return mValue;
    public void setValue(String aValue)
    {   mValue=aValue;

    public void assertInvariant() throws Exception
    {   if (mValue==null)
        {   throw new NKFException("Value must not be null");

The assertInvariant method can be used to assert that the bean is suitably initialised before it is returned by the XMLToBean transreptor. If any problems are found it such be implemented to throw exceptions containing information relating to the cause of the problem.

The transreptor will take the text values of elements to set values on to the bean, parsing text as appropriate to get the required primitive types. By default child elements of the document element are used and their names must match the property names on the bean. The name of the root element is not important. So in this case we can initialise the bean with:

  <value>Hello Bean</value>

By implementing a custom BeanInfo class it is possible to define the XPath of an element whose text value will be used to set the property value. In this case the PropertyDescriptor for that value should have its shortDescription property set to the appropriate XPath.