cancel
Showing results for 
Search instead for 
Did you mean: 

XML parser templateObject.

rmorant
Champ in-the-making
Champ in-the-making
Hi,
Is there a templateObject method to parse an xml String into Freemarker NodeModel?
I not find it in the code.
Then i'm going to extend templateObjects, but i don't like override webscripts.abstractcontainer Spring bean only for add a new method.
By the way, can i extend TemplateObjects o scriptObjects without override webscripts.abstractcontainer?

Thank you.
4 REPLIES 4

openpj
Elite Collaborator
Elite Collaborator
Yes, there is an XML parser to read each XML elements and attributes values, you can see more details here:
http://wiki.alfresco.com/wiki/FreeMarker_Template_Cookbook#XML_Document_Example

Hope this helps.

rmorant
Champ in-the-making
Champ in-the-making
OK,
No more questions, a bit of code.
The implementation method:

public class FreeMarkerXmlParser implements TemplateMethodModel {
   public TemplateModel exec(List args) throws TemplateModelException {
            if (args.size() != 1) {
                throw new TemplateModelException("Wrong arguments");
            }
            return parseXml((String)args.get(0));
        }
   public NodeModel parseXml(String textData) {
      NodeModel docData = null;
      try {
         docData = freemarker.ext.dom.NodeModel.parse(new InputSource(new StringReader((String) textData)));
      } catch (SAXException e) {
      } catch (ParserConfigurationException e) {
      } catch (IOException e) {}
      return docData;
   }
}
The application context:


<bean abstract="true" id="webscripts.abstractcontainer">
  …
  <property name="templateObjects">
         <map>
           <!– method to parse an xml String into Freemarker NodeModel –>
           <entry key="parseXml">
              <bean class="com.albasoft.alfresco.utils.FreeMarkerXmlParser" />
           </entry>
           ..
Then the template example.


  <#assign doc = parseXml(response) />   
     <#list doc.items.item as item>  
     <tr>
       <#list dao.fieldNames as f>
        <td>${item[f]!""}</td>
       </#list>
     </tr>
      </#list>

where response is an xml String (like):

<items>
  <item>
    <id></id>
    <name></name>
    <phone></phone>
  </item>
  <item>…
</items>

Any way, i wonder why there is not a root template object like this.
or, is there?, because i prefer use the original and not override abstract container bean.
Best regards.

rmorant
Champ in-the-making
Champ in-the-making
Yes, there is an XML parser to read each XML elements and attributes values, you can see more details here:
http://wiki.alfresco.com/wiki/FreeMarker_Template_Cookbook#XML_Document_Example

Hi OpenPj,
Thank you, i know this but this parses the document root object (isn't it?), and i need parse any string , for example:

  <#assign doc = parseXml(args['myDoc']) />

Thank you again.

openpj
Elite Collaborator
Elite Collaborator
I think that all you can you do with this object as documented as well here:
http://freemarker.org/docs/xgui_imperative_learn.html

Hope this helps.