PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 14, 2022

[FIXED] How do I find the actual data type of the Element values of a Java DOM element? As of now everything considered as String

 October 14, 2022     dom, java, types, xml     No comments   

Issue

I have a custom user-defined section within my standard XML. Something like this:

<rail:JourneyDate>2014-12-12</rail:JourneyDate>
<rail:Name>Rajadhani</rail:Name>
<rail:AxelCount>12</rail:AxelCount>
<rail:VehicleCount>true</rail:VehicleCount>
<rail:PassangerCount>20.5</rail:PassangerCount>

This part of the XML is completely user-defined and can be anything. I am reading it using JAXB and everything is working fine.

The problem is that all the values in the Dom Element are considered as String but as we can see in the above XML the values can be different data types such as Date, Integer, Float, Boolean, String etc.

However when I read the value of each element using element.getTextContent() then this function always returns String. Is there a way to find the actual data type of each Element rather than the String every time?


Solution

I created the class to determine the type:

public class ExtensionsDatatypeFinder {

    private ExtensionsDatatypeFinder() {}

    //Method to check the datatype for user extension, ILMD, Error extensions
    public static Object dataTypeFinder(String textContent) {

        if (textContent.equalsIgnoreCase("true") || textContent.equalsIgnoreCase("false")) {
            //Check if the Element Text content is of Boolean type
            return Boolean.parseBoolean(textContent);
        } else if (NumberUtils.isParsable(textContent)) {
            //Check if the Element Text content is Number type if so determine the Int or Float
            return textContent.contains(".") ? Float.parseFloat(textContent) : Integer.parseInt(textContent);
        } else {
            return textContent;
        }
    }
}

Then called it bypassing the required data:

//Check for the datatype of the Element
                final Object simpleFieldValue = ExtensionsDatatypeFinder.dataTypeFinder((String) extension.getValue());

                //Based on the type of Element value write the value into the JSON accordingly
                if (simpleFieldValue instanceof Boolean) {
                    gen.writeBooleanField(extension.getKey(), (Boolean) simpleFieldValue);
                } else if (simpleFieldValue instanceof Integer) {
                    gen.writeNumberField(extension.getKey(), (Integer) simpleFieldValue);
                } else if (simpleFieldValue instanceof Float) {
                    gen.writeNumberField(extension.getKey(), (Float) simpleFieldValue);
                } else {
                    //If instance is String directly add it to the JSON
                    gen.writeStringField(extension.getKey(), (String) extension.getValue());
                }



Answered By - BATMAN_2008
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing