|
Handling element attributes with JDOM |
|
|
JDOM provides high level and very convenient ways to get values from XML sources.
For example, the values from attributes can be taken as the concrete data type, like int.
This avoids a programer from numerous cast and pars operations.
In our purchase-order example we have an attribute 'number'. Below sample shows how
to get its value immediately as an integral number:
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.DOMBuilder;
/**
* This sample program showing how it is easy to narrow
* data values in JDOM document.
*/
public class Test {
public static void main(String[] args) {
try {
// a builder takes a boolean value meaning validation mode:
DOMBuilder builder = new DOMBuilder(false);
// simply load the document::
Document document = builder.build(
new java.io.File("sample.xml"));
Element order = document.getRootElement();
// get the number of the order:
Attribute orderNumber = order.getAttribute("number");
if (orderNumber != null) { // is it present ?
int number = orderNumber.getIntValue();
// ... do something with this integer ...
}
// .. do something else ...
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.