1

The aim is to have a xml(string) and a json(string) as input and build a template with thymeleaf from it. Configuration:

@Bean
StringTemplateResolver xmlTemplateResolver(ApplicationContext appCtx) {
        StringTemplateResolver templateResolver = new StringTemplateResolver();
        templateResolver.setTemplateMode(TemplateMode.XML);
         return templateResolver;
}

@Bean(name="springTemplateEngine")
  SpringTemplateEngine templateEngine(ApplicationContext appCtx) {
     SpringTemplateEngine templateEngine = new SpringTemplateEngine();
     templateEngine.setTemplateResolver(xmlTemplateResolver(appCtx));

     return templateEngine;
 }

and this is the usage: "schema" is the xml string and "json" is a json string with the key:value pairs

@Autowired
SpringTemplateEngine springTemplateEngine;

...

Context context = new Context();
ObjectMapper mapper = new ObjectMapper(new JsonFactory());
Map<String, Object> map  = mapper.readValue(json, new TypeReference<Map<String,Object>>(){});

for (Map.Entry<String, Object> entry : map.entrySet()) {
    context.setVariable(entry.getKey(),entry.getValue());
}

String content = springTemplateEngine.process(schema, context);

The replacement is working fine but the Encoding of words like ö,ü,ä isnt working. My guess it is not UTF-8 encoded and I have no idea how to do that with StringTemplateResolver. I looked for other resolvers but all of them seem to want a file as an input. I just have a sting as an Input and i dont want to build a file from that as a workaround. Im looking for a solution to encode my xml-string with UTF-8 without building a file.

Do you have any ideas?

EDIT Here is a example Json

  {
    "vorgang": {
        "vorgangsid":"12345678",
        "datum":"2020-06-10"
    },
     "val":"toller Wert üöä",
   "personen": [
        {
            "name":"Müller",
            "vorname":"Klaus"
        },{
             "name":"Mälzer",
             "vorname":"Mürte"
        }
    ]
}

Here is a example Schema

<?xml version="1.0" encoding="UTF-8"?>
<Schema>
     <was th:text="${val}"></was>
      <vorgang>
        <VorgangsID th:text="${vorgang['vorgangsid']}"></VorgangsID>
        <Anlagedatum th:text="${vorgang['datum']}"></Anlagedatum>
        <wert> ü ö ä</wert>
    </vorgang>
     <Personen th:each="person : ${personen}" >
        <Person>
        <name th:text="${person.name}" ></name>
        <vorname th:text="${person.vorname}" ></vorname>
        </Person>
    </Personen>
</Schema>

Here is the the expected result

<?xml version="1.0" encoding="UTF-8"?>
<Schema>
     <was>toller Wert üöä</was>
      <vorgang>
        <VorgangsID>12345678</VorgangsID>
        <Anlagedatum>2020-06-10</Anlagedatum>
        <wert> ü ö ä</wert>
    </vorgang>
     <Personen >
        <Person>
        <name >Müller</name>
        <vorname >Klaus</vorname>
        </Person>
    </Personen>
     <Personen >
        <Person>
        <name >Mälzer</name>
        <vorname >Mürte</vorname>
        </Person>
    </Personen>
</Schema>

Here is the actual result

   <?xml version="1.0" encoding="UTF-8"?>
<Schema>
     <was>toller Wert &#xfc;&#xf6;&#xe4;</was>
      <vorgang>
        <VorgangsID>12345678</VorgangsID>
        <Anlagedatum>2020-06-10</Anlagedatum>
        <wert> ü ö ä</wert>
    </vorgang>
     <Personen >
        <Person>
        <name >M&#xfc;ller</name>
        <vorname >Klaus</vorname>
        </Person>
    </Personen>
     <Personen >
        <Person>
        <name >M&#xe4;lzer</name>
        <vorname >M&#xfc;rte</vorname>
        </Person>
    </Personen>
</Schema>

all üöä are in a wrong format.

dino
  • 183
  • 2
  • 8
  • Welcome to Stack Overflow. Please [edit] your question to provide a [mcve]. This should contain (sanitized) example of both xml and json strings as well as failing and expected output. Where come from those xml and json strings? – JosefZ Jun 10 '20 at 09:28
  • I added an example and thanks for Welcome :) – dino Jun 10 '20 at 11:46
  • Sorry, I can't know how to [unescape a XML numeric character entity reference](https://stackoverflow.com/q/17152590/3439404) in _thymeleaf_… Maybe something with `th:text=` assignment., maybe something with getting/reading the Json string? – JosefZ Jun 10 '20 at 12:11
  • 1
    I could solve my problem with th:utext= thanks – dino Jun 10 '20 at 12:30

1 Answers1

1

Instead of using

<was th:text="${val}"></was>

I used

<was th:utext="${val}"></was>

with the utext I tell my Code to unescape text.

thanks JosefZ

dino
  • 183
  • 2
  • 8