Find values from XML

0
Hello Everyone, I have a requirement in my project where I need to import a xml file and need to gather some data from xml and store it in a table, the catch is I cannot declare a xsd file or convert the xml into json and declare import mapping because the xml input is not going to be same. What string operation do I need to use to get the data in between 2 tags and put it in a object.I.e. The data is going to be in between <ESADout_CUCustomsPaymentCalculation> and </ESADout_CUCustomsPaymentCalculation> tag and those tags can be single or multiple.I need to collect all the data from this and then generate a table. How can I achieve this.
asked
3 answers
0

Hi Ishwari,

 

For Taking exact XML portion you can use the below Xpath function,

substring( $XMLEntityName ,find($XMLEntityName,'<ESADout_CUCustomsPaymentCalculation>')

findLast($XMLEntityName,'</ESADout_CUCustomsPaymentCalculation>'))

After finding exact list value string adjust the string as per XML format then you can simply do an export with mapping (store as string). The mapping can be based on a Message Definition (Go with Exact format what you needed in XML Schema) from your entity.

 

Hope it helps!

Thanks & Regards,

Manikandan K

answered
2

Hi,

we had a similar requirement to extract data between two html tags, I created Js action for same it gives answer in numbering if multiple , pass your data and tag name 

i checked with your data it is working

 

image.png

 

// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
import "mx-global";
import { Big } from "big.js";

// BEGIN EXTRA CODE
// END EXTRA CODE

/**
 * @param {string} data
 * @param {string} tag
 * @returns {Promise.<string>}
 */
export async function HTML_Extractor(data, tag) {
	// BEGIN USER CODE
	   const startTagPattern = `<${tag}`;
    const endTagPattern = `</${tag.split(" ")[0]}`;
    let resultString = "";
    let startIndex = 0;
    let count = 1; // Initialize a counter for numbering

    while (true) {
        startIndex = data.indexOf(startTagPattern, startIndex);
        if (startIndex === -1) break;

        startIndex = data.indexOf('>', startIndex);
        if (startIndex === -1) break;

        startIndex += 1;
        const endIndex = data.indexOf(endTagPattern, startIndex);
        if (endIndex === -1) break;

        const content = data.substring(startIndex, endIndex).trim();
        resultString += `${count}) ${content} \n`; // Concatenate content with numbering
        count++; // Increment the counter
        startIndex = endIndex + endTagPattern.length;
    }

    if (resultString === "") {
        console.error(`No content found between <${tag}> and </${tag.split(" ")[0]}> tags.`);
        return ""; // Return an empty string if no content is found
    }

    return resultString.trim();

	// END USER CODE
}

image.png

 

image.png

answered
2

Ishwari,

Another way to do this is with Mendix string functions.  If the incoming XML will potentially have a different structure each time, this is how I would do this.  I created a sample microflow, shown below:

image.pngNote that I have logged the results to the console.  You could also put these results into an entity.

 

When I called this microflow with the following sample data:

image.png

These results displayed in the console

image.png

 

Hope that helps,

Mike

answered