我必须创建以下xml字符串,
<TRX>
<SVC>CheckoutStartSession</SVC>
<PRJ>PRJ</PRJ>
<CTY>ABC</CTY>
<COM>Internet</COM>
<CUR>DEF</CUR>
<ORD>1234</ORD>
<NET>1.0</NET>
<TAX>0.0</TAX>
<GRS>1.0</GRS>
<SES>
<SUU>SCHEME://IP:PORT/CONTEXT_PATH/SUU</SUU>
<FAU>SCHEME://IP:PORT/CONTEXT_PATH/FAU</FAU>
<STY>SCHEME://IP:PORT/CONTEXT_PATH/STY</STY>
<CVI>Required</CVI>
<ACT>AUTHORIZE</ACT>
<LAN>en-US</LAN>
</SES>
<CUS>
<CTP>PRIVATE</CTP>
<FNM>PRATEEK</FNM>
</CUS>
</TRX>
public class PGReqConfig {
public static void main(String[] args) throws JAXBException {
RequestSession sessionConfig = new RequestSession();
sessionConfig.setSuccessNotifURL("SCHEME://IP:PORT/CONTEXT_PATH/SUU");
sessionConfig.setFailureNotifURL("SCHEME://IP:PORT/CONTEXT_PATH/FAU");
sessionConfig.setStyleSheetURL("SCHEME://IP:PORT/CONTEXT_PATH/STY");
sessionConfig.setAction("AUTHORIZE");
sessionConfig.setCvcInput("Required");
sessionConfig.setLaguage("en-US");
CustomerDetails customerData = new CustomerDetails();
customerData.setCustomerType("PRIVATE");
customerData.setFirstName("PRATEEK");
TransactionRequest transactionConfig = new TransactionRequest();
transactionConfig.setService("CheckoutStartSession");
transactionConfig.setConsentMedium("Internet");
transactionConfig.setProjectCode("PRJ");
transactionConfig.setCountryCode("ABC");
transactionConfig.setCurrencyCode("DEF");
transactionConfig.setNet("1.0");
transactionConfig.setTax("0.0");
transactionConfig.setGross("1.0");
transactionConfig.setOrderId("1234");
transactionConfig.setCustomerData(customerData);
transactionConfig.setPaymentSession(sessionConfig);
StringWriter xmlData = new StringWriter();
JAXBContext context = JAXBContext.newInstance(TransactionRequest.class);
Marshaller marshal = context.createMarshaller();
marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshal.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshal.marshal(transactionConfig, xmlData);
System.out.println(xmlData.toString());
}
}
@XmlRootElement(name = "TRX")
@XmlAccessorType(XmlAccessType.FIELD)
class TransactionRequest{
@XmlElement(name = "SVC")
private String service;
@XmlElement(name = "PRJ")
private String projectCode;
@XmlElement(name = "CTY")
private String countryCode;
@XmlElement(name = "COM")
private String consentMedium;
@XmlElement(name = "CUR")
private String currencyCode;
@XmlElement(name = "ORD")
private String orderId;
@XmlElement(name = "NET")
private String netAmount;
@XmlElement(name = "TAX")
private String taxAmount;
@XmlElement(name = "GRS")
private String grossAmount;
@XmlElement(name = "SES")
private RequestSession paymentSession;
@XmlElement(name = "CUS")
private CustomerDetails customerData;
......
}
@XmlAccessorType(XmlAccessType.FIELD)
class RequestSession{
@XmlElement(name = "SUU")
private String successNotifURL;
@XmlElement(name = "FAU")
private String failureNotifURL;
@XmlElement(name = "STY")
private String styleSheetURL;
@XmlElement(name = "CVI")
private String cvcInput;
@XmlElement(name = "ACT")
private String action;
@XmlElement(name = "LAN")
private String laguage;
......
}
@XmlAccessorType(XmlAccessType.FIELD)
class CustomerDetails{
@XmlElement(name = "CTP")
private String customerType;
@XmlElement(name = "FNM")
private String firstName;
......
}
StringBuffer trxTag = new StringBuffer();
trxTag.append("<TRX>" + "<SVC>CheckoutStartSession</SVC>");
trxTag.append("<PRJ>PRJ</PRJ>");
trxTag.append("<CTY>ABC</CTY>");
trxTag.append("<COM>Internet</COM>");
trxTag.append("<CUR>DEF</CUR>");
StringBuffer cusTag = new StringBuffer("<CUS>");
cusTag.append("<CTP>Private</CTP>")
.append("<FNM>PRATEEK</FNM>")
.append("</CUS>");
StringBuffer sesTag = new StringBuffer("<SES>");
sesTag.append("<SUU>SCHEME://IP:PORT/CONTEXT_PATH/SUU</SUU>")
.append("<FAU>SCHEME://IP:PORT/CONTEXT_PATH/FAU</FAU>")
.append("<STY>SCHEME://IP:PORT/CONTEXT_PATH/STY</STY>")
.append("<CVI>Required</CVI>")
.append("<LAN>en-US</LAN>")
.append("<ACT>Authorize</ACT>" + "</SES>");
trxTag.append("<ORD>1234</ORD>")
.append("<NET>1.0</NET>")
.append("<TAX>0.0</TAX>")
.append("<GRS>1.0</GRS>")
.append(cusTag)
.append(sesTag)
.append("</TRX>");
System.out.println(trxTag.toString());
答案 0 :(得分:1)
StringBuffer代码可读性差。查看StAX(XMLStreamWriter)。
答案 1 :(得分:0)
好吧,如果这是单向操作,并且如果XML在问题中一样简单并且没有及时更改,那么JAXB
在性能方面没有机会,StringBuffer
是更快。