[英]Web service returning multiple values in a single row delimited by tags
I need to build a web service that returns multiple rows from database and each row should containt several fields delimited by tags. 我需要构建一个Web服务,该服务从数据库返回多行,并且每一行应包含由标签分隔的多个字段。 I created this ws using Netbeans and it is running on a glassfish server.
我使用Netbeans创建了该ws,它在glassfish服务器上运行。 I only managed to return tagged rows containing a string made by concatenated values (fields).
我只设法返回包含由串联值(字段)组成的字符串的标记行。 Can you please give me some advice on how to modify my return?
您能给我一些有关如何修改退货的建议吗?
Now my web service returns as following: 现在,我的Web服务返回如下:
<?xml version="1.0" encoding="UTF-8"?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:extractCSResponse xmlns:ns2="http://ws/">
<ROW xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string"><Firstname>Mark</Firstname><Lastname>Thomas</Lastname><ID>1112546</ID></ROW>
<ROW xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string"><Firstname>Mike</Firstname><Lastname>Jackson</Lastname><ID>1112547</ID></ROW>
</ns2:extractCSResponse>
</S:Body>
</S:Envelope>
My return needs to look like this: 我的退货需要看起来像这样:
<S:Body>
<ns2:extractCSResponse xmlns:ns2="http://ws/">
<ROWS>
<ROW>
<Firstname>Mark</Firstname>
<Lastname>Thomas</Lastname>
<IDname>1112546</IDname>
</ROW>
<ROW>
<Firstname>Mike</Firstname>
<Lastname>Jackson</Lastname>
<IDname>1112547</IDname>
</ROW>
</ROWS>
</ns2:extractCSResponse>
</S:Body>
Java code below: 下面的Java代码:
@WebService(serviceName = "GetCS2")
@Stateless()
public class GetCS2 {
/**
* This is a sample web service operation
*/
@WebMethod(operationName = "extractCS")
@WebResult(name = "ROW")
public List extractCS()
{
List l = new ArrayList();
//username and password for database connection
String userName = "username";
String password = "password";
//db connection
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://172.19.125.222:1433"+";databaseName=Test";
Connection con = DriverManager.getConnection(url, userName, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from list");
//loop through selection
while(rs.next())
{
l.add("<Firstname>" + rs.getString("fn") + "</Firstname>" + "<Lastname>" + rs.getString("ln") + "</Lastname>" + "<IDname>" + rs.getDate("ID") + "</ID>");
}
//close connections
stmt.close();
rs.close();
con.close();
} catch(SQLException e)
{
System.out.println("SQL Exception: " + e.toString());
}
catch(ClassNotFoundException cE)
{
System.out.println("Class Not Found Exception: " + cE.toString());
}
return l;
}
}
EDIT to show basic example in case the links will one day no longer have this information. 编辑以显示基本示例,以防万一链接将不再有此信息。
I generally try to avoid building xml
by concatenating strings. 我通常尝试通过连接字符串来避免构建
xml
。 This is error prone, can contain invalid xml
data (what if a name contains &
or <
or any other special characters which can't appear in xml
?) and hard to change once there is new data or something changes in the database structure. 这很容易出错,可能包含无效的
xml
数据(如果名称包含&
或<
或任何其他无法在xml
出现的特殊字符呢?),并且一旦有新数据或数据库结构发生更改就很难更改。 Take a look at JAXB for mapping objects to xml
files. 看一下将对象映射到
xml
文件的JAXB 。
Basically you will need to create a custom class representing your database object
and a wrapper for multiple ones. 基本上,您将需要创建一个代表您的数据库
object
的自定义类以及一个用于多个object
的包装器。 Populate objects from your DB, add them to the wrapper, then use JAXB
to write to xml
Here is an example JAXB Example 从数据库填充对象,将它们添加到包装器,然后使用
JAXB
写入xml
这是一个示例JAXB示例
@XmlRootElement(name = "Person")
public class Person{
private String firstName;
private String lastName;
private String idName;
....
}
@XmlRootElement(name = "PersonWrapper")
public class PersonWrapper{
private List<Person> persons;
@XmlElementWrapper(name = "PersonList")
@XmlElement(name = "Person")
public List getPersons(){
return persons;
}
....
}
Then in your code, add resultset
from database into a list. 然后在您的代码中,将数据库中的
resultset
添加到列表中。 Then add the list to the wrapper. 然后将列表添加到包装器。 Finally create
xml
file from the wrapper. 最后从包装器创建
xml
文件。
List<Person> personList=new ArrayList<Person>();
while(rs.next())
{
personList.add(new Person(rs.getString("fn"),rs.getString("lastName"),...));
}
PersonWrapper pw=new PersonWrapper();
pw.setPersons(personList);
JAXBContext context = JAXBContext.newInstance(PersonWrapper.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
//write to system.out or any other OutputStream (ByteArrayOutputStream)
m.marshal(pw, System.out);
//OR write to xml file
m.marshal(pw, new File("person.xml"));
If you want to return a String
, then simply convert OutputStream
to String
and display the results. 如果要返回
String
,则只需将OutputStream
转换为String
并显示结果。 You can use ByteArrayOutputStream
with baos.toString(desiredEncoding);
您可以将
ByteArrayOutputStream
与baos.toString(desiredEncoding);
一起使用baos.toString(desiredEncoding);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.