0

I am trying to use Smooks with Groovy to appendChild() to elements. So far I've been unable to do so.

Sample input file

<?xml version="1.0"?>
<orders>
    <order res="0">
        <header>
            <date>Wed Nov 27 13:45:28 EST 2013</date>
            <customer number="9404089839">Harish Nanda M</customer>
        </header>
        <orderitems>
            <orderitem>
                <product>007</product>
                <quantity>7</quantity>
                <price>7.90</price>
            </orderitem>
            <orderitem>
                <product>005</product>
                <quantity>3</quantity>
                <price>7.20</price>
            </orderitem>
        </orderitems>
    </order>
    <order res="0">
        <header>
            <date>Wed Nov 27 13:45:28 EST 2013</date>
            <customer number="2036139296">Sandesh G</customer>
        </header>
        <orderitems>
            <orderitem>
                <product>0023</product>
                <quantity>87</quantity>
                <price>57.90</price>
            </orderitem>
            <orderitem>
                <product>005</product>
                <quantity>3</quantity>
                <price>7.20</price>
            </orderitem>
        </orderitems>
    </order>
</orders>

Smooks config file

<?xml version="1.0"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd" xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.3.xsd" xmlns:g="http://www.milyn.org/xsd/smooks/groovy-1.1.xsd">
    <core:filterSettings type="SAX"/>
    <g:groovy executeOnElement="order">
        <g:imports>
            import org.milyn.xml.DomUtils;  
            import org.milyn.javabean.context.BeanContext;  
            import org.milyn.javabean.repository.BeanRepository;    
            import org.w3c.dom.*;
            import groovy.xml.dom.DOMCategory;  
            import groovy.xml.dom.DOMUtil;
            import groovy.xml.DOMBuilder;   
            import groovy.util.XmlSlurper;
        </g:imports>
        <g:script>
            <!--    javax.xml.transform.dom.DOMSource source = new javax.xml.transform.dom.DOMSource(element); 
javax.xml.transform.Transformer transformer = javax.xml.transform.TransformerFactory.newInstance().newTransformer();
    javax.xml.transform.stream.StreamResult result = new
 javax.xml.transform.stream.StreamResult(new java.io.StringWriter()); 
    transformer.transform(source, result);  String ss = 
 result.getWriter().toString();     def ord = new
 XmlSlurper().parseText(ss)     //ord.xxxx = "2222";    println
 ord.orderitems.orderitem.product.text()*.toInteger().sum();
element.appendChild("res");     //element['@res'] = element.'@res'.toInteger() +    
 ord.orderitems.orderitem.product.text()*.toInteger().sum();
writeFragment(element);
-->
        </g:script>
    </g:groovy>
</smooks-resource-list>
Emmanuel Rosa
  • 9,697
  • 2
  • 14
  • 20
  • 1
    Please clear your request – lifeisfoo Aug 31 '15 at 16:29
  • 1
    @govardhan-kamle, posting code that's executable in the groovy console would be helpful. I would like to help, but I don't use Smooks. To be frank with you, it's just not worth my time to figure out how to get Smooks up and running. But I can troubleshoot executable code :) – Emmanuel Rosa Aug 31 '15 at 21:25
  • Would be clearer if you trimmed down the sample message you are using here and also showed the type of markup you are trying to produce and why you are using groovy e.g. do you need to perform some sort of calculations as part of the transform? – Tom Fennelly Sep 01 '15 at 11:37
  • Also, there's a smooks scripting example using groovy here: http://www.smooks.org/mediawiki/index.php?title=V1.5:groovy – Tom Fennelly Sep 01 '15 at 11:38
  • yes got it. Thanks a lot – govardhan kamle Sep 02 '15 at 05:39
  • Actually I am trying to calculate expression in groovy and add the result to same xml element – govardhan kamle Sep 02 '15 at 05:43

0 Answers0