Java DOM 元素,如何找到元素值的实际数据类型?截至目前,一切都被视为 String
Java DOM Element, How to find the actual data type of the Element values? As of now everything considered as String
我的标准中有一个自定义的用户定义部分 XML。像这样:
<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>
XML这部分完全是用户自定义的,可以是任何东西。我正在使用 JAXB 阅读它并且一切正常。
问题是 Dom Element
中的所有值都被视为 String
但正如我们在上面看到的 XML 值可以是不同的数据类型,例如 Date
、Integer
、Float
、Boolean
、String
等
然而,当我使用 element.getTextContent()
读取每个元素的值时,此函数总是 returns String
。有没有办法找到每个 Element
而不是每次 String
的实际数据类型?
我创建了 class 来确定类型:
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;
}
}
}
然后绕过所需数据调用它:
//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());
}
我的标准中有一个自定义的用户定义部分 XML。像这样:
<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>
XML这部分完全是用户自定义的,可以是任何东西。我正在使用 JAXB 阅读它并且一切正常。
问题是 Dom Element
中的所有值都被视为 String
但正如我们在上面看到的 XML 值可以是不同的数据类型,例如 Date
、Integer
、Float
、Boolean
、String
等
然而,当我使用 element.getTextContent()
读取每个元素的值时,此函数总是 returns String
。有没有办法找到每个 Element
而不是每次 String
的实际数据类型?
我创建了 class 来确定类型:
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;
}
}
}
然后绕过所需数据调用它:
//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());
}