Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
sushantkapoor19
Contributor II
Contributor II

Reading a data from xml/database and creating a xml file with data in body field/xml field.

Hello All,

I have a scenario in my project.

I have to read the data from a database table with rows/xml file. I have to load this data to a xml field as body in another xml so that i can eventually feed the tRest client with this to generate a response.

so the flow is as below:

xml file---> xml field in xml file( generated as body)--> tRest client as input ( xml body needs to be an input)

Can you tell me how can i do this?

Labels (2)
2 Replies
amanda2369weaver
Contributor
Contributor

To accomplish your task of reading data from a database or XML file, embedding it into another XML as a field, and then feeding it to a REST client, you can use various programming languages and tools. Below is a generic outline of how you can achieve this in Python using libraries like xml.etree.ElementTree for XML manipulation and requests for making REST API calls.

Steps to Implement the Solution:
Read Data from the Database or XML File:

For a database, use a library like sqlite3, psycopg2, or SQLAlchemy to fetch data.
For an XML file, use xml.etree.ElementTree to parse the XML.
Create the Body XML:

Embed the fetched data into an XML field.
Generate the Final XML with the Body Field:

Create the final XML structure and insert the body XML.
Send the Final XML to the REST Client:

Use requests or a similar library to send the XML as the body of a POST request.

Sample Implementation in Python:

Step 1: Read Data


From Database:

import sqlite3

def fetch_data_from_db():
    conn = sqlite3.connect('your_database.db')
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM your_table")
    rows = cursor.fetchall()
    conn.close()
    return rows

 

From XML File:

import xml.etree.ElementTree as ET

def fetch_data_from_xml(file_path):
    tree = ET.parse(file_path)
    root = tree.getroot()
    return root

 

Step 2: Create the Body XML

def create_body_xml(data):
    body = ET.Element('body')
    for item in data:
        element = ET.SubElement(body, 'item')
        element.text = str(item)  # Adjust based on your data structure
    return body

 

Step 3: Generate the Final XML

def create_final_xml(body_xml):
    root = ET.Element('root')
    body_element = ET.SubElement(root, 'xmlField')
    body_element.append(body_xml)
    return ET.tostring(root, encoding='utf-8').decode('utf-8')

 

Step 4: Send the Final XML to the REST Client

import requests

def send_to_rest_client(xml_body):
    url = 'http://your_rest_api_endpoint'
    headers = {'Content-Type': 'application/xml'}
    response = requests.post(url, data=xml_body, headers=headers)
    return response

 

Putting It All Together

def main():
    # Fetch data from the database or XML file
    # data = fetch_data_from_db()  # Uncomment this if fetching from a database
    data = fetch_data_from_xml('input.xml')  # Uncomment this if fetching from an XML file
    
    # Create the body XML
    body_xml = create_body_xml(data)
    
    # Generate the final XML
    final_xml = create_final_xml(body_xml)
    
    # Send the final XML to the REST client
    response = send_to_rest_client(final_xml)
    
    # Print the response from the REST client
    print(response.status_code, response.text)

if __name__ == "__main__":
    main()

Notes:

  • Adjust the Data Processing: The create_body_xml function assumes the data is simple and can be converted to string directly. You might need to adjust this based on the actual structure of your data. Sedgwick
  • Error Handling: Add appropriate error handling for database connections, XML parsing, and HTTP requests.
  • XML Namespaces: If your XML files use namespaces, make sure to handle them correctly when parsing and creating XML.
  • This code provides a basic framework that you can extend based on the specific requirements of your project.
sushantkapoor19
Contributor II
Contributor II
Author

Hi,

can you please tell me how we can do this using talend components. I dont know python programming.