Below is a sample of XML i receive, i need to replace few special characters in the attributes and send it over ( xmlString.replaceAll("\[^A-Za-z0-9#&',-.\]", "")
), please refer to the last attribute for example
Is there a way to iterate over each node (XML attribute/node names are not fixed), then apply the regex only to the value part of attribute and rebuild the xml?
converting to string and applying the regex doesn't work always
open to any approaches in Java.
<AccountNumberId>JY00000830</AccountNumberId>
<XYZ:CompanyCd>DOC</XYZ:CompanyCd>
<XYZ:MultiPolicyDiscountCd>0</XYZ:MultiPolicyDiscountCd>
<QuestionAnswer>
<QuestionCd>XYZ:1</QuestionCd>
<YesNoCd>No</YesNoCd>
</QuestionAnswer>
<TransactionSeqNumber/>
<PersApplicationInfo>
<ApplicationWrittenDt>2023-02-26</ApplicationWrittenDt>
<KnownSinceDt>2007-02-05</KnownSinceDt>
</PersApplicationInfo>
<XYZ:TaxExemptionInd>0</XYZ:TaxExemptionInd>
</PersPolicy>
<Location id="LOC-1">
<ItemIdInfo>
<XYZ:FixedId>8001</XYZ:FixedId>
</ItemIdInfo>
<Addr>
<Addr1>`**`1234 $$$RIVERWOOD !!<GATE SUITE> 136`**`</Addr1>
...
var escapedXml = StringEscapeUtils.escapeXml(xmlString);
var replaceSplChars = escapedXml
.replaceAll("[^A-Za-z0-9#&',-.\n</>]", "")
.replace("\t", "");
var toXML = StringEscapeUtils.unescapeXml(replaceSplChars);
above approach doesn't help, since xml structure has attributes like "<XYZ:", and i end up removing ":"