英文:
How to remove extra added text 
 from xml in Springboot application
问题
你想要的XML输出应该去除每个新行后面的 
。以下是修改建议:
<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格式,没有额外的 
。
英文:
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 
 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("&#xd;", System.lineSeparator());
Thank you all for help.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论