Ce contenu n'est pas disponible dans la langue sélectionnée.
1.7. Example of Using SAX
Prerequisites
- Requires an implemented SAXVisitor interface. (Choose an interface that corresponds to the events of the process.)
- This example uses the
ExecutionContext
name. It is a public interface which extends theBoundAttributeStore
class.
Procedure 1.1. Task
- Create a new Smooks configuration. This will be used to apply the visitor logic at the <xxx> element's
visitBefore
andvisitAfter
events. - Apply the logic at the
visitBefore
andvisitAfter
events in a specific element of the overall event stream. The visitor logic is applied to the events in the <xxx> element. - Use Smooks with FreeMarker to perform an XML-to-XML transformation on a huge message.
- Insert the following source format:
<order id='332'> <header> <customer number="123">Joe</customer> </header> <order-items> <order-item id='1'> <product>1</product> <quantity>2</quantity> <price>8.80</price> </order-item> <!-- etc etc --> </order-items> </order>
- Insert this target format:
<salesorder> <details> <orderid>332</orderid> <customer> <id>123</id> <name>Joe</name> </customer> <details> <itemList> <item> <id>1</id> <productId>1</productId> <quantity>2</quantity> <price>8.80</price> <item> <!-- etc etc --> </itemList> </salesorder>
- Use this Smooks configuration:
<?xml version="1.0"?> <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd" xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd"> <!-- Filter the message using the SAX Filter (i.e. not DOM, so no intermediate DOM for the "complete" message - there are "mini" DOMs for the NodeModels below).... --> <params> <param name="stream.filter.type">SAX</param> <param name="default.serialization.on">false</param> </params> <!-- Create 2 NodeModels. One high level model for the "order" (header etc) and then one per "order-item". These models are used in the FreeMarker templating resources defined below. You need to make sure you set the selector such that the total memory footprint is as low as possible. In this example, the "order" model will contain everything accept the <order-item> data (the main bulk of data in the message). The "order-item" model only contains the current <order-item> data (i.e. there's max 1 order-item in memory at any one time). --> <resource-config selector="order,order-item"> <resource>org.milyn.delivery.DomModelCreator</resource> </resource-config> <!-- Apply the first part of the template when we reach the start of the <order-items> element. Apply the second part when we reach the end. Note the <?TEMPLATE-SPLIT-PI?> Processing Instruction in the template. This tells Smooks where to split the template, resulting in the order-items being inserted at this point. --> <ftl:freemarker applyOnElement="order-items"> <ftl:template><!--<salesorder> <details> <orderid>${order.@id}</orderid> <customer> <id>${order.header.customer.@number}</id> <name>${order.header.customer}</name> </customer> </details> <itemList> <?TEMPLATE-SPLIT-PI?> </itemList> </salesorder>--></ftl:template> </ftl:freemarker> <!-- Output the <order-items> elements. This will appear in the output message where the <?TEMPLATE-SPLIT-PI?> token appears in the order-items template. --> <ftl:freemarker applyOnElement="order-item"> <ftl:template><!-- <item> <id>${.vars["order-item"].@id}</id> <productId>${.vars["order-item"].product}</productId> <quantity>${.vars["order-item"].quantity}</quantity> <price>${.vars["order-item"].price}</price> </item> --></ftl:template> </ftl:freemarker> </smooks-resource-list>
- Use this code to execute:
Smooks smooks = new Smooks("smooks-config.xml"); try { smooks.filterSource(new StreamSource(new FileInputStream("input-message.xml")), new StreamResult(System.out)); } finally { smooks.close(); }
- An XML-to-XML transformation occurs as a result.