StringTemplate invalid character '<' when reading XML Template
Asked Answered
N

3

9

I am trying to create a simple XML-Template which so far only consists of:

<?xml version="1.0"?>

I read the file like this:

    STGroup group = new STGroupDir("templates");
    ST st = group.getInstanceOf("report");
    st.add("analysis", ana);
    String result = st.render();
    System.out.println(result);

And the result is several error messages:

report.st 1:1: invalid character '<'
report.st 1:1: invalid character '?'
report.st 1:19: invalid character '?'
report.st 1:20: invalid character '>'
report.st 1:2: no viable alternative at input 'xml'

I have seen other people reading HTML tempaltes that also use tags. So what am I doing wrong?

Naara answered 2/5, 2012 at 13:20 Comment(4)
It's empty : you just had declared document type.Hermann
Try to use inspector http://www.antlr.org/wiki/display/ST4/StringTemplate+Inspector+GUIHermann
It's not expecting xml file as input. See syntax at http://www.antlr.org/wiki/display/ST4/StringTemplate+cheat+sheet.Hermann
Sorry, I still don't get it. I tried to escape the < and > but it still would not help.Naara
N
6

Okay it seems I overlooked that you need to specify templates in a different snytax. Allthough this was not obvious from the examples I used:

My working template looks different now:

report (analysis) ::= <<
<?xml version="1.0"?>
>>

In addition I also changed the delimeters:

STGroup group = new STGroupDir("templates",'$','$');
Naara answered 2/5, 2012 at 16:8 Comment(5)
That's what I told you when said it wasn't expecting xml at all.Hermann
If you want something with a template which is actually a template, look at freemarker.Hermann
Yeah. Allthough Terrence Parr keeps saying that StringTemplate is good model/view separation, I cannot imagine a designer that can be bohtered to write templates for this.Naara
If you are writing Java code to process the template, use this to load a raw template file by itself: ST st = new ST("your xml here", '$', '$'); then your template will not need to have the template declaration like report(analysis) ::= << ... >>Cisterna
ST by default uses <> as delimiters. You don't have to change anything with your template as long as you pass "custom" delimiters. Note that I am using the latest (4.3) version as we speak.Littlefield
P
3

I've found that you can escape the angle bracket as well:

report(analysis) ::= <<
\<?xml version="1.0"?>
>>

Note the \ right before the <?xml -- additionally it's smart enough not to require another escape at the ?> closing bracket.

And I believe what Terrence Parr is suggesting about model/view separation is that the view never really has an opportunity to manipulate the underlying data structure (or model) passed to the template. This is accomplished by "applying" a template to the data or collection, rather than looping in the template over the data. Perhaps that is subtle, and perhaps practically (in the case of designers) a bit too pure.

Playlet answered 21/2, 2013 at 5:16 Comment(1)
This approached worked like a charm for me (I'm using StringTemplate 4.0.8). I'm voting up since sometimes it can be easier to just add the backslash to the template than replace for than find an unused character.Teratism
O
2

Alternatively, you can use STRawGroupDir if you don't want to use group file syntax. This class is similar to STGroupDir, but specifically for loading files like XML and HTML.

http://www.stringtemplate.org/api/org/stringtemplate/v4/STRawGroupDir.html

Overhead answered 9/7, 2013 at 2:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.