$ gnpm install rexml
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
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.
(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
: 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.
Source | Output |
---|---|
|
|
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. |
It's possible to give an array of tags which should be extracted from the XML string.
Source | Output |
---|---|
|
|
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.
Source | Output |
---|---|
|
|
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": ""
}
]
© Art Deco 2019 | Tech Nation Visa Sucks |
---|
Copyright 2013 - present © cnpmjs.org | Home |