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

rexml

v2.2.2

Published

Simple XML parsing with a regular expression.

Downloads

928

Readme

rexml

npm version

rexml is a Node.JS package for simple XML parsing with a regular expression. It's been tested to work for simple use cases (does work on nested tags).

yarn add rexml

Table Of Contents

API

The package is available by importing its default and named functions:

import rexml, { extractProps, extractTagsSpec, extractPropSpec } from 'rexml'

extractTags(  tag: string|!Array<string>,  string: string,): Return

Extract member elements from an XML string. Numbers and booleans will be parsed into their JS types.

  • tag* (string | !Array<string>): Which tag to extract, e.g., div. Can also pass an array of tags, in which case the name of the tag will also be returned.
  • string* string: The XML string.

The tags are returned as an array with objects containing content and props properties. The content is the inner content of the tag, and props is the attributes specified inside the tag.

import extractTags from 'rexml'

const xml = `
<html>
  <div id="d1"
    class="example"
    contenteditable />
  <div id="d2" class="example">Hello World</div>
</html>
`

const res = extractTags('div', xml)
[ { content: '',
    props: 
     { id: 'd1',
       class: 'example',
       contenteditable: true },
    tag: 'div' },
  { content: 'Hello World',
    props: { id: 'd2', class: 'example' },
    tag: 'div' } ]

Return: The return type.

| Name | Type | Description | | ------------ | --------------------------- | ------------------------------------------------------ | | content* | string | The content of the tag, including possible whitespace. | | props* | !Object<string, ?> | The properties of the element. | | tag* | string | The name of the extracted element. |

Extracting Multiple Tags

It's possible to give an array of tags which should be extracted from the XML string.

import extractTags from 'rexml'

const xml = `<html>
  <div id="d1"/>
  <div id="d2" class="example">Hello World</div>
  <footer>Art Deco, 2019</footer>
</html>
`

const res = extractTags(['div', 'footer'], xml)
[ { content: '',
    props: { id: 'd1' },
    tag: 'div' },
  { content: 'Hello World',
    props: { id: 'd2', class: 'example' },
    tag: 'div' },
  { content: 'Art Deco, 2019',
    props: {},
    tag: 'footer' } ]

extractProps(  string: string,  parseValue?: boolean,): Object<string,(boolean|string|number)>

Extracts the properties from the attributes part of the tag and returns them as an object. It will parse values if not specified otherwise.

import { extractProps, extractPropsSpec } from 'rexml'

const s = `id="d2"
class="example"
value="123"
parsable="true"
ignore="false"
2-non-spec
required`

const res = extractProps(s)
console.log(JSON.stringify(res, null, 2))

// don't parse booleans and integers
const res2 = extractProps(s, false)
console.log(JSON.stringify(res2, null, 2))

// conform to the spec
const res3 = extractPropsSpec(s)
console.log(JSON.stringify(res3, null, 2))
{
  "id": "d2",
  "class": "example",
  "value": 123,
  "parsable": true,
  "ignore": false,
  "2-non-spec": true,
  "required": true
}
{
  "id": "d2",
  "class": "example",
  "value": "123",
  "parsable": "true",
  "ignore": "false",
  "2-non-spec": true,
  "required": true
}
{
  "id": "d2",
  "class": "example",
  "value": 123,
  "parsable": true,
  "ignore": false,
  "required": true
}

extractTagsSpec(  tag: string,  string: string,): {content, props}[]

Same as the default method, but confirms to the XML specification in defining attributes.

import { extractTagsSpec } from 'rexml'

const xml = `
<html>
  <div id="d1" class="example" contenteditable />
  <div 5-non-spec>Attributes cannot start with a number.</div>
</html>`

const res = extractTagsSpec('div', xml)

console.log(JSON.stringify(res, null, 2))
[
  {
    "props": {
      "id": "d1",
      "class": "example",
      "contenteditable": true
    },
    "content": ""
  }
]

Copyright