1

I have a xml with format say

<x>    
    <y>
        <z Name="z"> File Explorer </z>
    </y>
</x>

I want to write a freemarker template to retrieve the content "File Explorer". I have written java code to transform xml to html. input xml is given xml.

Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
 configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        configuration.setDefaultEncoding("UTF-8");
InputSource inputSource = new InputSource( new StringReader(xml.asXML()));
        root.put("doc", freemarker.ext.dom.NodeModel.parse(inputSource));
        result = new StringWriter();
        template.process(root, result);

I tried freemarker template with xpath

<span>${doc["x/y/z[@Name='z']"/]}</span>

But this on transforming displays ${doc["x/y/z[@Name='z']"/]} instead of
File Explorer

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Priyanka Deo
  • 11
  • 1
  • 2
  • Why do you have to use freemarker for this task? Why can't you use standard xml parser like in http://stackoverflow.com/questions/773012/getting-xml-node-text-value-with-java-dom (see the answer)? – rsutormin Sep 17 '15 at 04:44
  • If it indeed shows the `${...}` thing as is, then that file doesn't go through FreeMarker processing. (When it does, you will have to remove the last `/`, otherwise you get a FreeMarker syntax error.) – ddekany Sep 17 '15 at 06:22
  • @ddekany Solved the issue. The problem was with java code to compile freemarker template. Should create template with method Template template = new Template("name", new StringReader(), Configuration) instead. Now I am stuck with another issue. Does anyone have a clue on how to display content using xpath? In this example I have display "File Explorer" using Name attribute. – Priyanka Deo Sep 21 '15 at 22:40
  • Isn't that what you are doing inside that `${...}`? Does it give an error, or what doesn't work? – ddekany Sep 23 '15 at 05:57

1 Answers1

0

Use the Freemarker Ant task freemarker.ext.ant.FreemarkerXmlTask:

  • Go to the lib folder which includes the freemarker JAR file
  • Copy the path
  • Paste it into the location attribute of the pathElement element of the Ant task
  • Copy the path of the XML file
  • Paste it into the attribute basedir in the freemarker element of the Ant task
  • Copy the path of the FTL file
  • Paste it into the template attribute of the freemarker element of the Ant task

For example:

  • Ant task

build.xml:

<?xml version="1.0"?>

<project basedir="." default="generate" name="FreeMarker-FAQ">

  <taskdef name="freemarker" classname="freemarker.ext.ant.FreemarkerXmlTask">
    <classpath> 
      <fileset dir="lib">
        <include name="freemarker.jar"/>
      </fileset>
    </classpath>
  </taskdef>

  <target name="generate">
    <mkdir dir="html"/>
    <freemarker basedir="." template="xml2html.ftl" includes="xml/data.xml" destdir="html"/>
  </target>

</project>
  • XML file

data.xml:

<?xml version="1.0"?>
<root name="test" value="123">
  <child name="data" value="456"/>
</root>
  • Freemarker Template

xml2html.ftl

<!DOCTYPE html>
<html lang="en">
 <head>
  <title>${.node.root.@name}</title>
  <meta name="keywords" content="FreeMarker, Java, servlet, HTML, template, free software, open source, XML" />
 </head>
 <body bgcolor="#ffffff">
  <h2><a name="top">${.node.root.child.@name}.</a></h2>
 </body>
</html>
  • Directory structure

  • build.xml

    • /lib
      • freemarker.jar
    • /html
      • data.html
    • /xml
      • data.xml

Then run ant from the directory which contains the build.xml file.

References

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265