Quantcast

Converting XmlSlurper node value to BigDecimal

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Converting XmlSlurper node value to BigDecimal

jensriis
I am using groovy to read an xml file:

def order = new XmlSlurper().parse(filename)

i then need to use the value of a tag as part of a SQL WHERE clause e.g

order.Order_Header.Order_Line.each
{
    def line = it.Line_Number as String // as BigDecimal
    def bdline = line as BigDecimal

    log.info(line)
    rows = bakerssql.firstRow("select CPRO, OQTY, LSEQ from  ETPSO100 where ORNO = '${orderId}' AND LSEQ = '${bdline}'")


the LSEQ column is of type NUMBER.  

Executing the code gives the error:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '1' with class 'groovy.util.slurpersupport.NodeChildren' to class 'java.lang.Number' as if the cast i am applying has no effect. As this is my first Groovy program and i simply can't get my head around how to do the conversion, hence any help would be appreciated.




Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Converting XmlSlurper node value to BigDecimal

Guillaume Laforge-4
I think you should get the text() on the node, before doing as BigDecimal.
So you could try: it.Line_Number.text() as BigDecimal

On Thu, Mar 5, 2009 at 18:05, jensriis <[hidden email]> wrote:

I am using groovy to read an xml file:

def order = new XmlSlurper().parse(filename)

i then need to use the value of a tag as part of a SQL WHERE clause e.g

order.Order_Header.Order_Line.each
{
   def line = it.Line_Number as String // as BigDecimal
   def bdline = line as BigDecimal

   log.info(line)
   rows = bakerssql.firstRow("select CPRO, OQTY, LSEQ from  ETPSO100 where
ORNO = '${orderId}' AND LSEQ = '${bdline}'")

the LSEQ column is of type NUMBER.

Executing the code gives the error:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast
object '1' with class 'groovy.util.slurpersupport.NodeChildren' to class
'java.lang.Number' as if the cast i am applying has no effect. As this is my
first Groovy program and i simply can't get my head around how to do the
conversion, hence any help would be appreciated.





--
View this message in context: http://www.nabble.com/Converting-XmlSlurper-node-value-to-BigDecimal-tp22355954p22355954.html
Sent from the groovy - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email





--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Converting XmlSlurper node value to BigDecimal

jensriis

Guillaume Laforge-2 wrote
I think you should get the text() on the node, before doing as BigDecimal.
So you could try: it.Line_Number.text() as BigDecimal

On Thu, Mar 5, 2009 at 18:05, jensriis <jens@agritursantalucia.it> wrote:

>
> I am using groovy to read an xml file:
>
> def order = new XmlSlurper().parse(filename)
>
> i then need to use the value of a tag as part of a SQL WHERE clause e.g
>
> order.Order_Header.Order_Line.each
> {
>    def line = it.Line_Number as String // as BigDecimal
>    def bdline = line as BigDecimal
>
>    log.info(line)
>    rows = bakerssql.firstRow("select CPRO, OQTY, LSEQ from  ETPSO100 where
> ORNO = '${orderId}' AND LSEQ = '${bdline}'")
>
> the LSEQ column is of type NUMBER.
>
> Executing the code gives the error:
>
> org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast
> object '1' with class 'groovy.util.slurpersupport.NodeChildren' to class
> 'java.lang.Number' as if the cast i am applying has no effect. As this is
> my
> first Groovy program and i simply can't get my head around how to do the
> conversion, hence any help would be appreciated.
>
>
>
>
>
> --
> View this message in context:
> http://www.nabble.com/Converting-XmlSlurper-node-value-to-BigDecimal-tp22355954p22355954.html
> Sent from the groovy - user mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>


--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one
Thanks Guillaume

Unfortunately this does not change anything, anyway i would have thought that the assignment

      def line = it.Line_Number as String
      def bdline = line as BigDecimal

would implicitly have called the text()

if I print the line i get the result:
Thu Mar 05 21:57:31 CET 2009:INFO:line as string = 1

the statement:
  rows = bakerssql.firstRow("select CPRO, OQTY, LSEQ from  ETPSO100 where
> ORNO = '${orderId}' AND LSEQ = '${bdline}'")


still fails as if the variable  bdline  has a  type of groovy.util.slurpersupport.NodeChildren i would expect the error when carrying out the actual cast in the lines above the failing statement.

Please let me know if you have other suggestions



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Converting XmlSlurper node value to BigDecimal

Guillaume Laforge-4
Perhaps if you could show what your XML looked like, that would help.
You may not be selecting the right node you think you're selecting.
"Thu Mar 05 21:57:31 CET 2009:INFO:line as string = 1" looks like a concatenation of nodes' content.

On Thu, Mar 5, 2009 at 22:07, jensriis <[hidden email]> wrote:



Guillaume Laforge-2 wrote:
>
> I think you should get the text() on the node, before doing as BigDecimal.
> So you could try: it.Line_Number.text() as BigDecimal
>
> On Thu, Mar 5, 2009 at 18:05, jensriis <[hidden email]> wrote:
>
>>
>> I am using groovy to read an xml file:
>>
>> def order = new XmlSlurper().parse(filename)
>>
>> i then need to use the value of a tag as part of a SQL WHERE clause e.g
>>
>> order.Order_Header.Order_Line.each
>> {
>>    def line = it.Line_Number as String // as BigDecimal
>>    def bdline = line as BigDecimal
>>
>>    log.info(line)
>>    rows = bakerssql.firstRow("select CPRO, OQTY, LSEQ from  ETPSO100
>> where
>> ORNO = '${orderId}' AND LSEQ = '${bdline}'")
>>
>> the LSEQ column is of type NUMBER.
>>
>> Executing the code gives the error:
>>
>> org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast
>> object '1' with class 'groovy.util.slurpersupport.NodeChildren' to class
>> 'java.lang.Number' as if the cast i am applying has no effect. As this is
>> my
>> first Groovy program and i simply can't get my head around how to do the
>> conversion, hence any help would be appreciated.
>>
>>
>>
>>
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Converting-XmlSlurper-node-value-to-BigDecimal-tp22355954p22355954.html
>> Sent from the groovy - user mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>>
>
>
> --
> Guillaume Laforge
> Groovy Project Manager
> Head of Groovy Development at SpringSource
> http://www.springsource.com/g2one
>
>

Thanks Guillaume

Unfortunately this does not change anything, anyway i would have thought
that the assignment

     def line = it.Line_Number as String
     def bdline = line as BigDecimal

would implicitly have called the text()

if I print the line i get the result:
Thu Mar 05 21:57:31 CET 2009:INFO:line as string = 1

the statement:
 rows = bakerssql.firstRow("select CPRO, OQTY, LSEQ from  ETPSO100 where
> ORNO = '${orderId}' AND LSEQ = '${bdline}'")

still fails as if the variable  bdline  has a  type of
groovy.util.slurpersupport.NodeChildren i would expect the error when
carrying out the actual cast in the lines above the failing statement.

Please let me know if you have other suggestions




--
View this message in context: http://www.nabble.com/Converting-XmlSlurper-node-value-to-BigDecimal-tp22355954p22360656.html
Sent from the groovy - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email





--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Converting XmlSlurper node value to BigDecimal

jensriis
The line printing the
Thu Mar 05 21:57:31 CET 2009:INFO:line as string = 1"

looks like

    def line = it.Line_Number.text() as BigDecimal
    log.info("line as string = " +line)


the '1' is exactly what i expect the xml to contain, anyway the relavant part of the xml looks like:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://dev.gistworld.com/schema/orders/v20071018/" >
  <Message_Header>
    <Sender>
      <Sender_ID>P & O PLC</Sender_ID>
      <Sender_Qualifier>1502120687025502</Sender_Qualifier>
    </Sender>
    <Receiver>
      <Receiver_ID>VIA TRADAN</Receiver_ID>
      <Receiver_Qualifier>2565050286000015</Receiver_Qualifier>
    </Receiver>
    <Control_Data>
      <Send_Date_Time>2009-02-05T05:13:09</Send_Date_Time>
      <Document_Reference>FO3148</Document_Reference>
    </Control_Data>
  </Message_Header>
  <Order_Header>
    <Order_ID>234567</Order_ID>
    <Order_Type>F</Order_Type>
    <Order_Date>2009-02-05T06:20:59</Order_Date>
    <Message_Purpose>C</Message_Purpose>
    <Client_ID>std</Client_ID>
    <Owner_ID>xxxxx</Owner_ID>
    <Buyer_Information>
      <Address_ID>5012068025502</Address_ID>
      <Address_Name>ADDR1</Address_Name>
    </Buyer_Information>
    <Seller_Information>
      <Address_ID>4706B</Address_ID>
    </Seller_Information>
    <Delivery_Location>
      <Address_ID>BB</Address_ID>
      <Address_Name>ADDR2</Address_Name>
    </Delivery_Location>
    <Delivery_Window>
      <Delivery_Date_Earliest>2009-02-09T00:00:00</Delivery_Date_Earliest>
      <Delivery_Date_Latest>2009-02-09T00:00:00</Delivery_Date_Latest>
    </Delivery_Window>
    <Transport_Mode>Undefined</Transport_Mode>
    <Instructions>MORNING</Instructions>
    <Order_Line>
      <Line_Number>1</Line_Number>
      <Product_Data>
        <SKU_ID>200001</SKU_ID>
        <Item_Description>FLAKES</Item_Description>
        <Order_Qty>1000</Order_Qty>
      </Product_Data>
      <Currency_Code>EUR</Currency_Code>
      <User_Defined_Fields>
        <UDFL2>F19A</UDFL2>
        <UDFL3>8</UDFL3>
        <UDFL4>0.000000</UDFL4>
      </User_Defined_Fields>
    </Order_Line>
    <Order_Line>
      <Line_Number>2</Line_Number>
      <Product_Data>
        <SKU_ID>200002</SKU_ID>
        <Item_Description>CORNFLAKES</Item_Description>
        <Order_Qty>2000</Order_Qty>
      </Product_Data>
      <Currency_Code>GBP</Currency_Code>
      <User_Defined_Fields>
        <UDFL2>F19A</UDFL2>
        <UDFL3>12</UDFL3>
        <UDFL4>0.000000</UDFL4>
      </User_Defined_Fields>
    </Order_Line>
    <Order_Line>
      <Line_Number>3</Line_Number>
      <Product_Data>
        <SKU_ID>200003</SKU_ID>
        <Item_Description>FRUIT & FLAKE</Item_Description>
        <Order_Qty>3000</Order_Qty>
      </Product_Data>
      <Currency_Code>GBP</Currency_Code>
      <User_Defined_Fields>
        <UDFL2>F19A</UDFL2>
        <UDFL3>10</UDFL3>
        <UDFL4>0.000000</UDFL4>
      </User_Defined_Fields>
    </Order_Line>
    <Order_Line>
      <Line_Number>4</Line_Number>
      <Product_Data>
        <SKU_ID>200004</SKU_ID>
        <Item_Description>COUNT-ON-US 500G</Item_Description>
        <Order_Qty>4000</Order_Qty>
      </Product_Data>
      <Currency_Code>GBP</Currency_Code>
      <User_Defined_Fields>
        <UDFL2>F19A</UDFL2>
        <UDFL3>10</UDFL3>
        <UDFL4>0.000000</UDFL4>
      </User_Defined_Fields>
    </Order_Line>
 


Guillaume Laforge-2 wrote
Perhaps if you could show what your XML looked like, that would help.
You may not be selecting the right node you think you're selecting.
"Thu Mar 05 21:57:31 CET 2009:INFO:line as string = 1" looks like a
concatenation of nodes' content.

On Thu, Mar 5, 2009 at 22:07, jensriis <jens@agritursantalucia.it> wrote:

>
>
>
> Guillaume Laforge-2 wrote:
> >
> > I think you should get the text() on the node, before doing as
> BigDecimal.
> > So you could try: it.Line_Number.text() as BigDecimal
> >
> > On Thu, Mar 5, 2009 at 18:05, jensriis <jens@agritursantalucia.it>
> wrote:
> >
> >>
> >> I am using groovy to read an xml file:
> >>
> >> def order = new XmlSlurper().parse(filename)
> >>
> >> i then need to use the value of a tag as part of a SQL WHERE clause e.g
> >>
> >> order.Order_Header.Order_Line.each
> >> {
> >>    def line = it.Line_Number as String // as BigDecimal
> >>    def bdline = line as BigDecimal
> >>
> >>    log.info(line)
> >>    rows = bakerssql.firstRow("select CPRO, OQTY, LSEQ from  ETPSO100
> >> where
> >> ORNO = '${orderId}' AND LSEQ = '${bdline}'")
> >>
> >> the LSEQ column is of type NUMBER.
> >>
> >> Executing the code gives the error:
> >>
> >> org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot
> cast
> >> object '1' with class 'groovy.util.slurpersupport.NodeChildren' to class
> >> 'java.lang.Number' as if the cast i am applying has no effect. As this
> is
> >> my
> >> first Groovy program and i simply can't get my head around how to do the
> >> conversion, hence any help would be appreciated.
> >>
> >>
> >>
> >>
> >>
> >> --
> >> View this message in context:
> >>
> http://www.nabble.com/Converting-XmlSlurper-node-value-to-BigDecimal-tp22355954p22355954.html
> >> Sent from the groovy - user mailing list archive at Nabble.com.
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe from this list, please visit:
> >>
> >>    http://xircles.codehaus.org/manage_email
> >>
> >>
> >>
> >
> >
> > --
> > Guillaume Laforge
> > Groovy Project Manager
> > Head of Groovy Development at SpringSource
> > http://www.springsource.com/g2one
> >
> >
>
> Thanks Guillaume
>
> Unfortunately this does not change anything, anyway i would have thought
> that the assignment
>
>      def line = it.Line_Number as String
>       def bdline = line as BigDecimal
>
> would implicitly have called the text()
>
> if I print the line i get the result:
> Thu Mar 05 21:57:31 CET 2009:INFO:line as string = 1
>
> the statement:
>   rows = bakerssql.firstRow("select CPRO, OQTY, LSEQ from  ETPSO100 where
> > ORNO = '${orderId}' AND LSEQ = '${bdline}'")
>
> still fails as if the variable  bdline  has a  type of
> groovy.util.slurpersupport.NodeChildren i would expect the error when
> carrying out the actual cast in the lines above the failing statement.
>
> Please let me know if you have other suggestions
>
>
>
>
> --
> View this message in context:
> http://www.nabble.com/Converting-XmlSlurper-node-value-to-BigDecimal-tp22355954p22360656.html
> Sent from the groovy - user mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>


--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one
Loading...