XFire客户端解析服务端返回List

来源:互联网 发布:友情 知乎 编辑:程序博客网 时间:2024/05/10 15:25

系统JDK:1.4


服务端方法如下:

/*
* 获取项目列表,新建
*/
public List getProjectList() {
/* 构造sql语句 */
StringBuffer sb = new StringBuffer();
sb
.append("select tpbi.ID as projectId, tpbi.PROJECT_NAME as projectName from T_PROJECT_BASIC_INF tpbi where 1>0"
+ " ");
sb.append("order by tpbi.ID");// 加上排序
String sql = sb.toString();
// 调用公用方法获取List
List tmpValue = this.executeSQLForList(sql);
return tmpValue;
}

客户端代码如果是:

/**
 * 动态客户端的实现SOAP authentication with handlers(已实现)
 * 
 * @author Administrator(无法传递List信息)
 */
public void testClientByDynamic_Spring() {
System.out.println(" ----> 根据动态客户端的实现 <---- ");
String wsURL = "http://localhost:8021/XFireWebService/services/conditionInfoService?wsdl";
try {
Client client = new Client(new URL(wsURL));
/** * 通过SOAP authentication with handlers 实现认证 */
client
.addOutHandler(new ClientAuthenticationHandler("test",
"test"));


/** * 设置mtom 开始 */
/*
 * client.setProperty("mtom-enabled", "true");
 * client.setProperty(HttpTransport.CHUNKING_ENABLED, "true");
 */
/** * 设置mtom 结束 */
System.out.println(" ----> getProjectList start <---- ");
Object[] ProjectListResults = client.invoke("getProjectList",
new Object[] { null });
System.out.println("ProjectListResults的长度:"
+ ProjectListResults.length);
System.out.println("getProjectList----" + ProjectListResults[0]);
System.out.println(" ----> getProjectList end <---- ");
} catch (Exception e) {
e.printStackTrace();
}
}


会发现返回的:

ProjectListResults的长度:是1

这说明是返回了的

但是System.out.println("getProjectList----" + ProjectListResults[0]);这一句确得不到正确的结果

