Parse XML String

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Parse XML String

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.  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


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: Parse XML String

Daniel Dekany
Friday, July 20, 2012, 7:33:56 PM, Guntu, Ashok (IS) (Contr) wrote:

> 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.

> 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


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: EXT :Re: Parse XML String

Guntu, Ashok (IS) (Contr)
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:[hidden email]]
Sent: Friday, July 20, 2012 5:49 PM
To: Guntu, Ashok (IS) (Contr)
Subject: Re: EXT :Re: [FreeMarker-user] Parse XML String

Friday, July 20, 2012, 9:26:01 PM, Guntu, Ashok (IS) (Contr) wrote:

> 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-----
> From: Daniel Dekany [mailto:[hidden email]]
> Sent: Friday, July 20, 2012 2:32 PM
> To: Guntu, Ashok (IS) (Contr)
> Cc: [hidden email]
> Subject: EXT :Re: [FreeMarker-user] Parse XML String
>
> Friday, July 20, 2012, 7:33:56 PM, Guntu, Ashok (IS) (Contr) wrote:
>
>> 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.
>
>> 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


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user