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

fsp-xml-parser

v1.0.0

Published

A better xml parser written in pure typescript and works well with both node and deno.

Downloads

816

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:

// pay attention to the package name 'fsp-xml-parser'
npm install fsp-xml-parser
// or
yarn add fsp-xml-parser

Then import it:

// 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

// 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:

let xml = `
<?xml version="1.0" encoding="utf-8" ?> 
<tagA></tagA>
`
let parsed = parse(xml)
// parsed:
// {
//    "declaration": {
//        "attributes": {
//            "version": "1.0",
//            "encoding": "utf-8"
//        }
//    },
//    "root": {
//        "name": "tagA"
//    }
//}

Namespace:

let xml = `
<?xml version="1.0" encoding="utf-8" ?> 
<propfind xmlns="DAV:" xmlns:R="RES:">
    <R:allprop/>
</propfind>
`
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:

let xml = `
<?xml version="1.0" encoding="utf-8" ?> 
<tagA>
    abc<![CDATA[123一二三]]>
</tagA>
`
let parsed = parse(xml)
// parsed:
// {
//     "declaration": {
//         "attributes": {
//             "version": "1.0",
//             "encoding": "utf-8"
//         }
//     },
//     "root": {
//         "name": "tagA",
//         "content": "abc<![CDATA[123一二三]]>"
//     }
// }

Mixed Content (a node owns text content and child nodes at the same time):

let xml = `
<?xml version="1.0" encoding="utf-8" ?>
<father>
    I have a son named John<fullname>Johnson</fullname>.
</father>
`
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:

let xml = `
<?xml version="1.0" encoding="utf-8" ?>
<China>
    <Henan></Henan>
    <Shandong>
        <Jinan alias="Quancheng">
            <Lixia />
            <Tianqiao>
                There is a big train station<station type="train">Tianqiao Station</station>.
            </Tianqiao>
        </Jinan>
    </Shandong>
</China>
`
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 <![CDATA[]>, it will be ignored as an escape character, if you are sure a single \ is necessary, type \\ instead.

  • <![CDATA[]]> can not be nested in a node content, if you really want to do that, encode your inner <![CDATA[]]> first, of course the receiver side should decode the content either.

Enjoy Yourself!