XML Lab Record
XML Lab Record
com
Department of MCA
XML AND WEB SERVICES LAB (MC7512)
Name : _____________________________
Reg. No : _____________________________
Semester : V
www.Vidyarthiplus.com
www.Vidyarthiplus.com
REGISTRATION NO:
BONAFIDE CERTIFICATE
This is to certify that it is the bonafide record of the practical work done by
Mr/Ms......................................................... of MCA (Master Of Computer
Applications) in the .............................................................. Laboratory during the
academic year 2016-2017.
--------------------------- -----------------------------------
Staff-In charge Head of the Department
---------------------------- -----------------------------
Internal Examiner External Examiner
www.Vidyarthiplus.com
www.Vidyarthiplus.com
INDEX
Exp Page
Date Name of the Experiment Signature
No No
1 XML Document Creation
3 XSL Transformation
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Aim:
To create a simple XML document to display the address book.
Procedure:
Step-1: Create the xml document using notepad.
Step-2: Create the addressbook as a root tag.
Step-3: Followed by create a three employees address in detail using different tag.
Step-4: Save the file as Employee.xml.
Step-5: Finally execute the program in Internet Explorer to view the output.
Source Code:
Filename: Employee.xml
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Output:
Result:
Thus, the simple xml document creation has been executed successfully.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Aim:
To create a xml document and database for importing and exporting xml document into
database.
Procedure:
Step-1: Start the process.
Step-2: Create the xml document using notepad and write the following code:
Source Code:
Filename: Hello.xml
If the Developer tab is not available, you need to enable it in the options. Click on File, and then go
all the way down to Options. Click on the Customize Ribbon section, and in the column on the
right, there should be a list of tabs called Main Tabs that you can check or uncheck.
Find where it says Developer, and make sure the box is checked. Then click OK. The Developer
tab should now be up top, to the right of the View tab.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Click Add to add a browse the location of xml document and click ok.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Step-8: Drag and drop XML elements into the spreadsheet and import the XML file into Excel. Add
the new record into the database.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Step-9: Once finished converting each column into an XML element, return to the Developer tab,
find the XML section again, and click Export. Save your XML file, and then open it in Notepad to
see the results:
Result:
Thus, the program to importing and exporting XML into Excel-database has been executed
successfully.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Aim:
To create a simple XSLT transformation from XSL to XML.
Procedure:
Step-1: Create a XSLT document using notepad.
Step-2: Using XSLT document, we can create a style sheet like Font Style, Font Size, bgcolor,..etc.
and write the following code:
Source Code:
Filename:cdcatalog.xsl
Source Code:
Filename: Simpxsl.xml
www.Vidyarthiplus.com
www.Vidyarthiplus.com
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd>
<title>Eros</title>
<artist>Eros Ramazzotti</artist>
<country>EU</country>
<company>BMG</company>
<price>9.90</price>
<year>1997</year>
</cd>
<cd>
<title>One night only</title>
<artist>Bee Gees</artist>
<country>UK</country>
<company>Polydor</company>
<price>10.90</price>
<year>1998</year>
</cd>
<cd>
<title>Sylvias Mother</title>
<artist>Dr.Hook</artist>
<country>UK</country>
www.Vidyarthiplus.com
www.Vidyarthiplus.com
<company>CBS</company>
<price>8.10</price>
<year>1973</year>
</cd>
<cd>
<title>Maggie May</title>
<artist>Rod Stewart</artist>
<country>UK</country>
<company>Pickwick</company>
<price>8.50</price>
<year>1990</year>
</cd>
<cd>
<title>Romanza</title>
<artist>Andrea Bocelli</artist>
<country>EU</country>
<company>Polydor</company>
<price>10.80</price>
<year>1996</year>
</cd>
<cd>
<title>When a man loves a woman</title>
<artist>Percy Sledge</artist>
<country>USA</country>
<company>Atlantic</company>
<price>8.70</price>
<year>1987</year>
</cd>
<cd>
<title>Black angel</title>
<artist>Savage Rose</artist>
<country>EU</country>
<company>Mega</company>
<price>10.90</price>
<year>1995</year>
</cd>
<cd>
<title>1999 Grammy Nominees</title>
<artist>Many</artist>
<country>USA</country>
<company>Grammy</company>
<price>10.20</price>
<year>1999</year>
</cd>
<cd>
<title>For the good times</title>
<artist>Kenny Rogers</artist>
<country>UK</country>
<company>Mucik Master</company>
<price>8.70</price>
<year>1995</year>
www.Vidyarthiplus.com
www.Vidyarthiplus.com
</cd>
<cd>
<title>Big Willie style</title>
<artist>Will Smith</artist>
<country>USA</country>
<company>Columbia</company>
<price>9.90</price>
<year>1997</year>
</cd>
<cd>
<title>Tupelo Honey</title>
<artist>Van Morrison</artist>
<country>UK</country>
<company>Polydor</company>
<price>8.20</price>
<year>1971</year>
</cd>
<cd>
<title>Soulsville</title>
<artist>JornHoel</artist>
<country>Norway</country>
<company>WEA</company>
<price>7.90</price>
<year>1996</year>
</cd>
<cd>
<title>The very best of</title>
<artist>Cat Stevens</artist>
<country>UK</country>
<company>Island</company>
<price>8.90</price>
<year>1990</year>
</cd>
<cd>
<title>Stop</title>
<artist>Sam Brown</artist>
<country>UK</country>
<company>A and M</company>
<price>8.90</price>
<year>1988</year>
</cd>
<cd>
<title>Bridge of Spies</title>
<artist>T`Pau</artist>
<country>UK</country>
<company>Siren</company>
<price>7.90</price>
<year>1987</year>
</cd>
<cd>
<title>Private Dancer</title>
www.Vidyarthiplus.com
www.Vidyarthiplus.com
<artist>Tina Turner</artist>
<country>UK</country>
<company>Capitol</company>
<price>8.90</price>
<year>1983</year>
</cd>
<cd>
<title>Midtomnatten</title>
<artist>Kim Larsen</artist>
<country>EU</country>
<company>Medley</company>
<price>7.80</price>
<year>1983</year>
</cd>
<cd>
<title>Pavarotti Gala Concert</title>
<artist>Luciano Pavarotti</artist>
<country>UK</country>
<company>DECCA</company>
<price>9.90</price>
<year>1991</year>
</cd>
<cd>
<title>The dock of the bay</title>
<artist>Otis Redding</artist>
<country>USA</country>
<company>Atlantic</company>
<price>7.90</price>
<year>1987</year>
</cd>
<cd>
<title>Picture book</title>
<artist>Simply Red</artist>
<country>EU</country>
<company>Elektra</company>
<price>7.20</price>
<year>1985</year>
</cd>
<cd>
<title>Red</title>
<artist>The Communards</artist>
<country>UK</country>
<company>London</company>
<price>7.80</price>
<year>1987</year>
</cd>
<cd>
<title>Unchain my heart</title>
<artist>Joe Cocker</artist>
<country>USA</country>
<company>EMI</company>
www.Vidyarthiplus.com
www.Vidyarthiplus.com
<price>8.20</price>
<year>1987</year>
</cd>
</catalog>
Result:
Thus the program to convert the simple XSLT transformation from XSL to XML has been
executed successfully.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Aim:
To create a XML program for Internal DTD(Document Type Definition) creation.
Procedure:
Step-1: Open the notepad.
Step-2: Create a DTD document for book details.
Step-3:Save the document as Filename.xml.
Step-4:Type a XML Parser using HTML coding.
Step-5:Save the document as Filename.html.
Step-6:Execute the program in Internet Explorer.
Source Code:
File name:Sche.xml
www.Vidyarthiplus.com
www.Vidyarthiplus.com
<book>
<title name="PUZZLES"></title>
<isbn number="2313"></isbn>
<zipcode zname="456644445"></zipcode>
<publish year="1995"></publish>
</book>
<book>
<title name="XML"></title>
<isbn number="2312"></isbn>
<zipcode zname="412684895585"></zipcode>
<publish year="1995"></publish>
</book>
</books>
Filename:Par.html
<html>
<body>
<h3>This demonstrates a parser error:</h3>
<script type="text/javascript">
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.validateOnParse="true";
xmlDoc.load("Sche.xml");
document.write("<br/>Error Code:");
document.write(xmlDoc.parseError.errorCode);
document.write("<br/>Error Reason:");
document.write(xmlDoc.parseError.reason);
document.write("<br/>Error Line:");
document.write(xmlDoc.parseError.line);
</script>
</body>
</html>
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Output:
Result:
Thus, the XML program for creating Internal DTD has been executed successfully.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Aim:
To create a XML program for External DTD(Document Type Definition) creation.
Procedure:
Step-1: Open the notepad.
Step-2: Create a DTD document for book details.
Step-3: Save the document as Filename.dtd.
Step-4: Create a XML document for book details.
Step-5: Save the document as Filename.xml.
Step-6: Type a XML Parser using HTML coding.
Step-7: Save the document as Filename.html.
Step-8: Execute the program in Internet Explorer.
Source Code:
Filename:not.dtd
Filename: Sc.xml
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Filename:Pars.html
<html>
<body>
<h3>This demonstrates a parser error:</h3>
<script type="text/javascript">
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.validateOnParse="true";
xmlDoc.load("Sc.xml");
document.write("<br/>Error Code:");
document.write(xmlDoc.parseError.errorCode);
document.write("<br/>Error Reason:");
document.write(xmlDoc.parseError.reason);
document.write("<br/>Error Line:");
document.write(xmlDoc.parseError.line);
</script>
</body>
</html>
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Output:
Result:
Thus, the XML program for creating External DTD has been executed successfully.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Aim:
To create a program for XML Schema creation and display element and attributes.
Procedure:
Step-1: Open the XMLwriter.
Step-2: Click File->New->XML Document and name the Filename.
Step-3:Type the following program:
Source Code:
Filename:Sche.xml
Source Code:
Filename:sche.xsd
www.Vidyarthiplus.com
www.Vidyarthiplus.com
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Output:
Result:
Thus, the program for XML Schema creation and display element and attributes has been
executed successfully.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Aim:
To create a parsing XML document using DOM(Document Object Model) parser.
Procedure:
Step-1: Open the notepad.
Step-2: Create a xml document for book collection.
Step-3: Save the document as Filename.xml.
Step-4: Type the following program:
Source Code:
Filename:Bookcollection.xml
<bookdb>
<book>
<name>Java Complete Reference</name>
<author>lan</author>
<year>2001</year>
<publication>Tata McDraw Hill</publication>
</book>
<book>
<name>C# Complete Reference</name>
<author>balagurusamy</author>
<year>2006</year>
<publication>Persion Addition</publication>
</book>
<book>
<name>Complete Reference</name>
<author>Kanithkar</author>
<year>2005</year>
<publication>Tata McDraw Hill</publication>
</book>
</bookdb>
Source Code:
Filename: Bookdetailsdom.html
<html>
<head>
<script language="javascript">
var root;
var xmldoc=new ActiveXObject("Microsoft.XMLDOM");
xmldoc.load("Bookcollection.xml");
www.Vidyarthiplus.com
www.Vidyarthiplus.com
function start()
{
if(xmldoc.readyState=="4")
loading();
else
alert("could not load");
}
function loading()
{
var i;
root=xmldoc.documentElement;
var count;
count=root.childNodes.length;
for(i=0;i<count;i++)
{
document.write(root.childNodes.item(i).text+"<br>");
}
}
</script>
</head>
<body onLoad="start()">
</body>
</html>
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Output:
Result:
Thus, the parsing XML document using DOM(Document Object Model) parser has been
executed successfully.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Aim:
To create a Web Service program using JAX-WS for currency conversion.
Procedure:
Step-1: Open the NetBeans IDE.
Step-2: Choose File->New Project. Select Web Application from the Java Web category from the
Java EE category.
Step-3: Name the project CurWSApplication. Select a location for the project. Click Next.
Step-4: Select the GlassFish Server and Java EE version and click Finish.
Step-5: Right-click the CurWSApplication node and choose New->Web Service.
Step-6: Name the web service CurWS and type org.me.calculator in Package and Click Finish.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Step-16: Click the Add Operation in the Design View dialog box. The Add Operation dialog box
opens.
Step-17: In the upper part of the Add Operation dialog box, type RupeesToPounds in Name and
type double in Return Type drop-down list.
Step-18: In the lower part of the Add Operation dialog box, click Add and create a parameter of type
double named Rupees.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Step-28: Edit the following code in the CurWS class of CurWS.java file:
Source Code:
@WebService(serviceName = "CurWS")
public class CurWS
{
@WebMethod(operationName = "RupeesToDollar")
public double RupeesToDollar(@WebParam(name = "Rupees") double Rupees)
{
return (Rupees/46.5);
}
@WebMethod(operationName = "RupeesToEuro")
public double RupeesToEuro(@WebParam(name = "Rupees") double Rupees)
{
return (Rupees/68.64);
}
@WebMethod(operationName = "RupeesToPounds")
public double RupeesToPounds(@WebParam(name = "Rupees") double Rupees)
{
return (Rupees/73.55);
}
@WebMethod(operationName = "RupeesToReal")
public double RupeesToReal(@WebParam(name = "Rupees") double Rupees)
{
return (Rupees/26.7);
}
@WebMethod(operationName = "RupeesToRiggit")
public double RupeesToRiggit(@WebParam(name = "Rupees") double Rupees)
{
return (Rupees/13.67);
}
}
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Output:
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Result:
Thus, the Web-Service creation using JAX-WS for currency conversion has been executed
successfully.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Aim:
To create a Web Service program using JAX-WS for temperature conversion.
Procedure:
Step-1:Open the NetBeans IDE.
Step-2:Choose File->New Project. Select Web Application from the Java Web category from the
Java EE category.
Step-3:Name the project TempWSApplication. Select a location for the project. Click Next.
Step-4:Select the GlassFish Server and Java EE version and click Finish.
Step-5:Right-click the TempWSApplication node and choose New->Web Service.
Step-6: Name the web service TempWS and type org.me.calculator in Package and Click Finish.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Step-16: Edit the following code in the TempWS class of TempWS.java file:
Source Code:
@WebService(serviceName = "TempWS")
public class TempWS
{
@WebMethod(operationName = "FarenheitToCelcius")
public double FarenheitToCelcius(@WebParam(name = "Farenheit") double Farenheit)
{
return (((Farenheit-32)/9)*5);
}
@WebMethod(operationName = "CelciusToFarenheit")
public double CelciusToFarenheit(@WebParam(name = "Celcius") double Celcius)
{
return (((Celcius*9)/5)+32);
}
}
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Output:
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Result:
Thus, the Web-Service creation using JAX-WS for temperature conversion has been
executed successfully.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Aim:
To create a Web Service program using JAX-RS.
Procedure:
Step-1: Start the process.
Step-2: Select File-> New project -> Java web specify the name.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
www.Vidyarthiplus.com
www.Vidyarthiplus.com
www.Vidyarthiplus.com
www.Vidyarthiplus.com
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Source Code:
Filename: GenericResource.java
package main;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
@Path("generic")
public class GenericResource
{
@Context
private UriInfo context;
public GenericResource()
{}
@GET
@Produces("text/html")
public String getHtml()
{
//TODO return proper representation object
return "<html><body>Welcome to XML Lab</body></html>";
}
@PUT
@Consumes("text/html")
public void putHtml(String content)
{
}
}
www.Vidyarthiplus.com
www.Vidyarthiplus.com
www.Vidyarthiplus.com
www.Vidyarthiplus.com
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Output:
Result:
Thus, the Web-Service creation using JAX-RS has been executed successfully.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Aim:
To create a Web Service program using .NET for currency conversion.
Procedure:
Step-1: Start->All programs->Microsoft Visual Studio 2010.
Step-2: Select New Project->Visual Basic->WCF Service Application->Filename.
Step-3: A Solution Explorer Window will be displaying on the screen.
Step-4: Right click on the file name and select Add->New Item->Web Service, and then click Add.
Step-5:Within the Web Method type the program for currency conversion.
Step-6: Execute the Web Service program.
Source Code:
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Service
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function RupeesToDollar(ByVal Rupee As String) As String
Dim rup
rup = Trim(Rupee)
If rup = "" Or IsNumeric(rup) = False Then
Return "Error"
Else
Return (Rupee/46.5)
End If
End Function
<WebMethod()> _
Public Function RupeesToPounds(ByVal Rupee As String) As String
Dim rup
rup = Trim(Rupee)
If rup = "" Or IsNumeric(rup) = False Then
Return "Error"
Else
Return (Rupee / 73.55)
End If
End Function
www.Vidyarthiplus.com
www.Vidyarthiplus.com
<WebMethod()> _
Public Function RupeesToEuro(ByVal Rupee As String) As String
Dim rup
rup = Trim(Rupee)
If rup = "" Or IsNumeric(rup) = FalseThen
Return "Error"
Else
Return (Rupee / 68.64)
End If
End Function
<WebMethod()> _
Public Function RupeesToRiggit(ByVal Rupee As String) As String
Dim rup
rup = Trim(Rupee)
If rup = "" Or IsNumeric(rup) = False Then
Return "Error"
Else
Return (Rupee / 13.67)
End If
End Function
<WebMethod()> _
Public Function RupeesToReal(ByVal Rupee As String) As String
Dim rup
rup = Trim(Rupee)
If rup = "" Or IsNumeric(rup) = False Then
Return "Error"
Else
Return (Rupee/26.7)
End If
End Function
End Class
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Output:
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Result:
Thus, the Web-Service creation using .Net for currency conversion has been executed
successfully.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Aim:
To create a Web Service program using .NET for temperature conversion.
Procedure:
Step-1: Start->All programs->Microsoft Visual Studio 2010.
Step-2: Select New Project->Visual Basic->WCF Service Application->Filename.
Step-3: A Solution Explorer Window will be displaying on the screen.
Step-4: Right click on the file name and select Add->New Item->Web Service, and then click Add.
Step-5: Within the Web Method type the program for temperature conversion.
Step-6: Execute the Web Service program.
Source Code:
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Service
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function FtoC(ByVal Faren As String) As String
Dim f
f = Trim(Replace(Faren, ",", "."))
If f = "" Or IsNumeric(f) = False Then
Return "Error"
Else
Return (((f - 32) / 9) * 5)
End If
End Function
<WebMethod()> _
Public Function CtoF(ByVal Cel As String) As String
Dim c
c = Trim(Replace(Cel, ",", "."))
If c = "" Or IsNumeric(c) = False Then
Return "Error"
Else
Return (((c * 9) / 5) + 32)
End If
End Function
End Class
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Output:
www.Vidyarthiplus.com
www.Vidyarthiplus.com
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Result:
Thus, the Web-Service creation using .Net for temperature conversion has been executed
successfully.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Aim:
To create a JAXB Marshaling program for converting Object into XML using Marshaller
interface.
Procedure:
Step-1: Create the process.
Step-2:Create POJO or bind the schema and generate the classes.
Step-3:Create the JAXBContent object.
Step-4:Create the Marshaler objects.
Step-5:Create the content tree by using set methods.
Step-6:Call the marshal method.
Step-7: Stop the process.
Source Code:
Filename: Question.java
importjava.util.List;
importjavax.xml.bind.annotation.XmlAttribute;
importjavax.xml.bind.annotation.XmlElement;
importjavax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Question
{
privateint id;
private String questionname;
private List<Answer> answers;
public Question()
{}
public Question(int id, String questionname, List<Answer> answers)
{
super();
this.id = id;
this.questionname = questionname;
this.answers = answers;
}
@XmlAttribute
publicintgetId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
www.Vidyarthiplus.com
www.Vidyarthiplus.com
@XmlElement
public String getQuestionname()
{
returnquestionname;
}
public void setQuestionname(String questionname)
{
this.questionname = questionname;
}
@XmlElement
public List<Answer>getAnswers()
{
return answers;
}
public void setAnswers(List<Answer> answers)
{
this.answers = answers;
}
}
Filename: Answer.java
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Filename:ObjectToXml.java
importjava.io.FileOutputStream;
importjava.util.ArrayList;
importjavax.xml.bind.JAXBContext;
importjavax.xml.bind.Marshaller;
public class ObjectToXml
{
public static void main(String[] args) throws Exception
{
JAXBContextcontextObj = JAXBContext.newInstance(Question.class);
MarshallermarshallerObj = contextObj.createMarshaller();
marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Answer ans1=new Answer(101,java is a programming language,ravi);
Answer ans2=new Answer(102,java is a platform,john);
ArrayList<Answer> list=new ArrayList<Answer>();
list.add(ans1);
list.add(ans2);
Question que=new Question(1,What is java?,list);
marshallerObj.marshal(que, new FileOutputStream(Question.xml));
}
}
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Output:
Result:
Thus, the JAXB Marshaling program for converting Object into XML has been executed
successfully.
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Aim:
To create a JAXB UnMarshaling program for converting XML into Object using Unmarshaller
interface.
Procedure:
Step-1: Start the process.
Step-2: Create POJO or bind the schema and generate the classes.
Step-3: Create the JAXBContent object.
Step-4: Create the Unmarshaller objects.
Step-5:Callthe unmarshal method.
Step-6:Use getter methods of POJO to access the data.
Step-7: Stop the process.
Source Code:
Filename: Question.xml
Filename: Question.java
importjava.util.List;
importjavax.xml.bind.annotation.XmlAttribute;
importjavax.xml.bind.annotation.XmlElement;
importjavax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Question
{
privateint id;
private String questionname;
private List<Answer> answers;
public Question()
{}
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Filename:Answer.java
www.Vidyarthiplus.com
www.Vidyarthiplus.com
publicintgetId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getAnswername()
{
returnanswername;
}
public void setAnswername(String answername)
{
this.answername = answername;
}
public String getPostedby()
{
returnpostedby;
}
public void setPostedby(String postedby)
{
this.postedby = postedby;
}
}
Filename: XmlToObject.java
importjava.io.File;
importjava.util.List;
importjavax.xml.bind.JAXBContext;
importjavax.xml.bind.JAXBException;
importjavax.xml.bind.Unmarshaller;
public class XmlToObject
{
public static void main(String[] args)
{
try
{
File file = new File("Question.xml");
JAXBContextjaxbContext = JAXBContext.newInstance(Question.class);
UnmarshallerjaxbUnmarshaller = jaxbContext.createUnmarshaller();
Question que= (Question) jaxbUnmarshaller.unmarshal(file);
System.out.println(que.getId()+" "+que.getQuestionname());
System.out.println("Answers:");
List<Answer> list=que.getAnswers();
for(Answer ans:list)
System.out.println(ans.getId()+" "+ans.getAnswername()+""+ans.getPostedby());
}
www.Vidyarthiplus.com
www.Vidyarthiplus.com
catch (JAXBException e)
{
e.printStackTrace();
}
}
}
Output:
Result:
Thus, the JAXB UnMarshaling program for converting XML into Object has been executed
successfully.
www.Vidyarthiplus.com