Best way to get a Web Service to return a database result as XML?
I am building a webservice using jax-rs and qu开发者_如何转开发erying a DB2 z/OS database with SQLJ and getting the result set as an arraylist. I would like to return this list as XML, but not sure how to do it.
Does anyone have an example of returning a result set as XML and is using an Arraylist the best way to do this?
Should I use JAXB? if so how?
you did not say which database you are using, on SQL Server you would use FOR XML
here is an example
USE AdventureWorks
GO
SELECT Cust.CustomerID,
OrderHeader.CustomerID,
OrderHeader.SalesOrderID,
OrderHeader.Status,
Cust.CustomerType
FROM Sales.Customer Cust
INNER JOIN Sales.SalesOrderHeader OrderHeader
ON Cust.CustomerID = OrderHeader.CustomerID
FOR XML AUTO
Perhaps XMLEncoder.writeObject(arrayList)
But this doesn't have to do with the database. Its DB > ArrayList
> xml
Easiest way is probably using jdom.
XStream is a simple library to serialize objects to XML and back again.
Have a look at their tutorial, you can extrapolate from it pretty quickly how an arraylist would be serialized.
精彩评论