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 .

Endpoint
Name:XMLToBean
Description:initialise a bean based on an XML definition
Id:apache.XMLToBean
Category:transreptor

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

From (any of)To (any of)
java.lang.ObjectIRepAny
IBeanFromXML
Import Requirements

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

<import>
  <uri>urn:org:netkernel:xml:core</uri>
</import>

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.

Example

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;
    }

    @Override
    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:

<MyBeanData>
  <value>Hello Bean</value>
</MyBeanData>

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.