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

Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

Sending and Receiving JSON and XML Part 3

No ratings

 

 

Step 5: Handling and Receiving XML Objects

 

Working with JSON with JavaScript can be easy and simple. Working with XML while using JavaScript can be more challenging, because XML is so tightly structured. This can affect how you setup code and how you use the Rhino JavaScript engine for your needs.

 

NOTE: Examples of these services can be found in the ** XMLProcessorThing** entity, which is provided in the download.

 

Handling XML

 

XML can be handled using two methods. The first method is converting the XML string to an XML object. The second methos is creating an XML object, then assigning the raw XML to that variable.

 

The simple XML object is as follows:

    <note>
        <to>Janice</to>
        <from>Billy</from>
        <heading>Please Remind Me</heading>
        <body>Take out the trash this weekend!</body>
    </note>


First, create the entity you will use for both methods.

  1. In ThingWorx Composer, click the + New at the top of the screen.
  2. Select Thing in the dropdown.
  3. Name the Thing XMLProcessorThing and click Save.
  4. Click the Services tab.
  5. Click Save and let’s begin with the first method of handling XML in ThingWorx.

 

Parsing XML Using Method 1

 

Now, you will create the service to handle the first method of XML handling. This is a simple method to easily convert XML string and grabbing a field in the XML.

 

  1. In the Services tab of the XMLProcessorThing Thing, create a new service called ParseXML.
  2. The service will have no parameters.
  3. Set the Output as String.
  4. Add the following JavaScript to help encode the string and return an HTML friendly string.

    /*jshint multistr: true */
    var xml = new XML("<note> \
            <to>Janice</to> \
            <from>Billy</from> \
            <heading>Please Remind Me</heading> \
            <body>Take out the trash this weekend!</body> \
        </note>");
    
    var result = "Sending note to " + String(xml.*::to);

 

5. Click Save and you’re all done with this service to parse XML string and grab information from it.

 

Parsing XML Using Method 2

 

The second method will show errors and warnings, if you’re using the Lint setting in your service JavaScript code window. This method will be very helpful in handling XML responses from a service call.

 

  1. In the Services tab of the XMLProcessorThing Thing, create a new service called ParseRawXML.
  2. The service will have no parameters.
  3. Set the Output as String.
  4. Add the following JavaScript to help encode the string and return an HTML friendly string.

    var xml = new XML();
    xml = <note>
            <to>Janice</to>
            <from>Billy</from>
            <heading>Please Remind Me</heading>
            <body>Take out the trash this weekend!</body>
          </note>
    
    var result = "Sending note to " + String(xml.to);

 

5. Click Save and you’re all done.

 

 

Step 6: Next Steps

 

Congratulations! You've successfully completed the Sending and Receiving JSON and XML guide, and learned how to use the ThingWorx Platform to handle REST requests and send payload for external SOAP and REST services.

 

Learn More

 

We recommend the following resources to continue your learning experience:

 

CapabilityGuide
BuildDesign Your Data Model
BuildUse REST API to Access ThingWorx

 

Additional Resources

 

If you have questions, issues, or need additional information, refer to:

 

ResourceLink
CommunityDeveloper Community Forum
SupportREST API Help Center
Version history
Last update:
‎Nov 02, 2022 10:29 AM
Updated by:
Labels (1)
Contributors