Display XML Data in HTML Page

XML stands as a self-descriptive language supported by W3C, purpose-built for efficient data storage and seamless data exchange. For Python application developers, the adaptability of XML format allows for easy transformation into user-friendly, human-readable formats like HTML. This article will guide you through the process of leveraging GroupDocs.Assembly Cloud SDK for Python and simple templates to translate XML data into comprehensive HTML reports.

Prerequisites:

Before getting started, make sure you have the following in place:

  • Python installed on your system.
  • GroupDocs.Assembly Cloud SDK for Python installed.
  • An active GroupDocs.Assembly Cloud account with valid API credentials.
  • Sample data and templates for testing (we will use the following sample XML data and report template).

Sample XML Data

Use the following XML data.

<Managers>
	<Manager>
		<Name>John Smith</Name>
		<Contract>
			<Client>
				<Name>A Company</Name>
			</Client>
			<Price>1200000</Price>
		</Contract>
		<Contract>
		...
		</Contract>
		...
	</Manager>
	<Manager>
		<Name>Tony Anderson</Name>
		...
	</Manager>
	...
</Managers>

Sample Template

Generate the specified template in TXT, DOCX, or the necessary format for iterating through Managers’ data alongside their corresponding Clients and related information. Subsequently, proceed with the implementation of the code to generate the report.

<<foreach \[in managers\]>>Manager: <<\[Name\]>>
Contracts:
<<foreach \[in Contract\]>>- <<\[Client.Name\]>> ($<<\[Price\]>>)
<</foreach>>
<</foreach>>

Points to be covered:

Configure the Python HTML Report Generator SDK

To initiate the process, incorporate the GroupDocs.Assembly Cloud SDK for Python into your Python project by utilizing pip (Python’s package installer). Execute the subsequent command in the console:

pip install groupdocs-assembly-cloud

Launch the API Client

Next, retrieve your Client ID and Client Secret from the dashboard, and incorporate the provided code as illustrated below:

import groupdocsassemblycloud as assembly
# Get app_sid & app_key from https://dashboard.groupdocs.cloud after free registration.
app_sid = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
app_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Get File API configurations.
configuration = assembly.Configuration(app_sid, app_key)
configuration.api_base_url = "https://api.groupdocs.cloud"
storage_name = "LocalStorage"

Upload the Data Source and Template Files

Firstly, upload the data source and template files to the cloud using the code example given below:

# Create an instance of the file API
file_api = groupdocsassemblycloud.FileApi.from_config(configuration)
# Call upload file request
request = groupdocsassemblycloud.UploadFileRequest("python-testing\input-sample-file.xml", "H:\\groupdocs-cloud-data\\input-sample-file.xml", "storage_name")
# Upload file to the cloud
response = file_api.upload_file(request)
print(response.uploaded)
view raw upload-file.py hosted with ❤ by GitHub

As a result, the uploaded files will be available in the [files section][https://dashboard.groupdocs.cloud/files] of your dashboard on the cloud.

Display XML Data on an HTML Page using Python

To present XML data within an HTML page, adhere to the subsequent steps:

  1. Create an instance of AssemblyApi with client_id and client_secret.
  2. Set up template information with a file path, storage name, version ID, and password.
  3. Prepare the assemble request using AssembleOptions with report data, template info, output path, and save format.
  4. Generate the HTML report using AssemblyApi.assemble_document(AssembleOptions) method.

The provided code sample exemplifies the implementation of Python SDK for HTML report generator REST API, enabling the presentation of XML data on an HTML page.

import groupdocsassemblycloud as assembly
client_id = "XXXX-XXXX-XXXX-XXXX"
client_secret = "XXXXXXXXXXXXXXXX"
assemblyAPI = assembly.AssemblyApi(client_id,client_secret )
# Set up template info
templateInfo = assembly.TemplateFileInfo()
templateInfo.file_path= 'Python_Testing/template.txt'
templateInfo.storage_name= 'LocalStorage'
templateInfo.version_id= ''
templateInfo.password='abc123'
# Prepare assemble request
options=assembly.AssembleOptions()
options.report_data = 'Python_Testing/report-data.xml';
options.template_file_info = templateInfo
options.output_path = 'Python_Testing/report.html';
options.save_format = 'html'
# Generate HTML report
assemblyAPI.assemble_document(options)

Download HTML File

The code given in the previous step saves the converted HTML file on the cloud. To download it, you can use the following code snippet.

# Create instance of the API
file_api = groupdocsassemblycloud.FileApi.from_config(configuration)
request = groupdocs_conversion_cloud.DownloadFileRequest('Python_Testing/report.html', my_storage)
response = file_api.download_file(request)
# Move downloaded file to your working directory
shutil.move(response, "C:\\Files\\")

Conclusion

Within this article, we have encompassed the steps involved in presenting XML data on an HTML page via the utilization of GroupDocs.Assembly Cloud REST API in conjunction with its Python SDK. This robust API empowers developers to seamlessly craft dynamic reports across diverse formats, harnessing information from XML or XML sources, and melding templates spanning Word documents, spreadsheets, and text files.

Additionally, for a more comprehensive grasp of the GroupDocs.Assembly Cloud API, delve into the detailed documentation. Also, take advantage of our API reference area, which empowers you to directly engage with and observe our APIs in action right from your browser. You can also freely access the complete source code of the Python SDK on Github.

In closing, we consistently produce fresh blog articles exploring a range of file formats and their parsing utilizing the REST API. Reach out to us for the most up-to-date information.

Ask a question

In case you would have any queries or confusion about the HTML report generator, please feel free to contact us via our forum.

See Also

Here are some related articles that you may find helpful: