gpt4 book ai didi

java - Jaxb 和 StringBuffer 哪一个在 Xml 字符串创建方面具有更好的性能?

转载 作者:行者123 更新时间:2023-12-01 13:50:34 25 4
gpt4 key购买 nike

我必须创建以下 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>

使用 JAXB 创建 XML 字符串

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 创建 XML 字符串

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());

1。 JAXB 和 StringBuffer 的性能哪个更好?

2。除了上述两种之外还建议其他方法吗?

最佳答案

StringBuffer代码可读性较差。查看 StAX (XMLStreamWriter)。

关于java - Jaxb 和 StringBuffer 哪一个在 Xml 字符串创建方面具有更好的性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19994538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com