Jaxb conversion of number with comma as a decimal separator

BlueLettuce16

I am using Jaxb to parse xml. In the xml file I have list of items defined as follows:

<item>
   <currency_name>US Dollar</currency_code>
   <currency_code>USD</currency_code>
   <rate>3,0223</rate>
</item>

When I put in my xsd file the following line:

<xsd:element name="rate" type="xsd:string" minOccurs="1" maxOccurs="1"/>

I get 3,0223 as a string value, however when I put:

<xsd:element name="rate" type="xsd:double/BigDecimal/float" minOccurs="1" maxOccurs="1"/>

then I recieve 0.0/null. I think that the problem is in the "," separator. How to unmarshall rate value to BigDecimal?

bdoughan

If you are generating your model from XML Schema, below is an approach you can use to cause an XmlAdapter to be generated for your use case.

Formatter Object

You will need to create a class with methods that can convert the string to/from BigDecimal.

public class BigDecimalFormatter {

    public static String printBigDecimal(BigDecimal value) {
        // TODO - Conversion logic
    }

    public static BigDecimal parseBigDecimal(String value) {
        // TODO - Conversion logic
    }

}

External Bindings Document (bindings.xml)

An external binding document is used to specify that your custom conversion class should be used when converting the XML string to/from your BigDecimal property.

<jxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb" version="2.1">
    <jxb:bindings schemaLocation="schema.xsd">
        <jxb:bindings node="//xs:element[@name='rate']">
            <jxb:property>
                <jxb:baseType>
                    <jxb:javaType name="java.math.BigDecimal"
                        parseMethod="forum20711223.BigDecimalFormatter.parseBigDecimal" printMethod="forum20711223.BigDecimalFormatter.printBigDecimal" />
                </jxb:baseType>
            </jxb:property>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

XJC Call

The -b flag is used to reference the external binding document.

xjc -b binding.xml schema.xsd

Full Example

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

jQuery Validation Plugin: validate decimal number with comma as decimal separator

From Dev

formatting decimal number to string with comma as decimal separator in racket

From Dev

Masked EditText with comma as decimal separator

From Dev

seq uses comma as decimal separator

From Dev

Format a decimal with comma as decimal separator and without thousands separator

From Dev

Regex a decimal number with comma

From Dev

Regex a decimal number with comma

From Dev

How to format number with "." as thousand separator, and "," as decimal separator?

From Dev

Why django uses a comma as decimal separator

From Dev

How to make InvariantCulture recognize a comma as a decimal separator?

From Dev

Show comma instead of point as decimal separator

From Dev

NumericUpDown: accept both comma and dot as decimal separator

From Dev

Convert a double to 2 decimal places with comma separator

From Dev

Read txt file with comma decimal separator in MATLAB

From Dev

Parsing numbers with a comma decimal separator in JavaScript

From Dev

Only add thousand separator before decimal comma

From Dev

NumericUpDown: accept both comma and dot as decimal separator

From Dev

Convert a double to 2 decimal places with comma separator

From Dev

Show comma instead of point as decimal separator

From Dev

Read txt file with comma decimal separator in MATLAB

From Dev

bc: decimal separator comma vs. point

From Dev

Add comma separator to a numbers with 2 decimal points

From Dev

Google form regex for numbers with comma as decimal separator

From Dev

How to convert number into decimal with shifted decimal separator?

From Dev

Pasting decimal numbers in excel / comma and point decimal separator

From Dev

Treat Comma and Period as Decimal Separator in Navision Decimal Fields

From Dev

r ggplot with zeroes and no comma as the big number separator

From Dev

Space separator INSTEAD of a comma - large number outputs

From Dev

Remove grouping separator from a decimal number in Android

Related Related

  1. 1

    jQuery Validation Plugin: validate decimal number with comma as decimal separator

  2. 2

    formatting decimal number to string with comma as decimal separator in racket

  3. 3

    Masked EditText with comma as decimal separator

  4. 4

    seq uses comma as decimal separator

  5. 5

    Format a decimal with comma as decimal separator and without thousands separator

  6. 6

    Regex a decimal number with comma

  7. 7

    Regex a decimal number with comma

  8. 8

    How to format number with "." as thousand separator, and "," as decimal separator?

  9. 9

    Why django uses a comma as decimal separator

  10. 10

    How to make InvariantCulture recognize a comma as a decimal separator?

  11. 11

    Show comma instead of point as decimal separator

  12. 12

    NumericUpDown: accept both comma and dot as decimal separator

  13. 13

    Convert a double to 2 decimal places with comma separator

  14. 14

    Read txt file with comma decimal separator in MATLAB

  15. 15

    Parsing numbers with a comma decimal separator in JavaScript

  16. 16

    Only add thousand separator before decimal comma

  17. 17

    NumericUpDown: accept both comma and dot as decimal separator

  18. 18

    Convert a double to 2 decimal places with comma separator

  19. 19

    Show comma instead of point as decimal separator

  20. 20

    Read txt file with comma decimal separator in MATLAB

  21. 21

    bc: decimal separator comma vs. point

  22. 22

    Add comma separator to a numbers with 2 decimal points

  23. 23

    Google form regex for numbers with comma as decimal separator

  24. 24

    How to convert number into decimal with shifted decimal separator?

  25. 25

    Pasting decimal numbers in excel / comma and point decimal separator

  26. 26

    Treat Comma and Period as Decimal Separator in Navision Decimal Fields

  27. 27

    r ggplot with zeroes and no comma as the big number separator

  28. 28

    Space separator INSTEAD of a comma - large number outputs

  29. 29

    Remove grouping separator from a decimal number in Android

HotTag

Archive