Altova MapForce 2024 Enterprise Edition

After you generate code from the example schema, a test Java application is created, along with several supporting Altova libraries. Recall that the example schema (Main.xsd) has multiple namespace declarations. Consequently, the generated code includes namespaces that correspond to namespace aliases (prefixes) from the schema, namely: com.Main.ord, com.Main.pur, com.Main.cmn, and com.Main.cust.

 

In general, in order to control XML namespaces and prefixes with the help of the schema wrapper libraries, you have the following methods at your disposal:

 

declareAllNamespacesFromSchema(). Call this method if you want to declare the same namespaces in your XML instance as in the schema. Otherwise, if you need different namespaces as in this example, then declareNamespace() should be used. The method declareAllNamespacesFromSchema() is not used in this example because we specifically want to create XML elements with prefixes that are slightly different from those declared in the schema.

declareNamespace(). Call this method to create or override the existing namespace prefix attribute on an element. The element must already be created using either the append() or appendWithPrefix() methods, as further illustrated below.

appendWithPrefix(). Use this method to append an instance element with a specific prefix. To create the XML instance illustrated in this example, it was sufficient to call this method for the root element only. All other elements were appended using just append(), and their prefixes were added automatically based on their namespaces, according to the rules above.

 

The code listing below shows you how to create an XML document with multiple namespace declarations and prefixed element names. Specifically, it generates a Purchase Order instance as illustrated in the Example: Purchase Order. Importantly, for illustrative purposes, some prefixes are overridden in the XML instance (that is, they are not exactly the same as the ones declared in the schema).

 

protected static void example() throws Exception {
  // Create the XML document and append the root element
  com.Main.pur.Main2 doc = com.Main.pur.Main2.createDocument();
  com.Main.pur.PurchaseType purchase = doc.Purchase.appendWithPrefix("p");
 
  // Set schema location
  doc.setSchemaLocation("Main.xsd");
 
  // Declare namespaces on root element
  purchase.declareNamespace("o", "http://NamespaceTest.com/OrderTypes");
  purchase.declareNamespace("c", "http://NamespaceTest.com/CustomerTypes");
  purchase.declareNamespace("cmn", "http://NamespaceTest.com/CommonTypes");
 
  // Append the OrderDetail element
  com.Main.ord.OrderType order = purchase.OrderDetail.append();
  com.Main.ord.ItemType item = order.Item.append();
  item.ProductName.append().setValue("Lawnmower");
  item.Quantity.append().setValue(1);
  java.math.BigDecimal price = new java.math.BigDecimal("148.42");
  item.UnitPrice.append().setValue(price);
 
  // Append the PaymentMethod element
  com.Main.cmn.PaymentMethodTypeType paymentMethod = purchase.PaymentMethod.append();
  paymentMethod.setEnumerationValue(com.Main.cmn.PaymentMethodTypeType.EVISA);
 
  // Append the CustomerDetails element
  com.Main.cust.CustomerType customer = purchase.CustomerDetails.append();
  customer.Name.append().setValue("Alice Smith");
  com.Main.cmn.AddressType deliveryAddress = customer.DeliveryAddress.append();
  deliveryAddress.Line1.append().setValue("123 Maple Street");
  deliveryAddress.Line2.append().setValue("Mill Valley");
  com.Main.cmn.AddressType billingAddress = customer.BillingAddress.append();
  billingAddress.Line1.append().setValue("8 Oak Avenue");
  billingAddress.Line2.append().setValue("Old Town");
 
  // Save to file
  doc.saveToFile("PurchaseOrder.xml", true);
}

© 2017-2023 Altova GmbH