Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
This article describes how to use Aspose.HTML for Java to create an HTML document based on a template and populate it with data from various sources, such as XML or JSON. Using inline expressions syntax, you can efficiently match data-sources against your HTML template and dynamically generate content.
Aspose.HTML for Java offers a set of convertTemplate() methods that are used to convert an HTML template into an HTML document. The methods take several parameters (for example, paths to the template and data source, an object of TemplateLoadOptions, and a path to the result HTML) and return the populated HTML document.
An HTML template is a standard HTML file that contains some special inline expressions. These expressions use double curly braces {{ }} to map data from the input data source to specific parts of the HTML document. When processed, these template markers are replaced with corresponding data values following the below-mentioned rules.
Following is the list of supported inline expressions syntax.
The data-binding expression is used to set values of control element based on the information that is contained in the data-source.
The following is the basic syntax of the data-binding expression:
{{ data-binding expression }}
The following demo shows how to use the data-binding expression to obtain information from the XML data-source.
XML Data Source
1<Data>
2<FirstName>John</FirstName>
3<LastName>Doe</LastName>
4<Address>
5 <City>Chicago</City>
6 <Street>Oakmound Drive</Street>
7 <Number>100</Number>
8</Address>
9</Data>The data-binding expression in the HTML page template:
HTML Template
This HTML template demonstrates the use of inline expressions for data binding. It features a table with two columns: “Person” and “Address.” The “Person” column dynamically displays data for {{FirstName}} and {{LastName}} from the data source, while the “Address” column shows {{Address.Street}}, {{Address.Number}}, and {{Address.City}}. Inline expressions are defined using curly-brace syntax.
1<table border=1>
2 <tr>
3 <th>Person</th>
4 <th>Address</th>
5 </tr>
6 <tr>
7 <td>{{FirstName}} {{LastName}}</td>
8 <td>{{Address.Street}} {{Address.Number}}, {{Address.City}}</td>
9 </tr>
10</table>The foreach directive expression is used to iterate through the list of elements in combination with data-binding expression.
The following demo shows how to get all persons from the data-source and populate a template:
XML Data Source
1<Data>
2 <Persons>
3 <Person>
4 <FirstName>John</FirstName>
5 <LastName>Doe</LastName>
6 <Address>
7 <Number>100</Number>
8 <Street>Oakmound Drive</Street>
9 <City>Chicago</City>
10 </Address>
11 <Phone1>773-349-7747</Phone1>
12 <Phone2>708-252-9790</Phone2>
13 </Person>
14 <Person>
15 <FirstName>Jack</FirstName>
16 <LastName>Fox</LastName>
17 <Address>
18 <Number>355</Number>
19 <Street>Haul Road</Street>
20 <City>San Francisco</City>
21 </Address>
22 <Phone1>650-942-7641</Phone1>
23 </Person>
24 <Person>
25 <FirstName>Sherlock</FirstName>
26 <LastName>Holmes</LastName>
27 <Address>
28 <Number>45</Number>
29 <Street>Baker str.</Street>
30 <City>London</City>
31 </Address>
32 <Phone1>012-5146-564</Phone1>
33 </Person>
34 </Persons>
35</Data>The foreach directive expression in the HTML page template:
HTML Template
The data_merge attribute specifies the data source is a list of objects and the table should be repeated for each object in the list. The expressions inside the template, such as {{FirstName}}, {{LastName}}, {{Address.Street}}, and {{Address.Number}}, indicate the fields from the data source that should be inserted in the corresponding cells of the table.
1<table border=1 data_merge='{{#foreach Persons.Person}}'>
2 <tr>
3 <th>Person</th>
4 <th>Address</th>
5 </tr>
6 <tr>
7 <td>{{FirstName}} {{LastName}}</td>
8 <td>{{Address.Street}} {{Address.Number}}, {{Address.City}}</td>
9 </tr>
10</table>Please note that the current implementation supports foreach directive expression only for the following list of HTML elements: DIV, OL, UL and TABLE.
As it was mentioned earlier, the data-source could be represented in XML and JSON formats. The following are the examples of both data-source:
XML Data Source
1 <Data>
2 <FirstName>John</FirstName>
3 <LastName>Doe</LastName>
4 <Address>
5 <City>Chicago</City>
6 <Street>Oakmound Drive</Street>
7 <Number>100</Number>
8 </Address>
9</Data>JSON Data Source
1{
2 "FirstName": "John",
3 "LastName": "Doe",
4 "Address": {
5 "City": "Chicago",
6 "Street": "Oakmound Drive",
7 "Number": "100"
8 }
9 }If your case involves creating a template and specifying data on-the-fly, you have to follow a few steps:
data) for the convertTemplate(sourcePath, data, options, outputPath) method.sourcePath).options).sourcePath, data, options, outputPath) method and pass sourcePath, data, options, and outputPath for it. outputPath is a path for data-filled template file saving. The convertTemplate() method substitutes the expressions in the template with values from the data source, generating a populated HTML document. 1// Populate an HTML template with structured JSON data using Java
2
3// Prepare a JSON data-source and save it to a file
4String data =
5 "{\n" +
6 " 'FirstName': 'John',\n" +
7 " 'LastName': 'Smith',\n" +
8 " 'Address': {\n" +
9 " 'City': 'Dallas',\n" +
10 " 'Street': 'Austin rd.',\n" +
11 " 'Number': '200'\n" +
12 " }\n" +
13 "}";
14try (java.io.FileWriter fileWriter = new java.io.FileWriter("data-source.json")) {
15 fileWriter.write(data);
16}
17
18// Prepare an HTML Template and save it to a file
19String template =
20 "<table border=1>\n" +
21 " <tr>\n" +
22 " <th>Person</th>\n" +
23 " <th>Address</th>\n" +
24 " </tr>\n" +
25 " <tr>\n" +
26 " <td>{{FirstName}} {{LastName}}</td>\n" +
27 " <td>{{Address.Street}} {{Address.Number}}, {{Address.City}}</td>\n" +
28 " </tr>\n" +
29 "</table>\n";
30try (java.io.FileWriter fileWriter = new java.io.FileWriter("template.html")) {
31 fileWriter.write(template);
32}
33
34// Convert Template to HTML
35com.aspose.html.converters.Converter.convertTemplate(
36 "template.html",
37 new com.aspose.html.converters.TemplateData("data-source.json"),
38 new com.aspose.html.loading.TemplateLoadOptions(), "document.html"
39);Always validate the data source format (XML/JSON) and ensure consistency with template markers to avoid mismatches.
Use
TemplateLoadOptions.NamesAreCaseSensitive to control case sensitivity during data binding. If the value is true, matching is case sensitive; if it is false, the character case will be ignored. The default value is true. Setting it to false simplifies handling inconsistent naming.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.