xml-extract

Extract elements from XML

Usage no npm install needed!

<script type="module">
  import xmlExtract from 'https://cdn.skypack.dev/xml-extract';
</script>

README

xml-extract

Node module for extracting elements from XML

Installtion

npm install xml-extract

Usage

const XML = `
<urlset xmlns="http://www.example.com">
  <url>
    <loc>http://www.example.com/1</loc>
  </url>
  <url>
    <loc>http://www.example.com/2</loc>
  </url>
  <url>
    <loc>http://www.example.com/3</loc>
  </url>
<urlset>`

const XMLExtract = require('xml-extract');

// Extract <loc> without tags
XMLExtract(XML, 'loc', false, (error, element) => {
  if (error) {
    throw new Error(error);
  }

  console.log(element);

  // output: 
  // http://www.example.com/1
  // http://www.example.com/2
  // http://www.example.com/3
});

// Extract <loc> with tags
XMLExtract(XML, 'loc', true, (error, element) => {
  if (error) {
    throw new Error(error);
  }

  console.log(element);

  // output: 
  // <loc>http://www.example.com/1</loc>
  // <loc>http://www.example.com/2</loc>
  // <loc>http://www.example.com/3</loc>
});

Test

npm run test