Parsing XML in ES6

📅 November 09, 2017

👷 Chris Power

Let’s say you’re a developer and you’re working on the latest disruptive app. This app tells you the property value of elderly homes in your area. Really cutting edge stuff. Let’s say you’re relying on the zillow API for your property data. There’s just one problem with this API: It returns, GASP, XML!

###Welcome to the real world. As a rock star developer, you’ve never dealt with XML APIs. “Wait, this isn’t JSON API spec compliant?” you ask yourself in disbelief. No friend, this is the real world. In the real world, nobody cares about you. Nobody holds your hand and tells you everything will be alright. Nobody is going to pay your bills, make your coffee, rock you to sleep and tell you you’re the prettiest developer in the whole school.

Where was I? Oh right, XML APIs.

###Enter DOMParser DOMParser is an amazing class in javascript. DOMParser can parse an HTML or XML string and create a new HTML document from it. Essentially, you can parse an XML string, and use all your favorite DOM functions to iterate over objects. It’s unbelievable! Let’s go over an example.

Lets say you have this XML string from an API response:

<?xml version="1.0" encoding="utf-8"?>
        <address>1220 Hillside Rd</address>
        <citystatezip>Fairfield, CT, 06824</citystatezip>
        <text>Request successfully processed</text>
                    <street>1220 Hillside Rd</street>
                    <amount currency="USD">1749560</amount>
                    <oneWeekChange deprecated="true"></oneWeekChange>
                    <valueChange duration="30" currency="USD">-37584</valueChange>
                        <low currency="USD">1662082</low>
                        <high currency="USD">1837038</high>
                    <region name="Fairfield" id="31506" type="city">
</SearchResults:searchresults><!-- H:012 T:95ms S:869 R:Thu Nov 09 06:46:53 PST 2017 B:5.0.49536.3-hotfix_2017-11-06.b48c77d~hotfix-platform-for-2017-11-06.a932834 -->

How in the world can you parse this?

Let’s take this xml string and parse it into a DOM document! Then we can fetch the zpid of this property.

let request = new Request(`;=${someCityStateAndZip}&address;=${someAddress}`);

fetch(request).then((results) => {
  // results returns XML. lets cast this to a string, then create
  // a new DOM object out of it!
    .then(( str ) => {
      let responseDoc = new DOMParser().parseFromString(str, 'application/xml');
      return responseDoc.getElementsByTagName('zpid')[0].textContent;

This will return the ZPID of the property we were looking up through the zillow API!

Breaking it down

  1. Get an XML response from the API
  2. use new DOMParser.parseFromString(string, “application/xml”) to parse result
  3. use any document function to parse for the data you want

###YAY! I find that javascript has an awesome built in solution to handling XML. Parsing an XML string into a DOM tree just feels right, and is very intuitive.

Lets Work Together

We're trusted by large, medium, and small companies all over the world

Have something you're working on?

Tell Us About It