Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fullstackplayer/ts-xml-parser
A better xml parser written in pure typescript and works well with deno.
https://github.com/fullstackplayer/ts-xml-parser
deno typescript xml
Last synced: 4 months ago
JSON representation
A better xml parser written in pure typescript and works well with deno.
- Host: GitHub
- URL: https://github.com/fullstackplayer/ts-xml-parser
- Owner: FullStackPlayer
- License: gpl-3.0
- Created: 2021-03-08T10:04:42.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2023-05-11T10:36:59.000Z (over 1 year ago)
- Last Synced: 2024-10-13T08:21:09.355Z (4 months ago)
- Topics: deno, typescript, xml
- Language: TypeScript
- Size: 89.8 KB
- Stars: 12
- Watchers: 1
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ts-xml-parser
A better xml parser written in pure typescript and works well with both node and deno.# Import to your project
### For Node.js
Install it first:
~~~bash
// pay attention to the package name 'fsp-xml-parser'
npm install fsp-xml-parser
// or
yarn add fsp-xml-parser
~~~Then import it:
~~~js
// CommonJS
const { parse } = require('fsp-xml-parser')
// ES Module
// In nodejs, you need bundlers(such as webpack/parcel...) support for now, this line of code couldn't run in nodejs directly.
// But if typescript is your good friend, this is the right way.
import { parse } from 'fsp-xml-parser'
~~~### For Deno
~~~ts
// remote import in Deno
import parse from "https://denopkg.com/FullStackPlayer/ts-xml-parser/mod.ts"
// latest update: now you can import from deno.land
import parse from "https://deno.land/x/ts_xml_parser/mod.ts"
// local import in Deno
import parse from "path/to/parser.ts"
~~~# Usage
Simple:
~~~ts
let xml = `
`
let parsed = parse(xml)
// parsed:
// {
// "declaration": {
// "attributes": {
// "version": "1.0",
// "encoding": "utf-8"
// }
// },
// "root": {
// "name": "tagA"
// }
//}
~~~Namespace:
~~~ts
let xml = `
`
let parsed = parse(xml,true) // true means prefixing namespace before tag name
// parsed:
// {
// "declaration": {
// "attributes": {
// "version": "1.0",
// "encoding": "utf-8"
// }
// },
// "root": {
// "name": "DAV:propfind",
// "attributes": {
// "xmlns": "DAV:",
// "xmlns:R": "RES:"
// },
// "children": [
// {
// "name": "RES:allprop"
// }
// ]
// }
// }
~~~Content:
~~~ts
let xml = `
abc
`
let parsed = parse(xml)
// parsed:
// {
// "declaration": {
// "attributes": {
// "version": "1.0",
// "encoding": "utf-8"
// }
// },
// "root": {
// "name": "tagA",
// "content": "abc"
// }
// }
~~~Mixed Content (a node owns text content and child nodes at the same time):
~~~ts
let xml = `I have a son named JohnJohnson.
`
let parsed = parse(xml)
// parsed:
// {
// "declaration": {
// "attributes": {
// "version": "1.0",
// "encoding": "utf-8"
// }
// },
// "root": {
// "name": "father",
// "children": [
// {
// "name": "fullname",
// "content": "Johnson"
// }
// ],
// "content": "I have a son named John."
// }
// }
~~~Deep Structure:
~~~ts
let xml = `
There is a big train stationTianqiao Station.
`
let parsed = parse(xml)
// parsed
// {
// "declaration": {
// "attributes": {
// "version": "1.0",
// "encoding": "utf-8"
// }
// },
// "root": {
// "name": "China",
// "children": [
// {
// "name": "Henan"
// },
// {
// "name": "Shandong",
// "children": [
// {
// "name": "Jinan",
// "attributes": {
// "alias": "Quancheng"
// },
// "children": [
// {
// "name": "Lixia"
// },
// {
// "name": "Tianqiao",
// "children": [
// {
// "name": "station",
// "attributes": {
// "type": "train"
// },
// "content": "Tianqiao Station"
// }
// ],
// "content": "There is a big train station."
// }
// ]
// }
// ]
// }
// ]
// }
// }
~~~# ATTENTION
- If you have single `\` characters in ``, it will be ignored as an `escape character`, if you are sure a single `\` is necessary, type `\\` instead.
- `` can not be nested in a node content, if you really want to do that, encode your inner `` first, of course the receiver side should decode the content either.
## Enjoy Yourself!