如何从Springboot应用程序的XML中删除额外添加的文本

huangapple go评论61阅读模式
英文:

How to remove extra added text 
 from xml in Springboot application

问题

你想要的XML输出应该去除每个新行后面的 &#xd。以下是修改建议:

<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>

这应该会得到你想要的XML格式,没有额外的 &#xd

英文:

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
        CustomerName: John Wick
        DSLAM Pozicija: Dawa_daslkads 342
   </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?

答案1

得分: 0

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

This is the code

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

Thank you all for help.

英文:

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

This is the code

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

Thank you all for help.

huangapple
  • 本文由 发表于 2023年5月11日 17:14:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定