xml 格式消息

场景:发送请求获取用户信息

传输消息格式: xml

传输消息协议:TCP/IP,http

客户端请求xml消息:

<?xmlversion="1.0"encoding="utf-8"standalone="yes"?>
<message>
<head>
<auth>001</auth>
<sign>abc</sign>
<version>1</version>
</head>
<body>
<age>20</age>
</body>
</message

服务端响应xml消息:

<?xmlversion="1.0"encoding="utf-8"standalone="yes"?>
<message>

<head>
<recode>0000</recode>
<reshow>返回成功</reshow>
<version>1</version>
</head>
<body>
<list>
<map>
<id>1</id>
<name>john</name>
<age>20</age>
</map>
<map>
<id>2</id>
<name>smith</name>
<age>20</age>
</map>
</list>
</body>
</message>

java 请求xml消息生成

DemoMessagemsg=newDemoMessage();

DemoHeadhead=newDemoHead();
head.setAuth("001");head.setSign("abc");

DemoBodybody=newDemoBody();
body.setAge("20");
body.setName("john");

msg.setHead(head);
msg.setBody(body);
//生成请求xml字符串
JAXBUtilutil=newJAXBUtil();
StringxmlStr=util.objectToXmlStr(msg,DemoMessage.class);

System.out.println(xmlStr);

java 解析响应xml

DemoRspMessagerspMsg1=newDemoRspMessage();
JAXBUtilutil=newJAXBUtil();
rspMsg1=util.xmlToObj(rspMsg1,xmlrspStr);

java xml字符串,java对象转换工具类

publicclassJAXBUtil{

@SuppressWarnings("unchecked")
publicStringobjectToXmlStr(Objectobj,ClassbeanClass)throwsException{
JAXBContextcontext=JAXBContext.newInstance(beanClass);
//根据上下文获取marshaller对象
Marshallermarshaller=context.createMarshaller();
//设置编码字符集
marshaller.setProperty(Marshaller.JAXB_ENCODING,"UTF-8");
//格式化XML输出,有分行和缩进
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);

ByteArrayOutputStreambaos=newByteArrayOutputStream();
marshaller.marshal(obj,baos);
StringxmlObj=newString(baos.toByteArray());
returnxmlObj.replace("standalone=\"yes\"","");
}

/**
*@Description:将对象转换为XML并且写入文件
*
*@paramobj
*@parambeanClass
*@paramfile
*@throwsException


*/
@SuppressWarnings("unchecked")
publicvoidobjectToXmlStr(Objectobj,ClassbeanClass,Filefile)throwsException{
JAXBContextcontext=JAXBContext.newInstance(beanClass);
//根据上下文获取marshaller对象
Marshallermarshaller=context.createMarshaller();
//设置编码字符集
marshaller.setProperty(Marshaller.JAXB_ENCODING,true);
//打印到控制台
marshaller.marshal(obj,System.out);
marshaller.marshal(obj,file);
}

/**
*@Description:XML转换为对象
*
*@param<T>
*@paramfile
*@parambeanClass
*@return
*@throwsException
*/
@SuppressWarnings("unchecked")
public<T>TxmlStrToObject(Filefile,Class<T>beanClass)throwsException{
Tbean=beanClass.newInstance();
JAXBContextcontext=JAXBContext.newInstance(beanClass);
Unmarshallerunmarshaller=context.createUnmarshaller();
bean=(T)unmarshaller.unmarshal(file);
returnbean;
}

/**
*
*@paramobjxml转换后的数据存放的对象
*@paramxml要转换的xml
*@return转换成的对象
*/
@SuppressWarnings("unchecked")
public<T>TxmlToObj(Tobj,Stringxml){
StringReaderreader;
JAXBContextcontext;
Unmarshallerunmarshaller;
reader=newStringReader(xml);
try{
context=JAXBContext.newInstance(obj.getClass());
}catch(JAXBExceptione){
System.out.println("创建转换器环境实例JAXBcontext实例出现异常:"+e.getCause()+"/"+e.getCause());
returnnull;
}

try{
unmarshaller=context.createUnmarshaller();
}catch(JAXBExceptione){
System.out.println("创建对象数据转xml数据转换器出现异常:"+e.getCause()+"/"+e.getCause());
returnnull;
}

Tretobj=null;
try{
retobj=(T)unmarshaller.unmarshal(reader);
}catch(JAXBExceptione){
System.out.println("xml数据转对象数据出现异常:"+e.getCause()+"/"+e.getCause());
}

returnretobj;
}
}

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念