Java中的XML内容

wmvff8tz  于 5个月前  发布在  Java
关注(0)|答案(3)|浏览(48)

我正在尝试使用httpclient发布xml请求,如下所示:

String parm1 = MyXml.toString();
PostMethod post = new Postmethod(url);
post.setRequestEntity(new StringRequestEntity(parm1));
...

字符串
我有一个对象在程序中,我想把它转换为xml表示。
我的问题是,在java中创建xml格式的Myxml的最佳方法是什么,然后我可以简单地打印出它的String格式。
谢谢.

polkgigr

polkgigr1#

在Java中创建XML有很多方法,下面的答案How to serialize and de-serialize objects using JAXB?提供了一个很好的演示,展示了一种似乎适合您的用例的常用方法。

9cbw7uwe

9cbw7uwe2#

以下是如何使用Apache HttpClient提交xml请求。

  • 使用apache Velocity创建请求xml格式
  • 使用Castor将响应流(respReader)转换为java对象
final String request = createXmlRequest(); // helper method to create the xml request
final HttpClient client = new HttpClient();
final PostMethod post = new PostMehod(url); // url - www.google.cm/someoperaion

post.setRequestHeader("Content-Language", "en-US");
post.setRequestEntity(new StringRequestEntity(request, "text/xml", "ISO-8859-1"));

final int returnCode = client.executeMethod(post);

final BufferedReader respReader = new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream()));

字符串

nue99wik

nue99wik3#

试着用这种方式...

public void postData() throws Exception {

 HttpClient client = new DefaultHttpClient();
 HttpPost httppost = new HttpPost("https://www.xyz.com");

 List<NameValuePair> list = new ArrayList<NameValuePair>(1);

 list.add(new BasicNameValuePair("name","ABC");

 httppost.setEntity(new UrlEncodedFormEntity(list));

 HttpResponse r = client.execute(httppost);

}

字符串

相关问题