How to convert a string to XML using the javascipt model of Apigee

Not applicable

I am converting an XML to JSON using Javascript. The XML comes in as a string and I am not able to convert it into an XML object. Can anyone guide me in this regard.

As of now I have tried

var parser=new DOMParser();

xmlDoc=parser.parseFromString(xml,"text/xml");

It throws an error indicating DOMParser is undefined. The above code is only for browsers, can you help in the execution of Apigee envt?

0 7 3,091
7 REPLIES 7

Dear @dj1 , Is your API publicly accessible ? Ideally if your backend API sets response content type to application/xml then out of the box XML to JSON should work for you. Can you post the XML you are trying to convert ?

------=_Part_583_857466782.1661861839925
Content-Type: application/xop+xml;charset=UTF-8;type="text/xml"
Content-Transfer-Encoding: 8bit
Content-ID: <a076358d-5662-4da7-b05c-e783954d3cff>

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><env:Header/><env:Body><env:Fault><faultcode>env:Server</faultcode><faultstring>JBO-AR:::AR_TW_INVALID_TRX_NUMBER: &lt;MESSAGE&gt;&lt;NUMBER&gt;AR-855040&lt;/NUMBER&gt;&lt;TEXT&gt;This transaction number already exists. Enter a unique transaction number.&lt;/TEXT&gt;&lt;CAUSE&gt;&lt;/CAUSE&gt;&lt;ACTION&gt;&lt;/ACTION&gt;&lt;DETAILS&gt;&lt;/DETAILS&gt;&lt;INCIDENT&gt;&lt;/INCIDENT&gt;&lt;/MESSAGE&gt;</faultstring><detail><tns:ServiceErrorMessage xmlns:tns="http://xmlns.oracle.com/adf/svc/errors/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><tns:code>AR:::AR_TW_INVALID_TRX_NUMBER</tns:code><tns:message>JBO-AR:::AR_TW_INVALID_TRX_NUMBER: &lt;MESSAGE&gt;&lt;NUMBER&gt;AR-855040&lt;/NUMBER&gt;&lt;TEXT&gt;This transaction number already exists. Enter a unique transaction number.&lt;/TEXT&gt;&lt;CAUSE&gt;&lt;/CAUSE&gt;&lt;ACTION&gt;&lt;/ACTION&gt;&lt;DETAILS&gt;&lt;/DETAILS&gt;&lt;INCIDENT&gt;&lt;/INCIDENT&gt;&lt;/MESSAGE&gt;</tns:message><tns:severity>SEVERITY_ERROR</tns:severity><tns:exceptionClassName>oracle.apps.fnd.applcore.messages.ApplcoreException</tns:exceptionClassName></tns:ServiceErrorMessage></detail></env:Fault></env:Body></env:Envelope>
------=_Part_583_857466782.1661861839925--

 

is the response which i am getting as text 

i need from <env:Envelope to <env:Envelope as xml 

ummmm, ya, 

You have added a new question, as a response! to a post from literally 7 years ago, and your question has no relation to the 7-year old question.  Don't do that. 

Ask a new question, if you have a new question.

ask-a-question-2.png

you are right, DOMParser is not available in the Apigee env, basically there is no DOM here,

If you want to convert your XML to JSON, then i would strongly recommend you to use XMLToJson policy and then manipulate the json in your js policy

If you still need to parse XML - java is much easier option

If you still need to parse XML in js - you will have to rely on other js libs, maybe libxmljs or xmlbuilder and browserify it [i have not used them in apigee before], so you could include them in your js

Hi dj1,

I think by now you already know the solution but just for the sake of sharing knowledge I am posting this. I faced the same issue yesterday and this is how I resolved it:

 <AssignMessage async="false" continueOnError="false" enabled="true" name="AM-StrToXML">
    <DisplayName>AM-StrToXML</DisplayName>      
    <Properties/>      
 	<Set> 
	    <Headers>
            	<Header name="Content-Type">text/xml; charset=utf-8</Header>
            </Headers>
            <Verb>POST</Verb>
            <Payload contentType="text/xml">{xmlStr}</Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage> 

The target was sending me the encrypted xml data which I have to convert to XML then to JSON and then send it back to the API caller.

I think it is self explanatory, but anyone, please feel free to ask in case of any confusion.

Thanks,

Saif ul Islam Khan

can you help me out with xml to string in request,target accepting string.

Sure Vaibhav, how are you formulating your target request. Please share more details.