本文为博主原创,未经博主允许,不得转载:
xml具有强大的功能,在很多地方都会用的到。比如在通信的时候,通过xml进行消息的发送和交互。
在项目中有很多拼写xml的地方,进行一个简单的总结。
先举例如下:
在java中拼接如下的xml
<?xml version="1.0" encoding="UTF-8" ?> <req version="1.0"> <disk command="list" > <vpath name="fileName" dir="/"/> </disk> </req>
拼接后的形式为:
//拼装请求报文 StringBuilder reqXml = new StringBuilder(); reqXml.append("<?xml version="1.0" encoding="UTF-8" ?>"); reqXml.append("<req version="1.0">"); reqXml.append("<disk command="list" >"); reqXml.append("<vpath name=""+fileName+"" dir="/"/>"); reqXml.append("</disk>"); reqXml.append("</req>"); String requestXml = reqXml.toString();
总结如下两个特点:
1.在java中拼接xml时,为了防止“转义,需要通过"的形式进行保护。所以在拼接的时候,可先把所有的“”号进行转换。
2.在拼接时含有变量的时候,需要通过"+变量+" 的形式进行连接。
OK