- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2023 05:55 AM
Hi ,
we are getting the below XML
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<_1:GetPriceAndStockResponseMessage
xmlns:_1="hck/1"
xmlns:_1_1="httommon/1">
<_1_1:MessageHeader language="US">
<_1_1:Status>SUCCESS</_1_1:Status>
<_1_1:ResponseMessage>SUCCESS</_1_1:ResponseMessage>
<_1_1:ResponseDetails>
<_1_1:Severity>S</_1_1:Severity>
</_1_1:ResponseDetails>
</_1_1:MessageHeader>
<Orders>
<Order>
<OrderLineItems>
<OrderLineItem>
<SourceSystemOrderLineNumber>1-1A2EAWCX</SourceSystemOrderLineNumber>
<ItemNumber>3766B002</ItemNumber>
<Quantity>1</Quantity>
<ItemPriceInfo>
<CurrencyCode>EUR</CurrencyCode>
<Amount>75</Amount>
<UnitPrice>75</UnitPrice>
<ListPrice>93.75</ListPrice>
</ItemPriceInfo>
</OrderLineItem>
</OrderLineItems>
</Order>
</Orders>
</_1:GetPriceAndStockResponseMessage>
</soapenv:Body>
</soapenv:Envelope>
am using below code :
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2023 09:01 AM
@String ,
Please try below mentioned code and let me know:
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(text);
var arr = []
var OrderLineItemNode= xmlDoc.getFirstNode("//OrderLineItem");
while(OrderLineItemNode!= null) {
var nodeValues = {};
nodeValues["ItemNumber"] = xmlDoc.getNodeText("//ItemNumber");
nodeValues["Amount"] = xmlDoc.getNodeText("//Amount");
arr.push(nodeValues);
OrderLineItemNode= xmlDoc.getNextNode(OrderLineItemNode);
}
gs.info(JSON.stringify(arr))
If my answer solved your issue, please mark my answer as ✅Correct & 👍Helpful based on the Impact.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2023 06:26 AM
Hi @String ,
You can try updating your code to use the ServiceNow XMLDocument2 API to parse the incoming XML
if (status == 200) {
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(responseBody);
var resposeStatus = xmlDoc.getNodeText("//_1_1:Status");
gs.info("price res"+resposeStatus);
if (resposeStatus == 'SUCCESS') {
var orders = xmlDoc.getNode("//Orders");
var orderNodes = orders.getChildNodes();
var mrvsArray = [];
for (var i = 0; i < orderNodes.getLength(); i++) {
var orderNode = orderNodes.item(i);
var orderLineItems = orderNode.getNode("//OrderLineItems");
var orderLineItemNodes = orderLineItems.getChildNodes();
for (var j = 0; j < orderLineItemNodes.getLength(); j++) {
var orderLineItemNode = orderLineItemNodes.item(j);
var mrvs1 = {};
mrvs1.ItemNumber = orderLineItemNode.getNodeText("ItemNumber");
mrvs1.amount = orderLineItemNode.getNodeText("ItemPriceInfo/Amount");
mrvsArray.push(mrvs1);
}
}
gs.info('price inside'+JSON.stringify(mrvsArray));
return JSON.stringify(mrvsArray);
} else {
this.transactionLog('500', requestBody, responseBody, 'Outbound-GetPrice', 'GetPrice');
return 500;
}
} else {
this.transactionLog(status, requestBody, responseBody, 'Outbound-GetPrice', 'GetPrice');
return 500;
}
In this updated code,using the ServiceNow XMLDocument2 API to parse the incoming XML and retrieve the values of ItemNumber and Amount from each OrderLineItem. Iterating over the OrderLineItem nodes using the getChildNodes() method and then using the getNodeText() method to retrieve the values of ItemNumber and Amount.
Note that the getNode() method is used to retrieve the OrderLineItems node and the getNodeText() method is used to retrieve the text content of the child nodes.
Shravan
Please mark this as helpful and correct answer, if this helps you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2023 06:47 AM
@Sai Shravan I have tried the same code but getting empty array[],not going to for loop.
I have kept logs at kine 73 and we getting node details as undefined,
Please guid me
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2023 07:00 AM
Hi @String ,
If you are getting "undefined" while trying to access the nodes, it means that the nodes you are trying to access do not exist in the XML document. You may need to check the structure of the XML document and make sure that you are using the correct node names and paths.
gs.info('XML document: ' + xmlDoc.toXMLString());
Check the log the names of the nodes that you are trying to access:
var itemNumberNodes = xmlDoc.getElementsByTagName("ItemNumber");
gs.info('Number of ItemNumber nodes: ' + itemNumberNodes.length);
var amountNodes = xmlDoc.getElementsByTagName("Amount");
gs.info('Number of Amount nodes: ' + amountNodes.length);
Shravan
Please mark this as helpful and correct answer, if this helps you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2023 07:14 AM
am getting below XML response from API
<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><_1:GetPriceAndStockResponseMessage xmlns:_1="http://www.canon-europe.com/cdm/services/priceandstock/1" xmlns:_1_1="http://www.canon-europe.com/cdm/common/1"><_1_1:MessageHeader language="US"><_1_1:Status>SUCCESS</_1_1:Status><_1_1:ResponseMessage>SUCCESS</_1_1:ResponseMessage><_1_1:ResponseDetails><_1_1:Severity>S</_1_1:Severity></_1_1:ResponseDetails></_1_1:MessageHeader><Orders><Order><OrderLineItems><OrderLineItem><SourceSystemOrderLineNumber>1-1A2EAWCX</SourceSystemOrderLineNumber><ItemNumber>3766B002</ItemNumber><Quantity>1</Quantity><ItemPriceInfo><CurrencyCode>EUR</CurrencyCode><Amount>75</Amount><UnitPrice>75</UnitPrice><ListPrice>93.75</ListPrice></ItemPriceInfo></OrderLineItem></OrderLineItems></Order></Orders></_1:GetPriceAndStockResponseMessage></soapenv:Body></soapenv:Envelope>
gs.info('XML document: ' + xmlDoc.toXMLString());...getting undefined !!