パッケージの詳細

rexml

artdecocode1.8kMIT2.2.2

Simple XML parsing with a regular expression.

rexml, xml, parse, extract

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.

  • <kbd>tag*</kbd> (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.
  • <kbd>string*</kbd> 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.

SourceOutput
js 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) js [ { 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.

SourceOutput
js 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) js [ { 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.

SourceOutput
js 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)) json { "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

Art Deco © Art Deco 2019 Tech Nation Visa Tech Nation Visa Sucks

更新履歴

8 August 2019

2.2.2

  • [republish] Publish again due to yarn lag.

2.2.1

  • [republish] Publish again due to yarn lag.

2.2.0

  • [feature] Extract multiple tags at the same time.

3 August 2019

2.1.0

  • [externs] Add externs of the return type for the compiler.

27 July 2019

2.0.3

  • [fix] Correctly parse empty string attributes (attr="").

20 June 2019

2.0.2

  • [fix] Quote mismatch properties for Depack.

14 June 2019

2.0.1

  • [fix] Build the package.

2.0.0

  • [feature] Simplify the regex to remove unicode matching, renaming the old method to extractTagsSpec.

8 April 2019

1.4.0

  • [package] Publish the module field.
  • [doc] Fix JSDoc + suppress RegExp closure warning.
  • [build] Pre-build regexp.

10 February 2019

1.3.0

  • [feature] Expose extractProps and add option to disable parsing.

1 October 2018

1.2.0

  • [build] Build modules with alamode, keep babel for regex compilation.

19 July 2018

1.1.0

  • [fix] Parse inverted quotes (e.g., attr="'hello' world").
  • [fix] Extract attributes with unicode regular expression.
  • [fix] Handle tags without attributes or content.
  • [fix] Process multiple self-closing tags.

18 July 2018

1.0.0