@POST
@Path("/add")
public Response addUser(
@FormParam("fname") String fname,
@FormParam("lname") String lname,@FormParam("mobile") String mobile,@FormParam("city") String city ,@FormParam("product") String product ) {
java.util.Date d = new Date();
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
String date=sd.format(d);
// Date xml_date=sd.parse(date);
//XMLGregorianCalendar xml_date=DatatypeFactory.newInstance().newXMLGregorianCalendar(date);
System.out.println("simple formate"+sd.format(d));
System.out.println("String form"+date);
int RM_ID=13455;
String rm=Integer.toString(RM_ID);
int sibel=1213;
String sibel_val=Integer.toString(sibel);
int emp=121;
String emp_code=Integer.toString(emp);
int branch=1212;
String branch_code=Integer.toString(branch);
int rq_id=12231;
String req=Integer.toString(rq_id);
// This is xml object with Request.xml format so this xml tag, i want tag with string variables :
String xml = "<?xml version='1.0' encoding='utf-8'?>"
+"<Create_BB_Lead mlns='http://www.kotak.com/Schemas/Create_BB_Lead_Req.xsd'>"
+"<RqUID></RqUID>"
+"<FIRST_NAME></FIRST_NAME>"
+"<LAST_NAME></LAST_NAME>"
+"<MOBILE></MOBILE>"
+"<PRODUCT_CODE></PRODUCT_CODE>"
+"<LOS_LOCATION></LOS_LOCATION>"
+"<SOURCE></SOURCE>"
+"<REMARKS></REMARKS>"
+"<RM_ID></RM_ID>"
+"<LEAD_TYPE></LEAD_TYPE>"
+"<SIEBEL_ID></SIEBEL_ID>"
+"<COMPANY_NAME></COMPANY_NAME>"
+"<EMP_CODE></EMP_CODE>"
+"<BRANCH_CODE></BRANCH_CODE>"
+"<LOGINID></LOGINID>"
+"<CREATED_DATE></CREATED_DATE>"
+"<CREATED_BY></CREATED_BY>"
+"</Create_BB_Lead>";
//This is my xml object.
xml.replaceAll("$RqUID", req);
xml.replaceAll("$FIRST_NAME", fname);
xml.replaceAll("$LAST_NAME", lname);
xml.replaceAll("$MOBILE",mobile);
xml.replaceAll("$PRODUCT_CODE", product);
xml.replaceAll("$LOS_LOCATION", city);
xml.replaceAll("$SOURCE", null);
xml.replaceAll("$REMARKS", null);
xml.replaceAll("$RM_ID",rm);
xml.replaceAll("$LEAD_TYPE", null);
xml.replaceAll("$SIEBEL_ID", sibel_val);
xml.replaceAll("$COMPANY_NAME", null);
xml.replaceAll("$EMP_CODE", emp_code);
xml.replaceAll("$BRANCH_CODE", branch_code);
xml.replaceAll("$LOGINID", null);
xml.replaceAll("$CREATED_DATE", date);//2018-03-20
xml.replaceAll("$CREATED_BY",fname);
System.out.println(xml);
MakeApi api=new MakeApi();
try {
String response=api.invokeHTTPURL(url, xml);
//System.out.println(response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I am passing the these string variables in xml tag. when i am passing these string variables getting those variables are not appended with the xml format.
答案 0 :(得分:0)
在你的情况下,我会完全使用Velocity库来向你的xml字符串添加信息。例如
VelocityEngine ve = new VelocityEngine();
ve.init();
//set your variables
VelocityContext context = new VelocityContext();
context.put("name", "Daniel");
context.put("message", "Hello Word");
//your template XML
String strTemplate = "<xml><name>$name</name><msg>$message</msg></xml>";
StringWriter writer = new StringWriter();
ve.evaluate(context, writer, "error.log", strTemplate);
System.out.println( writer.toString() );
//<xml><name>Daniel</name><msg>Hello Word</msg></xml>
有更多最佳方法可以做到这一点,所以我建议您查看Velocity教程https://www.javaworld.com/article/2075966/core-java/start-up-the-velocity-template-engine.html
速度maven repo https://mvnrepository.com/artifact/org.apache.velocity/velocity/1.7
答案 1 :(得分:0)
您的XML模板必须至少看起来像这样:
String xml = "<?xml version='1.0' encoding='utf-8'?>"
+"<Create_BB_Lead mlns='http://www.kotak.com/Schemas/Create_BB_Lead_Req.xsd'>"
+"<RqUID>$RqUID</RqUID>"
+"<FIRST_NAME>$FIRST_NAME</FIRST_NAME>"
+"<LAST_NAME>$LAST_NAME</LAST_NAME>"
... etc for all elements
+"</Create_BB_Lead>";
除此之外,这是一种构造XML的可怕且容易出错的方式,它最终会导致XML格式不正确,导致 收件人无法解析它。
使用真正的XML API(DOM,DOM4J或JDOM)构建XML,或使用JAXB。
答案 2 :(得分:0)
如果您不能使用速度库,并且想要避免使用正则表达式,则可以尝试使用 java.text.MessageFormat 。例如
String xml = "<xml><content>"
+ "<sender>{0}</sender>"
+ "<email>{1}</email>"
+ "<type>{2}</type>"
+ "<date>{3}</date>"
+ "<reply-to>{1}</reply-to>"
+ "</content></xml>";
String[] values = {
"Daniel",
"daniel@hi.com",
"invoice #123",
"2018-03-22"
};
System.out.println(MessageFormat.format(xml, values));
这将打印
<xml>
<content>
<sender>Daniel</sender>
<email>daniel@hi.com</email>
<type>invoice #123</type>
<date>2018-03-22</date>
<reply-to>daniel@hi.com</reply-to>
</content>
</xml>
但是,你应该小心你的String []值顺序。