naresh chinna
naresh chinna

Reputation: 11

how do I read an xml data if the data contains ® in the payload, it doesn't accept if the payload contains with ® tried replacing all the way

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:Envelope xmlns="http://www.testesb.com/schemas/esb/common/Envelope" xmlns:ns2="http://www.test.com" xmlns:ns3="com.bbby.schemas.esb.common.envelope">
                        <ns2:MasterShortDescription>The Air Fryer lets you to enjoy your favorite fried snacks and food without the added calories. Cooks food by circulating hot air in all directions and includes 8 presets pre-heat, fries, pork, shrimp, cake chicken, steak and fish</ns2:MasterShortDescription>
                        <ns2:MasterProductDescription>3.7 qt. &reg; Digital Air Fryer with 8 Presets in Plum</ns2:MasterProductDescription>
                        <ns2:WebProductDescription>3.7 qt. Digital Air Fryer with 8 Presets in Plum</ns2:WebProductDescription>
</ns3:Envelope>

I want this to be formatted as json but doesn't even read it

Upvotes: 0

Views: 509

Answers (3)

Thinker-101
Thinker-101

Reputation: 651

The problem with any XML data in dataweave is that, if your input contains non-XML characters, let it be Unicode, or any symbols, then conversion of XML to JSON first and then applying operations like replacing the character or splitBy at the character or vice-versa won't work. Reason? The special character gets auto interpreted into unicode by dataweave.

For example, let's say your JSON is as below and you do a payload.value with output format as application/xml, this will fail

{ 
value: "Hello\u000bWorld"
}

Similar is the your case but its the quite opposite i.e XML to JSON

Try doing a regex replace but with unicode search and remove operator like payload.value replace(/\p{C/) with " " before the output conversion.

Upvotes: 0

aled
aled

Reputation: 25812

You are trying to use &reg; which is not really an XML entity but an HTML entity. Probably whatever editing software used to create the product description allowed HTML entities. However when Mule 4 DataWeave XML parser tries to read it will fail because it doesn't recognize it, and properly so. It is not a valid XML document.

In XML the right way to use that character is by using &#174; instead of $reg;. However if you can not replace it at the source, you might need to use a crude workaround, like replacing as a string one entity by the other. This is something I never advise to do.

Upvotes: 0

Salim Khan
Salim Khan

Reputation: 4303

I tried with the following as the input

<ns3:Envelope
    xmlns="http://www.testesb.com/schemas/esb/common/Envelope"
    xmlns:ns2="http://www.test.com"
    xmlns:ns3="com.bbby.schemas.esb.common.envelope">
    <ns2:MasterShortDescription>The Air Fryer lets you to enjoy your favorite fried snacks and food without the added calories. Cooks food by circulating hot air in all directions and includes 8 presets pre-heat, fries, pork, shrimp, cake chicken, steak and fish</ns2:MasterShortDescription>
    <ns2:MasterProductDescription>3.7 qt. ® Digital Air Fryer with 8 Presets in Plum</ns2:MasterProductDescription>
    <ns2:WebProductDescription>3.7 qt. Digital Air Fryer with 8 Presets in Plum</ns2:WebProductDescription>
</ns3:Envelope>

Script

%dw 2.0
output application/json
---
payload

Output

{
  "Envelope": {
    "MasterShortDescription": "The Air Fryer lets you to enjoy your favorite fried snacks and food without the added calories. Cooks food by circulating hot air in all directions and includes 8 presets pre-heat, fries, pork, shrimp, cake chicken, steak and fish",
    "MasterProductDescription": "3.7 qt. ® Digital Air Fryer with 8 Presets in Plum",
    "WebProductDescription": "3.7 qt. Digital Air Fryer with 8 Presets in Plum"
  }
}

Is that what you are looking for? I had to make a few corrections in your input xml , since i think you were missing the "<" and ">" around your starting tags, or atleast if i copy the text from your question then the angled brackets seem to be missing on some of the start tags.

Upvotes: 1

Related Questions