Вопрос

I performed several tests and noticed that if the xml file is not idented correctly, the ouput of PrintWriter (with indent option set to false) contains newlines

Indented correctly:

<node bla="ste"> 
   <mp inf="blabl">text</mp>
   <mp inf="blabl">text</mp>
   <mp inf="blabl">text</mp>
   <mp inf="blabl">text</mp>
   ...  
</node>

not indented correctly:

<node bla="ste"> <mp inf="blabl">text</mp><mp inf="blabl">
text</mp><mp inf="blabl">text</mp>
   <mp inf="blabl">text</mp>
  ...
</node>


def xml= new XmlParser().parse(xmlFile)
StringWriter writer = new StringWriter()
new XmlNodePrinter(new IndentPrinter(new PrintWriter(writer), '', false)).print(xml)

println  writer.toString()

I don't have the posibility to change the xml file, so how can I do to have the ouput of the PrintWriter without new lines in this case?

Это было полезно?

Решение

You don't display the output of using the two blocks shown as input, but I'm guessing the reason there is still a newline in the XML output when using the 2nd block as an input, is that there is a newline in the data. The 2nd <mp> tag contains the text "\ntext".

IndentPrinter changes formatting, but not the data payload of individual tags, i.e. it can change the parts between two tag declarations or two close tags, but not the parts between a tag declaration and it's close tag.

If you want to get rid of ALL newlines, you'll need to parse the XML, then process it to remove all newlines from the .text() portions of each tag, THEN output it using the IndexPrinter as you do.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top