npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

utils-xml2object

v2.0.2

Published

Extract object from xml using xpath syntax

Downloads

9

Readme

xml2Obj

Test

xml2obj is a utility that convert a xml document in an object in regard to a given mapping

It uses the xpath syntax. Here is a helpfull cheatsheet

 const xml = "\
  <HEAD> \
    <TITLE>My title</TITLE> \
    <VERSION myattr='truc'> nothing </VERSION> \
  </HEAD>"

  const extractor = xml2Obj.extract(xml, {
    title: '/HEAD/TITLE/text()', // this will take the text content withing the tag TITLE
    attr: '/HEAD/VERSION/@myattr' // This will take the attribute "attr" attached to the VERSION tag,
  }

  // {title: 'My title', attr: 'truc'}

Extract html

When html is embedded within the html you have to specify it

const extractor = xml2Obj.extract(xml, {
  content: {
    path: '/HEAD/TITLE' // this will capture every thing withing the Title Element stripping out the TITLE tag
    isHtml: true,
  },
  attr: '/HEAD/VERSION/@myattr',
}

Extract nested object

Sometime it is handy to create an array or a nested object from a group of keys You can do that using the syntax below

const xml = "\
  <HEAD> \
    <TITLE>My title</TITLE> \
    <VERSIONS> \
       <VERSION num='v1'> \
          <ELEM myattr='this is v1'>v1text</ELEM> \
      </VERSION> \
       <VERSION num='v2'> \
          <ELEM myattr='this is v2'>v2text</ELEM> \
      </VERSION> \
    </VERSIONS> \
  </HEAD>"

  const extractor = xml2Obj.extract(xml, {
    myarray: {
      path: '/HEAD/VERSIONS/VERSION',
      mapping: {
        num: '/@num', // the num attribute of every VERSION 
        text: '/ELEM/text()', //The inner Text within all the ELEM tag in the VERSIONS
        attr: '/ELEM/@myattr', //You get it... 
      }
    }
  })

  //returns   { myarray: [{num: 'v1', text: 'v1text', attr: 'this is v1'}, {num: 'v2', text: 'v2text', attr: 'this is v2'}]

Check the tests for more examples

Strict Mode

By default all missing throws an error If you want to deactivate this beaviour you can call the extract function with an tolerance parameter The missing keys wont appear in the resulting object

 xml2Obj.extract(xml, {...mapping}, {tolerant: true})

You can also set the tolerance at key level:

  const extractor = xml2Obj.extract(xml, {
    firstVersion: {
      path: '/HEAD/VERSIONS/VERSION[1]/@doesnotexists',
      tolerance: true,
    },
  }, { tolerance: false })

NOTE: if tolerance is set to true at the global level. Tolerance at key level has no effect.

Namespaces

Whenever namespaces are used in the document you should specify the name space useds and their ids in the options The namespaces and id are specified in the xmls document

  const xml = "\
  <HEAD> \
    <NS1:TITLE xmlns:NS1='NS1ID'> \
      <NS1:SUBTITLE>My Sub Title</NS1:SUBTITLE> \
    </NS1:TITLE> \
    <NS2:TITLE xmlns:NS2='NS2ID'> \
      <NS2:SUBTITLE>My Sub Title 2</NS2:SUBTITLE> \
    </NS2:TITLE> \
    <CONTENT> Content </CONTENT> \
  </HEAD>"

  const extractor = xml2Obj.extract(xml, {
    subtitle: '/HEAD/NS1:TITLE/NS1:SUBTITLE/text()',
    subtitle2: '/HEAD/NS2:TITLE/NS2:SUBTITLE/text()',
    content: '/HEAD/CONTENT/text()',
  }, {namespaces: {
      'NS1': 'NS1ID',
      'NS2': 'NS2ID',
    }})

  // returns { subtitle: 'My Sub Title, subtitle2: My Sub title 2 , content: 'Content'}

Apply function to results

It is possible to apply a function to the result a the key level using the function apply

  const xml = "\
  <HEAD> \
    <TITLE>My title; garbage</TITLE> \
    <VERSION myattr='truc'> nothing </VERSION> \
  </HEAD>"

  const extractor = xml2Obj.extract(xml, {
    title: {
      path: '/HEAD/TITLE/text()',
      apply: (title) => title.split(';')[0],
    },
    attr: '/HEAD/VERSION/@myattr',
  })

Set default to key

You can set a default value to keys using the syntax below:

  const extractor = xml2Obj.extract(xml, {
    title: {
      path: '/HEAD/TITLE/text()',
      default: 'value if missing',
    },
  }, {tolerance: true})

Extract trees

Tree extraction can be done specifiying the name of the recursive node the _childrens key will be added when any child is found:

NOTE: The apply function cannot be used along with tree

  const xml = "<HEAD> \
    <TITLE>My title</TITLE> \
    <NAV> \
      <ITEM num='1' name='parent'> \
        <ITEM num='11' name='child'> \
          <ITEM num='111' name='child'> </ITEM> \
        </ITEM> \
      </ITEM> \
      <ITEM num='2' name='parent'> \
        <ITEM num='21' name='child'> </ITEM> \
        <ITEM num='22' name='child'> </ITEM> \
      </ITEM> \
    </NAV> \
  </HEAD>"

  const extractor = xml2Obj.extract(xml, {
    docs: {
      path: "//HEAD/NAV/ITEM",
      tree: 'ITEM',
      tolerance: true,
      mapping: {
        num: '/@num',
        name: '/@name',
      }
    }
  }

TEST

run test suite