title: Parsing XML in Node.js
body: <p>I am working on a Node.js app. I need to be able to parse a Sitemap.xml file. Currently, I have a file sitemap that looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"&gt;
  
    &lt;url&gt;
      &lt;loc&gt;http://www.example.com&lt;/loc&gt;
      &lt;lastmod&gt;2014-03-05&lt;/lastmod&gt;
      &lt;changefreq&gt;monthly&lt;/changefreq&gt;
    &lt;/url&gt;
  
    &lt;url&gt;
      &lt;loc&gt;http://www.example.com/contact&lt;/loc&gt;
      &lt;lastmod&gt;2014-03-05&lt;/lastmod&gt;
      &lt;changefreq&gt;never&lt;/changefreq&gt;
    &lt;/url&gt;
  
    &lt;url&gt;
      &lt;loc&gt;http://www.example.com/about&lt;/loc&gt;
      &lt;lastmod&gt;2015-03-01&lt;/lastmod&gt;
      &lt;changefreq&gt;monthly&lt;/changefreq&gt;
    &lt;/url&gt;
  &lt;/urlset&gt;
  </code></pre>
  
  <p>I am trying to parse this xml file and load it into my JavaScript class, which looks like this:</p>
  
  <pre><code>class SiteUrl {
    constructor() {
      this.loc = '';
      this.lastMod = null;
      this.changeFreq = 'never';
    }
  
    static loadFromSitemap(sitemapPath) {
  
    }
  }
  </code></pre>
  
  <p>Coming from a C# background, I know I could just do this:</p>
  
  <pre><code>public static List&lt;SiteUrl&gt; LoadFromSitemap(string sitemapPath)
  {
    // Load the sitemap into memory
    XDocument sitemap = XDocument.Load(sitemapPath);
  
    // Get the posts from the sitemap.
    List&lt;SiteUrl&gt; posts = (from post in sitemap.Root.Elements(ns + "url")
                           where ((string)post.Element(ns + "loc"))
                           select new SiteUrl(post)).ToList();
  
    return posts;
  }
  </code></pre>
  
  <p>I'm not sure how to read and parse Xml in the Node world though.</p>