Search code examples
javaxmlspring-boot

How to remove extra added text 
 from xml in Springboot application


I am trying to convert JSON object into XML with method:

xmlMapper.writeValueAsString()

This is the code:

    XmlMapper xmlMapper = new XmlMapper();
    String xml = "";

    xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    xmlMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

    try {
        xml = xmlMapper.writeValueAsString(inputJsonObject);
    }catch (Exception e){
        e.printStackTrace();
        throw new Exception("Error!");
    }

This is the JSON input (inputJsonObject)

 {
     "Atribut": [
            {
                "RnatrId": 71945793,
                "TxtValue": "AssetId: 545654, Aktiviran: 
                  DA\r\nCustomerName: John Wick\r\n
                  DSLAM Pozicija: Dawa_daslkads 342",
                "NumValue": 0,
                "DateValue": "",
                "MultiselectId": "",
                "MultiselectTxt": ""
            },
           ]
     }
  }

This is what I get

<Atribut>
   <RnatrId>71945793</RnatrId>
   <TxtValue>
        AssetId: 545654, Aktiviran: DA&#xd;
        CustomerName: John Wick&#xd;
        DSLAM Pozicija: Dawa_daslkads 342&#xd;
   </TxtValue>
    <NumValue>0</NumValue>
    <DateValue></DateValue>
    <MultiselectId></MultiselectId>
    <MultiselectTxt></MultiselectTxt>
</Atribut>

This is what I am trying to get:

<Atribut>
       <RnatrId>71945793</RnatrId>
       <TxtValue>
            AssetId: 545654, Aktiviran: DA
            CustomerName: John Wick
            DSLAM Pozicija: Dawa_daslkads 342
       </TxtValue>
        <NumValue>0</NumValue>
        <DateValue></DateValue>
        <MultiselectId></MultiselectId>
        <MultiselectTxt></MultiselectTxt>
    </Atribut>

With out this extra text &#xd for each new line.

Any help please?


Solution

  • I solved the problem buy adding .replace(" ", System.lineSeparator()) method.

    This is the code

    xml = xmlMapper.writeValueAsString(inputJsonObject).replace("&#xd;", System.lineSeparator()); 
    

    Thank you all for help.