JAXB is one of the powerful parser, which is used to parse xml file to java object and java object to xml file(i.e; Marshalling and Un marshalling xml files).
Below sample code explains how to use jaxb parser to parse an xml
Jaxb uses xml and xsd to parse xml to java objects.
Jaxb provides xjc compiler which can be used to compile the xsd file , after compiling xsd file with xjc compiler we will get java class files of each node of the xml file
I assume you have xsd , xml file and you compiled the xsd file with xjc compiler.
public class JAXBParser {
//in my xml the root node is student
//get the root node from the xml
//below is the class that we got after compiling the xsd with xjc
jaxbGenerated.Student studentElement=null;
private JAXBParser () throws SAXException, JAXBException {
loadXML();
}
// Making the class as singleton
public static synchronized JAXBParser getXMLParserInstance()
throws SAXException, JAXBException {
if (xmlParserObj == null) {
xmlParserObj = new JAXBParser();
}
return xmlParserObj;
}
public void loadXML() throws SAXException, JAXBException {
File xmlDocument = new File("test.xml");
//to get jaxb context object you need to specify the generated classes contained package
//in my case the package name jaxbGenerated
JAXBContext jaxbContext = JAXBContext.newInstance("jaxbGenerated");
// Unmarshaller creation which used to convert XML to Java object
Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
// Set the schema on the Unmarshaller object
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
javax.xml.validation.Schema schema = schemaFactory.newSchema(new File("test.xsd"));
unMarshaller.setSchema(schema);
// Calling unMarshall
unMarshall(xmlDocument, unMarshaller);
}
/**
* This method is used to unmarshal the xml Document which identifies rootnode of the xml
* @param xmlDocument
* @param unMarshaller
*/
public void unMarshall(File xmlDocument, Unmarshaller unMarshaller) {
try {
// UnMarshall the XML using unmarshal()
countryElement = (jaxbGenerated.Student) unMarshaller.unmarshal(xmlDocument);
}
catch (JAXBException e) {
}
catch(Exception excep)
{
}
}
/**
* This method will give studentnode
* @return student
*/
private Student getStudent(){
Student studentObj=null;
studentObj= studentElement;
return studentObj;
}
//By using student object we will get student property values
/**
* This method will give studentName
*
* @return studentName
*/
public String getStudentName() {
String studentName= null;
Student student= getStudent();
studentName= student.getName();
return studentName;
}
}
test.xml looks like as follows
<student name="xyz" language="abc">
<child nodes/>
</student>
hope this will help you
Below sample code explains how to use jaxb parser to parse an xml
Jaxb uses xml and xsd to parse xml to java objects.
Jaxb provides xjc compiler which can be used to compile the xsd file , after compiling xsd file with xjc compiler we will get java class files of each node of the xml file
I assume you have xsd , xml file and you compiled the xsd file with xjc compiler.
public class JAXBParser {
//in my xml the root node is student
//get the root node from the xml
//below is the class that we got after compiling the xsd with xjc
jaxbGenerated.Student studentElement=null;
private JAXBParser () throws SAXException, JAXBException {
loadXML();
}
// Making the class as singleton
public static synchronized JAXBParser getXMLParserInstance()
throws SAXException, JAXBException {
if (xmlParserObj == null) {
xmlParserObj = new JAXBParser();
}
return xmlParserObj;
}
public void loadXML() throws SAXException, JAXBException {
File xmlDocument = new File("test.xml");
//to get jaxb context object you need to specify the generated classes contained package
//in my case the package name jaxbGenerated
JAXBContext jaxbContext = JAXBContext.newInstance("jaxbGenerated");
// Unmarshaller creation which used to convert XML to Java object
Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
// Set the schema on the Unmarshaller object
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
javax.xml.validation.Schema schema = schemaFactory.newSchema(new File("test.xsd"));
unMarshaller.setSchema(schema);
// Calling unMarshall
unMarshall(xmlDocument, unMarshaller);
}
/**
* This method is used to unmarshal the xml Document which identifies rootnode of the xml
* @param xmlDocument
* @param unMarshaller
*/
public void unMarshall(File xmlDocument, Unmarshaller unMarshaller) {
try {
// UnMarshall the XML using unmarshal()
countryElement = (jaxbGenerated.Student) unMarshaller.unmarshal(xmlDocument);
}
catch (JAXBException e) {
}
catch(Exception excep)
{
}
}
/**
* This method will give studentnode
* @return student
*/
private Student getStudent(){
Student studentObj=null;
studentObj= studentElement;
return studentObj;
}
//By using student object we will get student property values
/**
* This method will give studentName
*
* @return studentName
*/
public String getStudentName() {
String studentName= null;
Student student= getStudent();
studentName= student.getName();
return studentName;
}
}
test.xml looks like as follows
<student name="xyz" language="abc">
<child nodes/>
</student>
hope this will help you
No comments:
Post a Comment