1.1.0 • Published 8 years ago

xml-extract v1.1.0

Weekly downloads
87
License
MIT
Repository
github
Last release
8 years ago

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