XML Handling :
In webMethods, XML handling refers to the capabilities and features provided by the webMethods Integration Platform to process, transform, validate, and manipulate XML data. XML (eXtensible Markup Language) is a widely used standard for representing structured data, and webMethods offers various tools and services to work with XML effectively.
- XML Parsing: webMethods provides built-in XML parsers to read and parse XML documents, allowing you to extract data from XML and access individual elements, attributes, and values. reframe the sentence
- XML Validation: With webMethods, you can validate XML against XML Schema Definitions (XSD) or Document Type Definitions (DTD). This ensures that the XML data adheres to the defined structure and rules.
- XML Transformation: webMethods supports XML transformations using technologies like XSLT (eXtensible Stylesheet Language Transformations) and XQuery. You can transform XML data into different formats or structures based on your requirements.
- XML Generation: Using webMethods, you can create XML documents programmatically by assembling XML elements, attributes, and values. This is useful when constructing XML messages or responses for integration scenarios.
- XML Manipulation: webMethods offers various functions and services to manipulate XML data, such as adding, updating, or deleting XML elements, attributes, or values. This allows you to modify XML dynamically during runtime.
- XML Integration: webMethods Integration Platform provides adapters and connectors to integrate with external systems that communicate using XML formats. It supports XML-based messaging protocols like SOAP (Simple Object Access Protocol) and RESTful web services.
- Create a flow service within webMethods Integration Platform that accepts an input string in XML format.
- Check the Employee name, remove the last name, and map it back to the document.
- Verify the telephone number within the XML document and check if it already has the country code “+91” appended to it. If the country code is absent, we will utilize webMethods Integration Platform’s XML handling capabilities to add “+91” to the telephone number and update it accordingly in the document structure.
- Convert the modified document structure back into XML format using webMethods Integration Platform’s XML handling features. Subsequently, validate the generated XML against the specified schema to ensure compliance and adherence to the defined structure and rules.
- If a schema validation error occurs while processing the XML, log the specific error details into the server log. On the other hand, if the schema validation is successful, log a message indicating the successful validation into the server log.Please click the below link to download XML and XSD and use it for the exercise.Click the below button to Download XSD-
- Create a folder with the name docType and create the document using the xsd which you have downloaded with the name Employee. Click here to know how to create a document from XSD.
- Now create a flow service with the name xmlHandlingDemo.
- Now create a variable with the name inputXml in the input of the flow service.
- Add a try-catch block to handle the exception.
- In Catch block invoke getLastError and then invoke debugLog service to log the error into the server log.
- Now we need to convert the XML string to the document so for that follow the below steps:
- invoke pub.xml:xmlStringToXMLNode which will generate the node object and map inputXml to the xmlData variable.
- invoke pub.xml:xmlNodeToDocument which will convert the node object to document and perform the below mappings.
- map input node to the node(it will be auto mapped as the variable names are same , it is called implicit mapping)
- In the pipeline out right-click click insert and select document reference, it will ask you to select the document to which you want to refer, select the document which was created using xsd, and name it Employee then map the output document to Employee document.
- Copy the Employee document from the docType folder so that the full path will be copied to the clipboard and hard code the value into dcoumentTypeName field in the Service In.
Note : When explicitly specifying the documentTypeName, it instructs the compiler to create a document with a structure identical to the designated document type. To avoid any potential exceptions, it’s important to additionally set the makeArray parameter to false. This ensures that the document is created as a single instance rather than as an array, facilitating proper handling and manipulation of the XML data within the flow service.
- Now double-click on makeArrays and select false.
- Now we have created our document, which holds the structured data, we can utilize it for further mapping purposes. Since the “Employee” document represents a list of employees, we need to incorporate a loop step in the flow service. Set the inputArray property of the loop step to “Employee/EmployeeDirectory/Employee” to enable iteration through the employee list.
- To remove the last name of the employee, we will use the pub.string:tokenize service in webMethods. We will invoke this service with the separator parameter set as a space (” “), which will split the full name of the employee into a list. By extracting the value at the 0th index of the list, we will obtain the first name. This first name can then be mapped back to the Name field, effectively removing the last name.
- To update the phone number, we will first check if “+91” is present at the beginning of the phone number. To perform this check, we will use the pub.string:substring service in webMethods. We will invoke this service, mapping the Telephone to the inString parameter and setting the beginIndex to 0 and the endIndex to 2. This will extract the first three digits from the phone number, allowing us to determine if it starts with “+91”.
- To verify if the extracted value is ‘+91’, we will add a branch step to the flow service. In the branch step, we will set the switch property as the value variable so that at the run-time value of the variable will be checked.
- As we have discussed in the Branch tutorial, when using the branch step as a switch, we need to provide the exact expected value that will be evaluated at runtime in the child label. Therefore, in our case, we will configure the child label of the branch step with the variable name containing the extracted value, enabling us to evaluate it against the specific expected value of ‘+91’.
- Inside the branch step, we will include two map steps. In the first map step, set the label to ‘+91’ to handle the scenario when the extracted value matches ‘+91’. In the second map step, set the label to ‘$default’ to handle all other cases that do not match the expected value.
- To append ‘+91’ in case it is not present, we will add the pub.string:concat service as a transformer to the $default map step. In the pub.string:concat service, we will set inString1 to ‘+91’ (hardcoded value) and inString2 to be mapped from ‘Employee/EmployeeDirectory/Employee/Telephone’.
- Now we need to validate the updated document against the schema so after the loop step invoke pub.schema:validate step and perform the below mapping:
- Map Employee document to object in the validate step service input.
- Hard code conformsTo variable with the full path of the Emploee document, you can copy the path and paste it into the confirmsTo variable.
- To retrieve all errors in the requested document at once, set the maxErrors parameter to ‘-1’. This configuration ensures that all errors present in the document are returned in a single run, rather than encountering and fixing one error at a time.
- The pub.schema:validate service will provide two main outputs. Firstly, the isValid flag will indicate the validation status, with a value of true or false. Secondly, the errors document list will contain a collection of errors encountered during the validation process.
- To verify the success of the schema validation, we will incorporate a branch step in the flow service. This branch step will utilize the isValid flag as the switch property, allowing us to evaluate whether the validation process was successful or not.
- Within the branch step, we will include a Sequence step to accommodate multiple operations in case of validation failure and a DebugLog step to write a log entry to the server log when the validation is successful.
- To configure the DebugLog step, set the label to true within the branch step. Additionally, hard code the message to “Employee Data validated successfully”.
- Set SEQUENCE label to false and follow the below steps to extract error and log.
- Add one map step and in the pipeline out create a document structure like below mentioned in the screenshot.
Note : Since the errors variable is a list of documents, directly converting it to a string would result in considering only the first index. To ensure accurate extraction of error details, it is preferable to create a document structure and then map the error information accordingly. While it is possible to loop through the error list and extract details individually, for this scenario, our intention is to extract all error information at once and convert it into a single string. This string will then be utilized for logging purposes, consolidating all error details into a comprehensive log entry. - Add one map step and map errors document list to the Error/Errors.
- Invoke the pub.xml:documentToXMLString service. In the service input, we will map the Error document to the document parameter, allowing the entire error document to be converted into XML.
- To log the schema validation error, we will add a DebugLog step. In the DebugLog step, set the message as “Schema validation error occurred. Error: %xmldata%”. It is important to ensure that the variable substitution option is checked so that the %xmldata% placeholder will be replaced with the actual value of the xmldata variable at runtime.
- Add one map step and in the pipeline out create a document structure like below mentioned in the screenshot.
- Now, execute this service with the input XML that was downloaded at the beginning of this exercise. By running the service with the provided XML input, we will trigger the XML handling operations.
- To test the validation error scenario, you can click the download button below to obtain the XML file. Once downloaded, execute the service by providing this XML file as input.
- Now see the output it got failed with schema validation Error
To obtain the source code, simply click on the download button below to initiate the package download. This will allow you to access and save the code files for further reference😊.
- invoke pub.xml:xmlStringToXMLNode which will generate the node object and map inputXml to the xmlData variable.