「java返回xml格式」spring返回xml

博主:adminadmin 2023-03-22 06:03:09 950

今天给各位分享java返回xml格式的知识,其中也会对spring返回xml进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

JAVA调第三方接口返回XML文件用httpclient实现求大神给指示(新号就5分全给了)要一个工具类,一个用例

以下是一个http接口调用的例子:区别是返回的json数据,xml数据也是一样的,解析下xml数据就可以了,希望对你有帮助

public String getOrderCount() {

String mobile = ServletActionContext.getRequest().getParameter("mobile");

String urlStr = "";

urlStr += "?telphone=" + mobile + "endTime=" + DateUtil.getCurrentDay("yyyyMMdd") + "timeLength=2";

URL url = null;

HttpURLConnection httpurlconnection = null;

try {

url = new URL(urlStr);

// 以post方式请求

httpurlconnection = (HttpURLConnection) url.openConnection();

httpurlconnection.setDoOutput(true);

httpurlconnection.setRequestMethod("POST");

httpurlconnection.getOutputStream().flush();

httpurlconnection.getOutputStream().close();

// 获取响应代码

//int code = httpurlconnection.getResponseCode();

//System.out.println("code " + code);

// 获取页面内容

java.io.InputStream in = httpurlconnection.getInputStream();

java.io.BufferedReader breader = new BufferedReader(

new InputStreamReader(in, "utf-8"));

StringBuffer result = new StringBuffer();

String str = breader.readLine();

while (str != null) {

result.append(str);

str = breader.readLine();

}

HttpServletResponse response = ServletActionContext.getResponse();

response.setContentType("text/json;charset=utf-8");

response.getWriter().print(result);

} catch (Exception e) {

e.printStackTrace();

} finally {

if (httpurlconnection != null)

httpurlconnection.disconnect();

}

return null;

}

java 调用cxfWebService 接口返回类型是xml格式数据显示在jsp页面中该如何做到

dom4j解析xml,然后把解析的数据封装到bean里。jsp页面上获取bean然后显示就行了。

和平常的从数据库取数据,封装后在页面上显示没有区别。不过就是不去数据库取数据,而是靠xml来取数据。

java如何使sql查询的数据返回xml格式

你想输出什么样的XML格式

String s=null;

while(rs.next()){

s+=(rs.getString("member")+rs.getString("content"));

}

out.print(s);

修改成:

out.println("?xml version=\"1.0\" encoding=\"gbk\"?");

out.println("XmlMembers");

while(rs.next()){

out.println("XmlMember");

out.println("member![CDATA[");

out.println(rs.getString("member"));

out.println("]]/member");

out.println("content![CDATA[");

out.println(rs.getString("content"));

out.println("]]/content");

out.println("/XmlMember");

}

out.println("/XmlMembers");

rs.close();

c#调用java webservice怎么返回xml

这要取决于这个webservice返回的数据类型吧,如果是单纯的string数据,你们将接收到数据写入一个空的xml中,如果webservice本身返回回来的就是xml格式,你可以这么接收

XmlDocument xml = newXmlDocument();

xml.LoadXml(XmlNode.OuterXml) //XmlNode---为webservice返回xml的函数

JAVA以POST方式提交XML获取返回值(返回格式为XML)

URL url = new URL(requestUrl);

HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();

httpUrlConn.setRequestMethod(“POST”);

OutputStream outputStream = httpUrlConn.getOutputStream();

outputStream.write(xml);

outputStream.close();

InputStream inputStream = httpUrlConn.getInputStream();

InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

String str = null;

while ((str = bufferedReader.readLine()) != null) {

buffer.append(str);

}

bufferedReader.close();

inputStreamReader.close();

// 释放资源

inputStream.close();

inputStream = null;

httpUrlConn.disconnect();

用Java怎么把String类型的字符串转化为XML格式输出?

JXmlSerializable 是一个利用java反射,通过调用对象中所有以get(不区分大小写)开头的方法除去getClass方法,生成xml格式,希望与大家分享一下

下面是一个parent对象,包含两个child对象 生成的xml如下

Child类

1package xmlTest;

2

3import java.io.FileNotFoundException;

4import java.io.IOException;

5import java.io.PrintWriter;

