1

I have a requirement according to which i need to send some trailing whitespaces(tabs) in the end of xml tag like below:-

<ImportFile Name="32201">Type   Action  </ImportFile>

I am using xmlbeans to set the value in tags:-

ImportFile importFile = importOption.addNewImportFile();
importFile.setName("Id");
importFile.setStringValue(value);

But by using above all trailing whitespaces get deleted and result in following structure:-

<ImportFile Name="32201">Type   Action</ImportFile>

P.S. I am not using pretty print XMLoption

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
Yatin Garg
  • 141
  • 1
  • 8

2 Answers2

1

Try to put your text into a CDATA block:

<ImportFile Name="32201"><![CDATA[Type   Action  ]]></ImportFile>

As I know there isn't right way to preserve whitespaces in the XML
According to the Annotated XML Specification, whitespaces in attribute values are normalized by the XML processor.

This topic could also be useful for understanding parsing of whitespaces in the XML - What You Need to Know About Whitespace in XML

Denys Kurochkin
  • 1,360
  • 1
  • 18
  • 34
  • Thanks Denis. Yeah I too thought of using CDATA but I need to adhere to guidlines given by client. :( – Yatin Garg Aug 31 '16 at 06:48
  • You could parse that xml with regexp and wrap such blocks with CDATA. Or you could write your own xml parser like here - http://stackoverflow.com/a/869050/1916536. But if a client doesn't follow standards you'll probably be doomed to do such workarounds forever. So I wouldn't silently do dirty hacks and would open discussion with a client at least to let him know about inconsistencies in his guidelines. – Denys Kurochkin Aug 31 '16 at 10:49
0

Well it turned out that it was resulting in following structure only:- Type Action Actually to print the resulting element in docs i was using importFile.toString(). Instead of it printing as importFile.xmlText(), I found the actual result.

Yatin Garg
  • 141
  • 1
  • 8