In this example you can see how we can read xml attribute value as an integer value instead of string. JDOM
provides method such as getIntValue()
, getLongValue()
, getFloatValue()
, getDoubleValue()
to get numerical values. For boolean value we can use the getBooleanValue()
method.
package org.kodejava.example.jdom;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import java.io.IOException;
import java.io.StringReader;
public class JDOMIntegerAttributeValue {
public static void main(String[] args) {
String xml = "<root><table width=\"100\"/></root>";
SAXBuilder builder = new SAXBuilder();
try {
Document document = builder.build(new StringReader(xml));
Element child = document.getRootElement().getChild("table");
int tableWidth = child.getAttribute("width").getIntValue();
System.out.println("tableWidth = " + tableWidth);
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Maven Dependencies
<!-- https://search.maven.org/remotecontent?filepath=org/jdom/jdom2/2.0.6/jdom2-2.0.6.jar -->
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.6</version>
</dependency>
Latest posts by Wayan (see all)
- How do I create a generic class in Java? - January 1, 2021
- How do I convert java.util.TimeZone to java.time.ZoneId? - April 25, 2020
- How do I get a list of all TimeZones Ids using Java 8? - April 25, 2020