cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

How can I parse a XML from a SOAP Request in ThingWorx Javascript?

ddeoliveira
11-Garnet

How can I parse a XML from a SOAP Request in ThingWorx Javascript?

Hi,

I´m trying to parse a XML output came from a POSTXML request to add a infotable, but it´s being hard.
During my research I found some examples in the community but nothing expected.

I have this code that´s working fine:

var resultXML = Resources["ContentLoaderFunctions"].PostXML(params);

var returnXMLString = resultXML.*::Body.*::editItemResponse.return;

var result = ""+returnXMLString[0].toString();

My problem is that I don´t know how can I navigate into output using the sintaxy "resultXML.*::Body.*::...."


My XML output has some information that I want to put in a infotable but is more than one information that´s would be necessary a kind loop.

<WarehouseInternalId>alm</WarehouseInternalId>

<CurrentStockAmount>9999</CurrentStockAmount>

<BookedStockAmount>0</BookedStockAmount>


<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">

   <env:Header/>

   <env:Body>

      <ns2:receiveMessageResponse xmlns:ns2="http://www.ptc.com/">

         <receiveMessageResult>

<![CDATA[<ptcMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../xmlschema/general/requests_new/ptc.xsd">

<MessageInformation version="2.000"><UUID>97228f82-4984-dab7-3314-2b2058908c58</UUID>

<Type>Response</Type>

<Transaction>stocklevel</Transaction>

<StandardVersion>1.0</StandardVersion>

<SourceApplication>dts11buzios8480</SourceApplication>

<CompanyId>10</CompanyId>

<BranchId/><Product name="PTC" version="12.1.19"/>

<GeneratedOn>2017-12-15T16:54:12.555-02:00</GeneratedOn>

<DeliveryType>Sync</DeliveryType>

</MessageInformation>

<ResponseMessage>

<ReceivedMessage>

<SentBy>PPI</SentBy>

<UUID>e9a028b2-522e-4d3a-ad9e-d5c518ac03fe</UUID>

<Event>upsert</Event>

</ReceivedMessage><ProcessingInformation>

<ProcessedOn>2017-12-15T16:54:12.555-02:00</ProcessedOn>

<Status>OK</Status>

</ProcessingInformation>

<ReturnContent>

<ClosingStockDate>2017-12-15</ClosingStockDate>

<ListOfReturnItem>

<ReturnItem>

<BranchId>1</BranchId>

<ItemInternalId>.compras1</ItemInternalId>

<ReferenceCode/>

<ListOfStockBalance>

<StockBalance>

<WarehouseInternalId>fab</WarehouseInternalId>

<CurrentStockAmount>35067</CurrentStockAmount>

<BookedStockAmount>0</BookedStockAmount>

<LotNumber/>

<Address/>

</StockBalance>

<StockBalance>

<WarehouseInternalId>cq</WarehouseInternalId>

<CurrentStockAmount>0</CurrentStockAmount>

<BookedStockAmount>0</BookedStockAmount>

<LotNumber/>

<Address/>

</StockBalance>

<StockBalance>

<WarehouseInternalId>alm</WarehouseInternalId>

<CurrentStockAmount>9999</CurrentStockAmount>

<BookedStockAmount>0</BookedStockAmount>

<LotNumber/>

<Address/>

</StockBalance>

<StockBalance>

<WarehouseInternalId>fab</WarehouseInternalId>

<CurrentStockAmount>662</CurrentStockAmount>

<BookedStockAmount>0</BookedStockAmount>

<LotNumber/>

<Address>11</Address>

</StockBalance>

<StockBalance>

<WarehouseInternalId>CQ</WarehouseInternalId>

<CurrentStockAmount>40</CurrentStockAmount>

<BookedStockAmount>0</BookedStockAmount>

<LotNumber/>

<Address>11</Address>

</StockBalance>

</ListOfStockBalance>

</ReturnItem>

</ListOfReturnItem>

</ReturnContent>

</ResponseMessage>

</ptcMessage>]]>

</receiveMessageResult>

      </ns2:receiveMessageResponse>

   </env:Body>

</env:Envelope>

What´s is the best way to acomplish that?

Thank you,

Douglas

1 ACCEPTED SOLUTION

Accepted Solutions

var resultXML = new XML(content);

var xmlresult = resultXML.*::Body.*::receiveMessageResponse.*::receiveMessageResult.*;

var xmltext = String(xmlresult);

var newXML = new XML(xmltext);

xmlresult = newXML.*::ResponseMessage.*::ReturnContent.*::ListOfReturnItem.ReturnItem.ListOfStockBalance;

//result = xmlresult;

var params = {  

  infoTableName : "InfoTable",  

  dataShapeName : "StockBalance"  

};

var infoTable1 = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

var newRow = new Object(); 

  

//PARSE XML PUT INTO INFO TABLE  

// iterate through xml and add to info table  

for each (var tag in xmlresult.StockBalance) {

    newRow.WarehouseInternalId = tag.WarehouseInternalId;

    newRow.CurrentStockAmount = tag.CurrentStockAmount;

    newRow.BookedStockAmount = tag.BookedStockAmount;

    newRow.Address = tag.Address;

    infoTable1.AddRow(newRow);  

}  

  

// return info table  

var result = infoTable1;

View solution in original post

1 REPLY 1

var resultXML = new XML(content);

var xmlresult = resultXML.*::Body.*::receiveMessageResponse.*::receiveMessageResult.*;

var xmltext = String(xmlresult);

var newXML = new XML(xmltext);

xmlresult = newXML.*::ResponseMessage.*::ReturnContent.*::ListOfReturnItem.ReturnItem.ListOfStockBalance;

//result = xmlresult;

var params = {  

  infoTableName : "InfoTable",  

  dataShapeName : "StockBalance"  

};

var infoTable1 = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

var newRow = new Object(); 

  

//PARSE XML PUT INTO INFO TABLE  

// iterate through xml and add to info table  

for each (var tag in xmlresult.StockBalance) {

    newRow.WarehouseInternalId = tag.WarehouseInternalId;

    newRow.CurrentStockAmount = tag.CurrentStockAmount;

    newRow.BookedStockAmount = tag.BookedStockAmount;

    newRow.Address = tag.Address;

    infoTable1.AddRow(newRow);  

}  

  

// return info table  

var result = infoTable1;

Top Tags