jackson-dataformat-xml icon indicating copy to clipboard operation
jackson-dataformat-xml copied to clipboard

Allow preserving "is-attribute" with JsonNode to write output as attribute value

Open cihadbaskoy opened this issue 9 years ago • 2 comments

I want to map a JsonNode to a XML. If I directly call XmlMapper write methods, this converts all the SubNode's of JsonNode as XMLElement which is fine.

But I want one of the field/subfield convert as XMLAttribute.

Is there a way to tell the mapper 'this node is not element, please convert it as attribute?'

Check the OrgnlIntrBkSttlmAmt field below (XML and json format is not preserved, sorry for that)

A sample JSON file is as follows :

{
    "FIToFIPmtCxlReq": {
        "Undrlyg": [
            {
                "TxInf": [
                    {
                        "CxlId": "3EfXiRbNk517MHY",
                        "OrgnlInstrId": "0amcNeP5QhXnY9x3Ykkj5TScqoTQF",
                        "OrgnlIntrBkSttlmAmt": {
                            "Ccy": "EUR",
                            "Value": 1462216
                        }
                    }
                ]
            }
        ]
    }
}

The converted XML is as follows

<?xml version='1.1' encoding='UTF-8'?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.056.001.01">
	<FIToFIPmtCxlReq>
		<Undrlyg>
			<TxInf>
				<CxlId>3EfXiRbNk517MHY</CxlId>
				<OrgnlInstrId>0amcNeP5QhXnY9x3Ykkj5TScqoTQF</OrgnlInstrId>
				<OrgnlEndToEndId>yy</OrgnlEndToEndId>
				<OrgnlIntrBkSttlmAmt>
					<Ccy>EUR</Ccy>
					<Value>1462216</Value>
				</OrgnlIntrBkSttlmAmt>
			</TxInf>
		</Undrlyg>
	</FIToFIPmtCxlReq>
</Document>

The requested output is as follows:

<?xml version='1.1' encoding='UTF-8'?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.056.001.01">
	<FIToFIPmtCxlReq>
		<Undrlyg>
			<TxInf>
				<CxlId>3EfXiRbNk517MHY</CxlId>
				<OrgnlInstrId>0amcNeP5QhXnY9x3Ykkj5TScqoTQF</OrgnlInstrId>
				<OrgnlEndToEndId>yy</OrgnlEndToEndId>
				<OrgnlIntrBkSttlmAmt Ccy="EUR">1462216</OrgnlIntrBkSttlmAmt>
			</TxInf>
		</Undrlyg>
	</FIToFIPmtCxlReq>
</Document>

cihadbaskoy avatar Dec 09 '16 10:12 cihadbaskoy

The classes and code pieces I created for this issue is as follows

@JacksonXmlRootElement(localName = "Document") private static class Wrapper {

    @JacksonXmlProperty(localName = "xmlns", isAttribute = true) private String namespace;

    @JacksonXmlProperty(localName = "FIToFIPmtCxlReq", isAttribute = false) private JsonNode payload;
}


    Wrapper wrapper = new Wrapper();
    wrapper.namespace = "urn:iso:std:iso:20022:tech:xsd:camt.056.001.01";
    wrapper.payload = jsonNode.get("FIToFIPmtCxlReq");

    XmlMapper xmlMapper = new XmlMapper();
    xmlMapper.configure(WRITE_XML_1_1, true);
    String output = xmlMapper.writeValueAsString(wrapper);

cihadbaskoy avatar Dec 09 '16 10:12 cihadbaskoy

@cihadbaskoy Keep in mind that JsonNode is not officially supported with XML module. So it may not work at all for this purpose.

But beyond this, there is no way to indicate special handling wrt attribute/element distinction with tree model.

cowtowncoder avatar Dec 10 '16 01:12 cowtowncoder