gpt4 book ai didi

java - 如何从对象生成 XML

转载 作者:行者123 更新时间:2023-11-30 02:04:04 25 4
gpt4 key购买 nike

我想问一下如何从对象生成xml?

假设是json,我只需要使用jaskson或Gson

json = someObj.writeValueAsString

但是对于我通过互联网搜索的 xml,似乎需要首先信任该文档?就像将其写入流写入器然后输出到某个目录成为文件(xxx.xml)?

但我的目的是(从对象)construst 并将 xml 解析为其他 RESTful API。

如果有人可以帮助解析我会非常高兴。我的意思是如何调用 Http Request 并将 xml 放入正文中并触发某个 url。

val url = "https://api.mch.weixin.qq.com/pay/unifiedorder"
val headers = HttpHeaders()
headers.contentType = MediaType.APPLICATION_XML
val xmlMapper = XmlMapper()
var strObject = xmlMapper.writeValueAsString(wxPayOrderWithSign).replace(Regex("<[^>]*?/>"), "")

strObject = strObject.substring(12,strObject.length - 13)
strObject = "<xml>$strObject</xml>"

val entity = HttpEntity(strObject, headers)


val respEntity = restTemplate.postForEntity(url, entity, String::class.java)
val return_msg = respEntity.body

这是我在 kotlin 中的代码,但这不是正确的方法,因为我在对象转换为字符串期间使用正则表达式替换某些字符。

我需要生成类似的东西

<xml>
<appid>wx0b6d2803d20b379f1</appid>
<body>QQMember-TopUp</body>
<detail>test</detail>
<mch_id>1508478951</mch_id>
<nonce_str>c9c21120a9724ee993e6f9c866ec30e1</nonce_str>
<notify_url>http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php</notify_url>
<out_trade_no>20150806125346</out_trade_no>
<sign>6E18248C5FFA26D1A96BD8F6A0B0CB02</sign>
<spbill_create_ip>123.12.12.123</spbill_create_ip>
<total_fee>1</total_fee>
<trade_type>JSAPI</trade_type>
</xml>

最佳答案

JAXB 从版本 6 开始就附带了 java sdk,并提供了一种简单的方法来编码和解码 java 对象:

@XmlRootElement
public class Transaction {

private Long id;
private Float value;
private Boolean authorized;

public static void main(String[] args) throws JAXBException {

Transaction transaction = new Transaction();
transaction.setId(1L);
transaction.setValue(20.9f);
transaction.setAuthorized(true);

JAXBContext jaxbContext = JAXBContext.newInstance( Transaction.class );
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );

jaxbMarshaller.marshal( transaction, new File( "transaction.xml" ) );//save to file
jaxbMarshaller.marshal( transaction, System.out ); //send to stdout
//raw xml as string
StringWriter writer = new StringWriter();
jaxbMarshaller.marshal( transaction, writer);
String rawXml = writer.toString();

}
}

输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<transaction>
<authorized>true</authorized>
<id>1</id>
<value>20.9</value>
</transaction>

使用 RestTemplate,您可以在 HttpEntity 构造函数中传递应以 XML 形式发送的对象,例如:

Transaction transaction = new Transaction();
transaction.setId(1L);
transaction.setValue(20.9f);
transaction.setAuthorized(true);

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_XML);
HttpEntity<Transaction> request = new HttpEntity<Transaction>(transaction, headers);

ResponseEntity<String> response = restTemplate.postForEntity("http://localhost:8080/xml/transactions", request, String.class);

关于java - 如何从对象生成 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51869278/

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