XMLBeans - Spare me the parsing and mapping !
I have been using Castor(http://www.castor.org) for a while to translate XML to JavaBeans vice versa. It was ok and reliable but our XML got complicated, it was tedious and cumbersome to maintain the XML Mapping file. Our mapping file keeps growing bigger as our objects gets more complicated. On top of that we have to maintain our XML schemas as well. So we have to maintain the XML mapping file and the XML schema. Then i stumbled upon XMLBeans. XMLBeans got me hooked just by not having to maintain a mapping file. Here are the few easy steps that i followed when i used XMLBeans.
1.) Download and Install XMLBeans (http://xmlbeans.apache.org/). I got version 2.1.0
2.) Create the XML Schema
3.) Do a SCOMP (Please refer to XMLBeans documentation)
4.) Once the XMLBeans jar for my XML Schema was created then it was ready to use.
Here is a sample Code:
Sample Code processing an XML Message to an Object
xmlTest is the XML Message in String
InstructorQualification is the root element of the XML based on the XML Schema.
1 InstructorQualificationDocument iqDoc = null;
2 ArrayList validationErrors = new ArrayList();
3 XmlOptions validationOptions = new XmlOptions();
4 validationOptions.setErrorListener(validationErrors);
5 iqDoc = InstructorQualificationDocument.Factory.parse(xmlTest);
6 boolean isValid = iqDoc.validate(validationOptions);
7 if(!isValid){
8 Iterator iter = validationErrors.iterator();
9 while (iter.hasNext())
10 {
11 System.out.println(">> " + iter.next() + "\n");
12 }
13 return;
14 }
Sample Code creating an XML from an Object
1 InstructorQualificationDocument iqDoc2 = InstructorQualificationDocument.Factory.newInstance();
2 InstructorQualification iqTest2 = iqDoc2.addNewInstructorQualification();
3 // This methods will depend upon your XML Schema. Each Method represents an element in your XML with the same name
4 iqTest2.setEMPNUM("0000456789");
5 iqTest2.setINSTRACCDE("320");
6 iqTest2.setINSTRDSIGCDE("PCPSIM");
7 iqTest2.setINSTRQLFSTATSCDE("C");
8 System.out.println(iqDoc2.toString());
I hope that this post gave you an idea on how easy it is to use XMLBeans. GoodBye marshalling and unmarshalling!
1.) Download and Install XMLBeans (http://xmlbeans.apache.org/). I got version 2.1.0
2.) Create the XML Schema
3.) Do a SCOMP (Please refer to XMLBeans documentation)
4.) Once the XMLBeans jar for my XML Schema was created then it was ready to use.
Here is a sample Code:
Sample Code processing an XML Message to an Object
xmlTest is the XML Message in String
InstructorQualification is the root element of the XML based on the XML Schema.
1 InstructorQualificationDocument iqDoc = null;
2 ArrayList validationErrors = new ArrayList();
3 XmlOptions validationOptions = new XmlOptions();
4 validationOptions.setErrorListener(validationErrors);
5 iqDoc = InstructorQualificationDocument.Factory.parse(xmlTest);
6 boolean isValid = iqDoc.validate(validationOptions);
7 if(!isValid){
8 Iterator iter = validationErrors.iterator();
9 while (iter.hasNext())
10 {
11 System.out.println(">> " + iter.next() + "\n");
12 }
13 return;
14 }
Sample Code creating an XML from an Object
1 InstructorQualificationDocument iqDoc2 = InstructorQualificationDocument.Factory.newInstance();
2 InstructorQualification iqTest2 = iqDoc2.addNewInstructorQualification();
3 // This methods will depend upon your XML Schema. Each Method represents an element in your XML with the same name
4 iqTest2.setEMPNUM("0000456789");
5 iqTest2.setINSTRACCDE("320");
6 iqTest2.setINSTRDSIGCDE("PCPSIM");
7 iqTest2.setINSTRQLFSTATSCDE("C");
8 System.out.println(iqDoc2.toString());
I hope that this post gave you an idea on how easy it is to use XMLBeans. GoodBye marshalling and unmarshalling!
Comments