上上楼正解返回xml, 然后还原
解决方案 »
- webservice 生成客户端时报错
- struts2 调用request报错 请各位高手指教!
- 能不能用ramdom()类产生汉字、英语字母、数字、符号综合的验证码
- weblogic ejb javax.naming.NameNotFoundException异常
- cookie的存储
- spring中如何引用org.springframework.orm.hibernate3.LocalSessionFactoryBean???
- 紧急提问。(无法解析格式不规范的HTML文件)怎么解决?在线等待
- Hibernate操作Orcle的Date列(精确到秒)的问题(急急,在线等,100分)
- 一初级问题:MVC和Struts?
- 要开发EJB是不是一定要安装J2EE,谢谢!
- 一个简单的webservice问题?请各位前辈帮忙(急!!!进来就有分不够再加!!)
- jboss4.0 下载
java理论上可以把集放到一个结果集吧,,不太明白楼主的意思
<name></name>
<height>170</height>
<age>20</age>
然后再读这个xml,根据其标签
说简单点就是一个javabean
import java.util.Random;import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;import com.dayang.archive.api.param.ArchiveFileInfo;
import com.dayang.archive.api.param.ArchiveParam;
import com.dayang.archive.api.param.CopyFileInfo;
import com.dayang.archive.api.param.DownloadFileInfo;
import com.dayang.archive.api.param.DownloadParam;
import com.dayang.archive.api.param.FileCopyParam;
import com.dayang.archive.structure.TaskInfo;
import com.dayang.archive.structure.TaskProgress;import junit.framework.TestCase;public class TestArchiveAPIWebServices extends TestCase {
Service service = null;
Call call = null;
protected void tearDown() throws Exception {
// TODO Auto-generated method stub
super.tearDown();
}
protected void setUp() throws Exception {
super.setUp();
//建立请求服务框架实例。
service = new Service();
//从框架中生成一个维护调用的实例。
call = (Call) service.createCall();
//注册输出参数类型
QName qn = new QName("urn:archive_api","TaskProgress");
call.registerTypeMapping(TaskProgress.class,qn,
new BeanSerializerFactory(TaskProgress.class, qn),
new BeanDeserializerFactory(TaskProgress.class, qn) ); QName qn1 = new QName("urn:archive_api","TaskInfo");
call.registerTypeMapping(TaskInfo.class,qn1,
new BeanSerializerFactory(TaskInfo.class, qn1),
new BeanDeserializerFactory(TaskInfo.class, qn1) ); QName qn2 = new QName("urn:archive_api","FileCopyParam");
call.registerTypeMapping(FileCopyParam.class,qn2,
new BeanSerializerFactory(FileCopyParam.class, qn2),
new BeanDeserializerFactory(FileCopyParam.class, qn2) );
QName qn3 = new QName("urn:archive_api","CopyFileInfo");
call.registerTypeMapping(CopyFileInfo.class,qn3,
new BeanSerializerFactory(CopyFileInfo.class, qn3),
new BeanDeserializerFactory(CopyFileInfo.class, qn3) );
QName qn4 = new QName("urn:archive_api","DownloadParam");
call.registerTypeMapping(DownloadParam.class,qn4,
new BeanSerializerFactory(DownloadParam.class, qn4),
new BeanDeserializerFactory(DownloadParam.class, qn4) );
QName qn5 = new QName("urn:archive_api","DownloadFileInfo");
call.registerTypeMapping(DownloadFileInfo.class,qn5,
new BeanSerializerFactory(DownloadFileInfo.class, qn5),
new BeanDeserializerFactory(DownloadFileInfo.class, qn5) );
QName qn6 = new QName("urn:archive_api","ArchiveFileInfo");
call.registerTypeMapping(ArchiveFileInfo.class,qn6,
new BeanSerializerFactory(ArchiveFileInfo.class, qn6),
new BeanDeserializerFactory(ArchiveFileInfo.class, qn6) );
QName qn7 = new QName("urn:archive_api","ArchiveParam");
call.registerTypeMapping(ArchiveParam.class,qn7,
new BeanSerializerFactory(ArchiveParam.class, qn7),
new BeanDeserializerFactory(ArchiveParam.class, qn7) );
// 调用的web服务的url地址
// call.setTargetEndpointAddress(new URL("http://localhost:8080/archive_api_ws/services/archive_api"));
call.setTargetEndpointAddress(new URL("http://localhost:8080/archive_api_ws/TestServlet"));
}
<student>
<id>200601</id>
<name>张寒</name>
<age>15</age>
</student>
<student>
<id>200602</id>
<name>陆飞</name>
<age>16</age>
</student>
<student>
<id>200603</id>
<name>钟其合</name>
<age>15</age>
</student>
<student>
<id>200604</id>
<name>董月霞</name>
<age>16</age>
</student>
<student>
<id>003</id>
<name>zhangsan</name>
<age>22</age>
</student>
<student>
<id>4030110</id>
<name>李四</name>
<age>22</age>
</student>
</studentset>
提供添加,删除,修改,并写到文件中!
仅供参考!~package student;import java.io.*;
import java.util.*;import org.dom4j.*;
import org.dom4j.io.*;public class Dom4jTest { // XML文件路径
private String xmlPath = null; // XML文档
private Document document = null; private SAXReader saxReader = null; // 存放names
Vector<String> vector = new Vector<String>(); // 存放<id, bean>
HashMap<String, StudentBean> hashMap = new HashMap<String, StudentBean>(); // 打开文档
public void openXML(String xmlPath) {
this.xmlPath = xmlPath;
saxReader = new SAXReader();
try {
this.document = saxReader.read(this.xmlPath);
// System.out.println("openXML() successful ...");
} catch (Exception e) {
System.out.println("openXML() Exception:" + e.getMessage());
}
} public Vector getElementNames() { Element root = this.document.getRootElement();// 取跟节点studntset
Element temp = null; for (Iterator i = root.elementIterator("student"); i.hasNext();) {
temp = (Element) i.next();
StudentBean studentBean = new StudentBean();
studentBean.setId(temp.elementTextTrim("id"));
studentBean.setName(temp.elementTextTrim("name"));
studentBean.setAge(temp.elementTextTrim("age"));
// System.out.println(temp.elementText("id"));
vector.add(studentBean.getName());
// System.out.println(vector);
String str = temp.elementTextTrim("id");
hashMap.put(str, studentBean);
// System.out.println("========================str="+str);
} Iterator it = hashMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry map = (Map.Entry) it.next();
System.out.println(map.getKey() + " - " + map.getValue());
}
this.writeToLocalFile(); return vector;
} // 写到本地文件
public void writeToLocalFile() {
File outFile = new File("c:/test.txt");
String strVector = ""; Iterator it = vector.iterator();
while (it.hasNext()) {
strVector += it.next() + " - ";
}
// strVector += "\r\n";
// System.out.println(strVector);
FileWriter fw = null;
BufferedWriter bw = null;
try {
// fw = new FileWriter(outFile);
fw = new FileWriter(outFile, true);
bw = new BufferedWriter(fw);
bw.write(strVector + "\r\n"); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} public void updateElement(String str1, String str2, String str3) { List list = document.selectNodes("/studentset/student");
Iterator iter = list.iterator();
while (iter.hasNext()) {
Element studentElement = (Element) iter.next();
Iterator iterator = studentElement.elementIterator(str1);
while (iterator.hasNext()) {
Node strElement = (Node) iterator.next();
if (strElement.getText().equals(str2)) {
strElement.setText(str3);
}
}
}
try {
this.writeXML(xmlPath);
} catch (IOException e) {
e.printStackTrace();
}
} public boolean loadDocument(String xmlPath) {
InputStreamReader isr = null;
BufferedReader br = null;
saxReader = new SAXReader();
try {
isr = new InputStreamReader(new FileInputStream(xmlPath));
br = new BufferedReader(isr);// 建立BufferedReader对象,并实例化为br
this.document = saxReader.read(br);
System.out.println(this.document.asXML());
return true;
} catch (DocumentException e) {
System.out.println(e.getMessage());
return false;
} catch (IOException e) {
System.out.println(e.getMessage());
return false;
}
} // 添加元素
public void addElement(String str1, String str2, String str3) { // System.out.println(this.document);
Element root = this.document.getRootElement();
// System.out.println(root);
List<String> list = new LinkedList<String>(); for (Iterator i = root.elementIterator("student"); i.hasNext();) {
Element temp = (Element) i.next();
list.add(temp.elementTextTrim("id"));
} if (list.indexOf(str1) == -1) {
// 添加员工列表 "student" 节点
Element studentElement = DocumentHelper.createElement("student");
root.add(studentElement); // 添加 "id" 节点
Element idElement = DocumentHelper.createElement("id");
idElement.setText(str1);
studentElement.add(idElement); // 添加 "name" 节点
Element nameElement = DocumentHelper.createElement("name");
nameElement.setText(str2);
studentElement.add(nameElement); // 添加 "age" 节点
Element ageElement = DocumentHelper.createElement("age");
ageElement.setText(str3);
studentElement.add(ageElement); try {
this.writeXML(xmlPath);
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("id 已经存在...");
System.exit(-1);
} } // 删除元素
public void delElement(String str) { List list = document.selectNodes("/studentset/student");
Iterator iter = list.iterator();
while (iter.hasNext()) {
Element rootElement = this.document.getRootElement();
Element studentElement = (Element) iter.next();
Iterator iterator = studentElement.elementIterator("id");
while (iterator.hasNext()) {
Element idElement = (Element) iterator.next();
if (idElement.getText().equals(str)) {
// studentElement.remove(idElement);
rootElement.remove(studentElement);
}
}
}
try {
this.writeXML(xmlPath);
} catch (IOException e) {
e.printStackTrace();
}
} // 写入文件
public void writeXML(String file) throws IOException {
// 美化格式
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GB2312");
XMLWriter writer = null;
try {
writer = new XMLWriter(new FileWriter(file), format);
writer.write(this.document);
} finally {
if (writer != null)
writer.close();
}
} public static void main(String[] args) { // Dom4jTest dt = new Dom4jTest();
// dt.openXML("F:/workspace/XMLProject/student.xml"); // dt.addElement("4030111", "王五", "22");
// dt.getElementNames();
// dt.delElement("4030111");
// dt.updateElement("id", "4030111", "22");
// dt.loadDocument("F:/workspace/XMLProject/student.xml"); }}