6import java.lang.reflect.InvocationTargetException;

7public class Child extends JXmlSerializable {

8

9 private String _name;

10 private String _sex;

11 private int age;

12

13 public void setAge(int num) {

14 age = num;

15 }

16

17 public int getAge() {

18 return age;

19 }

20

21 public void setName(String name) {

22 _name = name;

23 }

24

25 public void setSex(String sex) {

26 _sex = sex;

27 }

28

29 public String getName() {

30 return _name;

31 }

32

33 public String getSex() {

34 return _sex;

35 }

36

37}

38

Parent类 1package xmlTest;

2

3import java.io.PrintWriter;

4import java.lang.reflect.Array;

5import java.util.*;

6

7public class Parent extends JXmlSerializable {

8

9 private String _name;

10 private String _sex;

11 private LinkedList list = new LinkedList();

12 private Vector vec = new Vector();

13 int age;

14

15 public void setAge(int num) {

16 age = num;

17 }

18

19 public int getAge() {

20 return age;

21 }

22

23 public void setName(String name) {

24 _name = name;

25 }

26

27 public void setSex(String sex) {

28 _sex = sex;

29 }

30

31 public String getName() {

32 return _name;

33 }

34

35 public String getSex() {

36 return _sex;

37 }

38

39 public void addChild(Child child) {

40 list.add(child);

41 vec.add(child);

42 }

43

44 public Child[] getChild() {

45

46 Child[] aa = new Child[vec.size()];

47 // list.toArray(aa);

48 vec.toArray(aa);

49 return aa;

50 }

51

52 public static void main(String[] args) {

53 // TODO Auto-generated method stub

54 try {

55 Parent pat = new Parent();

56 pat.setName("jack");

57 pat.setSex("male");

58 Child child1 = new Child();

59 child1.setName("tom");

60 child1.setSex("male");

61 pat.addChild(child1);

62 Child child2 = new Child();

63 child2.setName("Marie");

64 child2.setSex("female");

65 pat.addChild(child2);

66 pat.getChild();

67 PrintWriter out = new PrintWriter("abc.xml");

68 pat.toXmlSerial(out,0);

69 out.flush();

70

71 } catch (Exception e) {

72 e.printStackTrace();

73 }

74

75 }

76}

类 JXmlSerializable

1package xmlTest;

2

3import java.lang.reflect.Method;

4import java.lang.reflect.InvocationTargetException;

5import java.lang.reflect.Array;

6import java.io.PrintWriter;

7import java.io.IOException;

8public class JXmlSerializable {

9

10 public void toXmlSerial(PrintWriter out, int num)

11 throws InvocationTargetException, IllegalAccessException,

12 IOException {

13 out.write("?xml version="1.0"? ");

14 String head = "";

15 for (int i = 0; i num; i++) {

16 head += " ";

17 }

18 out.write(head + "" + this.getClass().getName() + " ");

19 Method[] methods = this.getClass().getMethods();

20 for (int i = 0; i methods.length; i++) {

21 Class[] paras = methods[i].getParameterTypes();

22 String name = methods[i].getName();

23 if (paras == null || paras.length == 0) {

24 if ((name.substring(0, 3).toLowerCase().equals("get"))

25 !name.equals("getClass")) {

26 Object obj = methods[i].invoke(this, null);

27 getMethodXmlSerial(out, obj, methods[i], num);

28 }

29 }

30 }

31

32 out.write(head + "/" + this.getClass().getName() + " ");

33

34 }

35

36 private void getMethodXmlSerial(PrintWriter out, Object obj, Method method,

37 int num) throws InvocationTargetException, IllegalAccessException,

38 IOException {

39 if (obj == null)

40 return;

41 String head = "";

42 for (int i = 0; i = num; i++) {

43 head += " ";

44 }

45 if (obj.getClass().isArray()) {

46 for (int i = 0; i Array.getLength(obj); i++) {

47 Object childobj = Array.get(obj, i);

48 if (childobj instanceof JXmlSerializable) {

49 ((JXmlSerializable) childobj).toXmlSerial(out, num + 1);

50 } else {

51 getMethodXmlSerial(out, childobj, method, num);

52 }

53 }

54 } else {

55 out.write(head + " " + method.getName().substring(3) + " ");

56 out.write(obj.toString());

57 out.write(" /" + method.getName().substring(3) + " ");

58 }

59

60 }

61}

