Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
fsp-xml-parser
Advanced tools
Readme
A better xml parser written in pure typescript and works well with both node and deno.
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'
// 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"
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."
// }
// ]
// }
// ]
// }
// ]
// }
// }
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.
FAQs
A better xml parser written in pure typescript and works well with both node and deno.
The npm package fsp-xml-parser receives a total of 58 weekly downloads. As such, fsp-xml-parser popularity was classified as not popular.
We found that fsp-xml-parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.