Home > other >  Are multi-line strings allowed in XML using karate framework?
Are multi-line strings allowed in XML using karate framework?

Time:05-02

I have 2 files as:

  1. The first is text file contains multiple lines:

And def txtContent = read(path-to-text-file)

txtContent is multiple-line string, as example:

1a
2b
3c
  1. The second is xml file contain request template:
    <customizedTag>
       <time>#(send_time)<time>
       <bodyData>#(txtContent)</bodyData>
    </customizedTag>

When run the following steps to build body request then get wrong format, as below:

And def send_time = LocalDateTime.now()   ''
And def txtContent = read(path-to-text-file)
And def buildBodyRequest = read(path-to-xml-template-file)
* print buildBodyRequest

The output of buildBodyRequest same as:

<customizedTag>
<time>2022-05-01T12:56:45.324104300<time>
<bodyData>1a&#13;
2b&#13;
3c</bodyData>
</customizedTag>

The special string &#13; appeared in the body request and its make request is wrong format.

So please help me if you know that how to remove/replace the special string &#13; in the body request in karate? Thanks!

My expected for that:

<customizedTag>
<time>2022-05-01T12:56:45.324104300<time>
<bodyData>1a
2b
3c</bodyData>
</customizedTag>

CodePudding user response:

The special string appeared in the body request and its make request is wrong format.

In Windows text files, line breaks consist of two characters, carriage return (CR, character code 13) and line feed (LF, character code 10). This combination is often called CRLF.

Here the &#13; represents the CR, but the LF is also there - it's what causes the lines to break visually:

<customizedTag>
    <time>2022-05-01T12:56:45.324104300</time>
    <bodyData>1a&#13;
2b&#13;
3c</bodyData>
</customizedTag>

When this XML gets parsed, the &#13; will get translated back to the CR, and that means <bodyText> will contain exactly the same thing as your text file did.

So technically, there is nothing wrong. Everything works exactly as expected and you really should not have any issues. (This is important. You need to figure out why the &#13; is causing issues for you, and you will be better off fixing that, than "fixing" the XML, because the XML is not broken.)

But if for some reason you don't like the &#13; showing up in your XML, you must change your source text file. Open it in a proper text editor (i.e. not Windows Notepad), change the line ending type to Linux (which just LF, without CR), and save it.

In both cases, the contents of <bodyText> will be exactly the same as the text file it came from.

  • Related