编译出来还可以,能够达到我的理想。

编译结果是

1?xml version="1.0"?

2xmlTest.Parent

3 Name jack /Name

4 Age 0 /Age

5 Sex male /Sex

6?xml version="1.0"?

7 xmlTest.Child

8 Name tom /Name

9 Age 0 /Age

10 Sex male /Sex

11 /xmlTest.Child

12?xml version="1.0"?

13 xmlTest.Child

14 Name Marie /Name

15 Age 0 /Age

16 Sex female /Sex

17 /xmlTest.Child

18/xmlTest.Parent

今天看了看java.beans包,发现了两个好东西,XMLEncoder和XMLDecoder。发现自己以前把从XML存取对象真是太费力气啦。做了小工具类,以后可以用用了。

1以下是引用片段:

2package com.imct.util;

3import java.beans.XMLDecoder;

4import java.beans.XMLEncoder;

5import java.io.File;

6import java.io.FileInputStream;

7import java.io.FileNotFoundException;

8import java.io.FileOutputStream;

9import java.io.IOException;

10import java.util.ArrayList;

11import java.util.List;

12/** *//**

13 * title使用XML文件存取可序列化的对象的类/title

14 * description提供保存和读取的方法/description

15 * @author 殷晋

16 * copyright清华大学汽车工程开发研究院@2005/copyright

17 * @version 1.0

18 * 2005-8-5 16:44:49

19 */

20public class ObjectToXMLUtil

21{

22 /** *//**

23 * 把java的可序列化的对象(实现Serializable接口)序列化保存到XML文件里面,如果想一次保存多个可序列化对象请用集合进行封装

24 * 保存时将会用现在的对象原来的XML文件内容

25 * @param obj 要序列化的可序列化的对象

26 * @param fileName 带完全的保存路径的文件名

27 * @throws FileNotFoundException 指定位置的文件不存在

28 * @throws IOException 输出时发生异常

29 * @throws Exception 其他运行时异常

30 */

31 public static void objectXmlEncoder(Object obj,String fileName)

32 throws FileNotFoundException,IOException,Exception

33 {

34 //创建输出文件

35 File fo = new File(fileName);

36 //文件不存在,就创建该文件

37 if(!fo.exists())

38 {

39 //先创建文件的目录

40 String path = fileName.substring(0,fileName.lastIndexOf('.'));

41 File pFile = new File(path);

42 pFile.mkdirs();

43 }

44 //创建文件输出流

45 FileOutputStream fos = new FileOutputStream(fo);

46 //创建XML文件对象输出类实例

47 XMLEncoder encoder = new XMLEncoder(fos);

48 //对象序列化输出到XML文件

49 encoder.writeObject(obj);

50 encoder.flush();

51 //关闭序列化工具

52 encoder.close();

53 //关闭输出流

54 fos.close();

55 }

56 /** *//**

57 * 读取由objSource指定的XML文件中的序列化保存的对象,返回的结果经过了List封装

58 * @param objSource 带全部文件路径的文件全名

59 * @return 由XML文件里面保存的对象构成的List列表(可能是一个或者多个的序列化保存的对象)

60 * @throws FileNotFoundException 指定的对象读取资源不存在

61 * @throws IOException 读取发生错误

62 * @throws Exception 其他运行时异常发生

63 */

64 public static List objectXmlDecoder(String objSource)

65 throws FileNotFoundException,IOException,Exception

66 {

67 List objList = new ArrayList();

68 File fin = new File(objSource);

69 FileInputStream fis = new FileInputStream(fin);

70 XMLDecoder decoder = new XMLDecoder(fis);

71 Object obj = null;

72 try

73 {

74 while( (obj = decoder.readObject()) != null)

75 {

76 objList.add(obj);

77 }

78 }

79 catch (Exception e)

80 {

81 // TODO Auto-generated catch block

82 }

83 fis.close();

84 decoder.close();

85 return objList;

86 }

87}

88

89

90当然用Beans.instantiate也可以从文件中反序列化初对象

java返回xml格式的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于spring返回xml、java返回xml格式的信息别忘了在本站进行查找喔。