如何从对象制作XML

时间:2018-08-16 03:40:14

标签: java xml object kotlin xml-parsing

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

如果是json,我只需要使用jaskson或Gson

json = someObj.writeValueAsString

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

但是我的目的是要(从对象)进行信任并将XML解析为其他RESTful API。

如果有人可以帮助您进行解析,我也会很高兴。我的意思是喜欢如何调用Http Request并将xml放入正文中并触发某些网址。

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>

1 个答案:

答案 0 :(得分:2)

JAXB自版本6起随Java sdk一起提供,并提供了一种简单的方法来mashalling和Unmashalling 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);