Accessing simple types through XML facade

This recipe explains how to access simple types through XML facade.

Getting ready

To complete the recipe, we will amend the example BPEL process from our previous recipe, Accessing complex types through XML facade.

How to do it...

  1. Open the Banking_BPEL process and double-click on the XML_facade_node Java Embedding activity.
  2. We amend the code snippet with the code to access the XML simple types:
    for (org.packt.cashflowfacade.PrincipalExchange pe: princEx) {
      addAuditTrailEntry("Received cashflow with id: " + pe.getId() + "
    " +
                "  Unadj. Principal Exch. Date ...: " + pe.getUnadjustedPrincipalExchangeDate() + "
    " +
                "  Adj. Principal Exch. Date .....: " + pe.getAdjustedPrincipalExchangeDate() + "
    " +              
                "  Discount factor ...............: " + pe.getDiscountFactor() + "
    " +
                "  Principal Exch. Amount ........: " + pe.getPrincipalExchangeAmount() + "
    "              
                ); 
    }          
  3. With the preceding code, we output all Java class members to the audit trail. Now if we run the BPEL process, we can see the following part of output in the BPEL flow trace:
    How to do it...

How it works...

The XML schema simple types are mapped to Java classes as members. If we check our example, we have three simple types in the XML schema:

<xsd:complexType name="PrincipalExchange">
  <xsd:sequence>
   <xsd:element minOccurs="0" name="unadjustedPrincipalExchangeDate" type="xsd:date"/>     
   <xsd:element minOccurs="0" name="adjustedPrincipalExchangeDate" type="xsd:date"/>
   <xsd:element minOccurs="0" name="principalExchangeAmount" type="xsd:decimal"/>
   <xsd:element minOccurs="0" name="discountFactor" type="xsd:decimal"/>
  </xsd:sequence>
  <xsd:attribute name="id" type="xsd:int"/>
</xsd:complexType>    

The simple types defined in the XML schema are <xsd:date>, <xsd:decimal>, and <xsd:int>. Let us find the corresponding Java class member definitions. Open the PrincipalExchange.java file. The definition of members we can see is as follows:

@XmlSchemaType(name = "date")
protected XMLGregorianCalendar unadjustedPrincipalExchangeDate;
@XmlSchemaType(name = "date")
protected XMLGregorianCalendar adjustedPrincipalExchangeDate;
protected BigDecimal principalExchangeAmount;
protected BigDecimal discountFactor;
@XmlAttribute
protected Integer id;

We can see that the mapping between the XML content and the Java classes was performed as shown in the following table:

XML schema simple type

Java class member

<xsd:date>

javax.xml.datatype.XMLGregorianCalendar

<xsd:decimal>

java.math.BigDecimal

<xsd:int>

java.lang.Integer

Also, we can identify that the XML simple type definitions as well as the XML attributes are always mapped as members in corresponding Java class representations.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset