0

i have a xml string that i will replace all values of specific tags using javascript, and this is the code :

function replaceDomainName (xmlPayload,domainId)
{
var oldDomain = '<DOMAIN_NAME>OOO';
var oldDomain2 = '<DomainName>OOO';
var newDomain  = '<DOMAIN_NAME>'+domainId ;
var newDomain2  = '<DomainName>'+domainId ;
var xmlString = xmlPayload.toString();
var x  = xmlString.replace(/oldDomain/g,newDomain)
 x = x.replace(/oldDomain2/g,newDomain2)
console.log(x);
return x ; 
}

when I try to invoke the function with the following XML it throws error

<TransmissionHeader xmlns:tran="http://xmlns.oracle.com/apps/otm/TransmissionService" xmlns="">
            <Version>20b</Version>
            <TransmissionCreateDt>
                <GLogDate>20200819124057</GLogDate>
                <TZId>UTC</TZId>
                <TZOffset>+00:00</TZOffset>
            </TransmissionCreateDt>
            <TransactionCount>1</TransactionCount>
            <SenderHostName>https://xxx</SenderHostName>
            <SenderSystemID>https:xxx</SenderSystemID>
            <UserName>OOO</UserName>
            <SenderTransmissionNo>404836</SenderTransmissionNo>
            <ReferenceTransmissionNo>0</ReferenceTransmissionNo>
            <GLogXMLElementName>PlannedShipment</GLogXMLElementName>
            <NotifyInfo>
                <ContactGid>
                    <Gid>
                        <DomainName>OOO</DomainName>
                        <Xid>SYS</Xid>
                    </Gid>
                </ContactGid>
                <ExternalSystemGid>
                    <Gid>
                        <DOMAIN_NAME>OOO</DOMAIN_NAME>
                        <Xid>IOT_SYSTEM</Xid>
                    </Gid>
                </ExternalSystemGid>
            </NotifyInfo>
        </TransmissionHeader>

error: unknown: Unexpected token (14:23)
osfar
  • 401
  • 1
  • 4
  • 23
  • If you do want to solve this with Regex, then you might want to look at this https://stackoverflow.com/questions/43390873/template-literal-inside-of-the-regex; answer below also works: https://stackoverflow.com/a/63789604/1178142 – Pavel Sep 08 '20 at 12:45
  • P.S. `/oldDomain/g` means "the exact word 'oldDomain'" and not the value of the variable oldDomain. – Pavel Sep 10 '20 at 08:33

2 Answers2

0
x.replace(/<DOMAIN_NAME>OOO/g,'<DomainName>'+domainId)

use this

Corleone
  • 19
  • 5
0

While you can get a lot done with Regex, it can get really complicated when parsing XML.

See this example of using DOMParser and XMLSerializer: https://jsfiddle.net/o1cenvs3/

const XML = `<TransmissionHeader xmlns:tran="http://xmlns.oracle.com/apps/otm/TransmissionService" xmlns="">
            <Version>20b</Version>
            <TransmissionCreateDt>
                <GLogDate>20200819124057</GLogDate>
                <TZId>UTC</TZId>
                <TZOffset>+00:00</TZOffset>
            </TransmissionCreateDt>
            <TransactionCount>1</TransactionCount>
            <SenderHostName>https://xxx</SenderHostName>
            <SenderSystemID>https:xxx</SenderSystemID>
            <UserName>OOO</UserName>
            <SenderTransmissionNo>404836</SenderTransmissionNo>
            <ReferenceTransmissionNo>0</ReferenceTransmissionNo>
            <GLogXMLElementName>PlannedShipment</GLogXMLElementName>
            <NotifyInfo>
                <ContactGid>
                    <Gid>
                        <DomainName>OOO</DomainName>
                        <Xid>SYS</Xid>
                    </Gid>
                </ContactGid>
                <ExternalSystemGid>
                    <Gid>
                        <DOMAIN_NAME>OOO</DOMAIN_NAME>
                        <Xid>IOT_SYSTEM</Xid>
                    </Gid>
                </ExternalSystemGid>
            </NotifyInfo>
        </TransmissionHeader>`;

if(typeof(String.prototype.trim) === "undefined")
{
    String.prototype.trim = function() 
    {
        return String(this).replace(/^\s+|\s+$/g, '');
    };
}

function replaceDomainName (xmlPayload, oldValue, newValue)
{
  const  parser = new DOMParser();
  const xmlDoc = parser.parseFromString(xmlPayload,"text/xml");
  
  for(let tagName of ['DOMAIN_NAME', 'DomainName']) {
    const instances = xmlDoc.getElementsByTagName(tagName);
    for (let instance of instances) {
        if(instance.innerHTML.trim() == oldValue ) 
        instance.innerHTML = newValue;
    }
  };
  
  const s = new XMLSerializer();
  const d = document;
  const result = s.serializeToString(xmlDoc);
  
  return result;
}
const resultXML = replaceDomainName(XML, 'OOO', 'new.com');
console.log('resultXML', resultXML);

const textarea = document.createElement("textarea");
textarea.innerHTML = resultXML;
textarea.cols = 80;
textarea.rows = 24;
document.body.appendChild(textarea);
Pavel
  • 3,537
  • 1
  • 16
  • 14
  • thanks for your comment , but i'm using a limited functionality JS compiler , that doesn't support most of the commands – osfar Sep 08 '20 at 18:57