Discussion:
[FreeMarker-user] Parse XML String
Guntu, Ashok (IS) (Contr)
2012-07-20 17:33:56 UTC
Permalink
Hi,
I have access to a variable in my template. The variable is of type String and contains xml in it. How am I supposed to parse it. If anybody could point me in the right direction that would be great.

All the examples I am looking at point to reading an xml document and parsing it.

Thanks
Ashok
Daniel Dekany
2012-07-20 18:32:26 UTC
Permalink
Post by Guntu, Ashok (IS) (Contr)
Hi,
I have access to a variable in my template. The variable is of
type String and contains xml in it. How am I supposed to parse it.
You are supposed to parse it before you put it into the data-model. If
that's not feasible, you can still write a TemplateMethodModelEx
implementation that parses a string as XML and then "?new" it into
some app-specific #import-ed/#included library.
Post by Guntu, Ashok (IS) (Contr)
If anybody could point me in the right direction that would be great.
All the examples I am looking at point to reading an xml document and parsing it.
Thanks
Ashok
--
Best regards,
Daniel Dekany
Guntu, Ashok (IS) (Contr)
2012-07-24 20:13:59 UTC
Permalink
Daniel
I implemented the way you suggested and it works fine.

I have pasted some sample code below for beginners like me. Create a jar file of the code and drop the jar along with the other jars where the template can read it. The code below is just for reference and might not work as it is.

Thanks
Ashok


*************************Template**********************
<!DOCTYPE html>
<html>
<body>
<#assign parseXML = 'com.example.ParseXml'?new()>
<table>
<tr><td>${parseXML("<book><title>Treasure Island</title></book>")}</td></tr>
</table>
</body>
</html>
*********************************************************************



***********************************Java Code*************************
import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModelException;

public class ParseXml implements TemplateMethodModel
{
public String exec(List args) throws TemplateModelException
{
String returnValue = parseXml((String) args.get(0));
return returnValue;
}

protected String parseXml (String xmlString)
{
//Your code goes in here
}

}



********************************************************************

-----Original Message-----
From: Daniel Dekany [mailto:***@freemail.hu]
Sent: Friday, July 20, 2012 5:49 PM
To: Guntu, Ashok (IS) (Contr)
Subject: Re: EXT :Re: [FreeMarker-user] Parse XML String
Thanks Daniel,
Yes, I cant parse it before it gets into the datamodel. The data
model is populated by a software called geoserver and I can write a
template as to how my data is going to be displayed.
But looks like even the TemplateMethodModelEx needs to be written
on the application (server side) somewhere. Can I write code within the template and to parse it?
You can't realistically do it in a template. The point of implementing
a TemplateMethodModelEx instead of a #function is exactly that for the
earlier you use Java, where you can call the standard XML API-s, and
all the related FreeMarker API-s (freemarker.ext.dom). When you have
implemented this TemplateMethodModelEx, drop its jar into WEB-INF/lib
or its class into WEB-INF/classes (or wherever you want as far as the
class loader finds it), then in FreeMarker you can create a callable
variable from it like <#assign parseXML =
'com.example.ParseXMLMethod'?new()>. It's pretty much like
<#function parseXML s>...</#function>, only the implementation is in
Java.
Thanks
Ashok
-----Original Message-----
Sent: Friday, July 20, 2012 2:32 PM
To: Guntu, Ashok (IS) (Contr)
Subject: EXT :Re: [FreeMarker-user] Parse XML String
Post by Guntu, Ashok (IS) (Contr)
Hi,
I have access to a variable in my template. The variable is of
type String and contains xml in it. How am I supposed to parse it.
You are supposed to parse it before you put it into the data-model. If
that's not feasible, you can still write a TemplateMethodModelEx
implementation that parses a string as XML and then "?new" it into
some app-specific #import-ed/#included library.
Post by Guntu, Ashok (IS) (Contr)
If anybody could point me in the right direction that would be great.
All the examples I am looking at point to reading an xml document and parsing it.
Thanks
Ashok
--
Best regards,
Daniel Dekany
Loading...