一般客户端返回[#document: null]

这个的原因是没有解析的问题

下面是解析代码(此处解析是根据客户端获取的那个Document来解析的 所以如果你要解析你服务端返回的数据 是需要差看服务端返回的xml文件的 这里只是提供了一个思路)

客户端代码如下:

/**
* 动态客户端的实现SOAP authentication with handlers(已实现)

* @author Administrator(无法传递List信息)
*/
public void testClientByDynamic_Spring() {
System.out.println(" ----> 根据动态客户端的实现 <---- ");
String wsURL = "http://localhost:8021/XFireWebService/services/conditionInfoService?wsdl";
try {
Client client = new Client(new URL(wsURL));
/** * 通过SOAP authentication with handlers 实现认证 */
client
.addOutHandler(new ClientAuthenticationHandler("test",
"test"));


/** * 设置mtom 开始 */
/*
* client.setProperty("mtom-enabled", "true");
* client.setProperty(HttpTransport.CHUNKING_ENABLED, "true");
*/
/** * 设置mtom 结束 */
System.out.println(" ----> getProjectList start <---- ");
Object[] ProjectListResults = client.invoke("getProjectList",
new Object[] { null });
Document docProjectListResults = (Document) ProjectListResults[0];
saveToLocal(docProjectListResults, "D:\\docxml");//保存document到本地文件 文件格式文件doc.xml


analysisDocument1(docProjectListResults);
System.out.println(" ----> getProjectList end <---- ");
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 解析返回的List信息

* @param xtree
*/
public static void analysisDocument1(Document xtree) {
Element element = xtree.getDocumentElement();
NodeList childNode = element.getChildNodes();
for (int i = 0; i < childNode.getLength(); i++) {
Element element1 = (Element) childNode.item(i);//<ns1:anyType2anyTypeMap
NodeList childNode1 = element1.getChildNodes();//MAP (projectId/projectName)
System.out.println("-----projectId-------:"
+ ((Element)childNode1.item(0)).getElementsByTagName("value").item(0).getFirstChild().getNodeValue());
System.out.println("-----projectName-------:"
+ ((Element)childNode1.item(1)).getElementsByTagName("value").item(0).getFirstChild().getNodeValue());
/*Node node = childNode.item(i);
stepThrough(node);*/
}
}

/**
* 保存XML到本地
* @param docProjectListResults
* @param fileName
*/
public static void saveToLocal(Document docProjectListResults,
String fileName) {
try {
/** 将document中的内容写入文件中 */
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
/** 编码 */
// transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312");
DOMSource source = new DOMSource(docProjectListResults);
StreamResult result = new StreamResult(new File(fileName));
transformer.transform(source, result);
} catch (Exception ex) {
ex.printStackTrace();
}
}

doc.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<ns1:out xmlns:ns1="http://www.com.dlmu.base/xfire">
<ns1:anyType2anyTypeMap
xmlns:ns1="http://www.com.dlmu.base/xfire">
<entry xmlns="http://www.com.dlmu.base/xfire">
<key xsi:type="xsd:string">PROJECTID</key>
<value xsi:type="xsd:decimal">101</value>
</entry>
<entry xmlns="http://www.com.dlmu.base/xfire">
<key xsi:type="xsd:string">PROJECTNAME</key>
<value xsi:type="xsd:string">333333333333333333</value>
</entry>
</ns1:anyType2anyTypeMap>
<ns1:anyType2anyTypeMap
xmlns:ns1="http://www.com.dlmu.base/xfire">
<entry xmlns="http://www.com.dlmu.base/xfire">
<key xsi:type="xsd:string">PROJECTID</key>
<value xsi:type="xsd:decimal">104</value>
</entry>
<entry xmlns="http://www.com.dlmu.base/xfire">
<key xsi:type="xsd:string">PROJECTNAME</key>
<value xsi:type="xsd:string">444444444444444444444</value>
</entry>
</ns1:anyType2anyTypeMap>
<ns1:anyType2anyTypeMap
xmlns:ns1="http://www.com.dlmu.base/xfire">
<entry xmlns="http://www.com.dlmu.base/xfire">
<key xsi:type="xsd:string">PROJECTID</key>
<value xsi:type="xsd:decimal">105</value>
</entry>
<entry xmlns="http://www.com.dlmu.base/xfire">
<key xsi:type="xsd:string">PROJECTNAME</key>
<value xsi:type="xsd:string">555555555555555555555555 </value>
</entry>
</ns1:anyType2anyTypeMap>
<ns1:anyType2anyTypeMap
xmlns:ns1="http://www.com.dlmu.base/xfire">
<entry xmlns="http://www.com.dlmu.base/xfire">
<key xsi:type="xsd:string">PROJECTID</key>
<value xsi:type="xsd:decimal">106</value>
</entry>
<entry xmlns="http://www.com.dlmu.base/xfire">
<key xsi:type="xsd:string">PROJECTNAME</key>
<value xsi:type="xsd:string">666666666666666666666666 </value>
</entry>
</ns1:anyType2anyTypeMap>
<ns1:anyType2anyTypeMap
xmlns:ns1="http://www.com.dlmu.base/xfire">
<entry xmlns="http://www.com.dlmu.base/xfire">
<key xsi:type="xsd:string">PROJECTID</key>
<value xsi:type="xsd:decimal">108</value>
</entry>
<entry xmlns="http://www.com.dlmu.base/xfire">
<key xsi:type="xsd:string">PROJECTNAME</key>
<value xsi:type="xsd:string">0000000000000000000000000 </value>
</entry>
</ns1:anyType2anyTypeMap>
<ns1:anyType2anyTypeMap
xmlns:ns1="http://www.com.dlmu.base/xfire">
<entry xmlns="http://www.com.dlmu.base/xfire">
<key xsi:type="xsd:string">PROJECTID</key>
<value xsi:type="xsd:decimal">109</value>
</entry>
<entry xmlns="http://www.com.dlmu.base/xfire">
<key xsi:type="xsd:string">PROJECTNAME</key>
<value xsi:type="xsd:string">345455555555555555555555555555 </value>
</entry>
</ns1:anyType2anyTypeMap>
<ns1:anyType2anyTypeMap
xmlns:ns1="http://www.com.dlmu.base/xfire">
<entry xmlns="http://www.com.dlmu.base/xfire">
<key xsi:type="xsd:string">PROJECTID</key>
<value xsi:type="xsd:decimal">110</value>
</entry>
<entry xmlns="http://www.com.dlmu.base/xfire">
<key xsi:type="xsd:string">PROJECTNAME</key>
<value xsi:type="xsd:string">345</value>
</entry>
</ns1:anyType2anyTypeMap>
<ns1:anyType2anyTypeMap
xmlns:ns1="http://www.com.dlmu.base/xfire">
<entry xmlns="http://www.com.dlmu.base/xfire">
<key xsi:type="xsd:string">PROJECTID</key>
<value xsi:type="xsd:decimal">111</value>
</entry>
<entry xmlns="http://www.com.dlmu.base/xfire">
<key xsi:type="xsd:string">PROJECTNAME</key>
<value xsi:type="xsd:string">33333333</value>
</entry>
</ns1:anyType2anyTypeMap>
<ns1:anyType2anyTypeMap
xmlns:ns1="http://www.com.dlmu.base/xfire">
<entry xmlns="http://www.com.dlmu.base/xfire">
<key xsi:type="xsd:string">PROJECTID</key>
<value xsi:type="xsd:decimal">112</value>
</entry>
<entry xmlns="http://www.com.dlmu.base/xfire">
<key xsi:type="xsd:string">PROJECTNAME</key>
<value xsi:type="xsd:string">d333333333333333</value>
</entry>
</ns1:anyType2anyTypeMap>
<ns1:anyType2anyTypeMap
xmlns:ns1="http://www.com.dlmu.base/xfire">
<entry xmlns="http://www.com.dlmu.base/xfire">
<key xsi:type="xsd:string">PROJECTID</key>
<value xsi:type="xsd:decimal">113</value>
</entry>
<entry xmlns="http://www.com.dlmu.base/xfire">
<key xsi:type="xsd:string">PROJECTNAME</key>
<value xsi:type="xsd:string">99999999999999</value>
</entry>
</ns1:anyType2anyTypeMap>
<ns1:anyType2anyTypeMap
xmlns:ns1="http://www.com.dlmu.base/xfire">
<entry xmlns="http://www.com.dlmu.base/xfire">
<key xsi:type="xsd:string">PROJECTID</key>
<value xsi:type="xsd:decimal">114</value>
</entry>
<entry xmlns="http://www.com.dlmu.base/xfire">
<key xsi:type="xsd:string">PROJECTNAME</key>
<value xsi:type="xsd:string">999999997777777777</value>
</entry>
</ns1:anyType2anyTypeMap>
</ns1:out>



欢迎交流