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

xml-js-parse

v0.0.1

Published

Parse the xml as a js object or build an xml through a js object

Downloads

5

Readme

xml-js-parse

Parse the xml as a js object or build an xml through a js object.

Usage

npm i xml-js-parse -S
const xmlParser = new require('xml-js-parse').Parser()
const xml = `
<xml>
  <name>xiaobai</name>
</xml>
`
const obj = xmlParser.parseString(xml)

new Parser([options])

  • options <Object> 配置对象
    • trim <boolean> 去除文本节点头尾的空格。默认为 false
    • normalize <boolean> 去除文本节点内的空格。默认为 false
    • normalizeTags <boolean> 将所有标签名转成小写字母。默认为 false
    • ignoreAttrs <boolean> 忽略属性节点。默认为 false
    • attrkey <string> 设置属性节点的字段名。默认为 $
    • charkey <string> 设置文本节点的字段名。默认为 _
    • explicitArray <boolean> 保存子元素为数组。默认为 true 。当设置为 false ,只有出现多个相同标签名的子元素时才合并成数组。

parseString

解析 xml 数据为对象。

  • xml <string>
const xmlParser = new Parser({ explicitArray: false })
const xml = `
  <user>
    <name>xiaobai</name>
    <age>12</age>
  </user>
  <user>
    <name>xiaohong</name>
    <age>11</age>
  </user>
  <count>2</count>
`
const result = xmlParser.parseString(xml)
/**
{
  user: [
    {
      name: 'xiaobai',
      age: '12'
    },
    {
      name: 'xiaohong',
      age: '11'
    }
  ],
  count: '2'
}
*/

new Builder([options])

  • options <Object> 配置对象
    • rootName <string> 根节点名称。默认为 root
    • renderOpts <Object>
      • pretty <boolean> 使用格式。默认为 true
      • indent <string> 缩进。默认为 ' '
      • newline <string> 换行。默认 \n
    • xmldec <Object>
      • version <string> 头信息,版本号。默认 1.0
      • encoding <string> 头信息,编码格式。默认 UTF-8
      • standalone <boolean> 头信息,独立。默认 true
    • headless <boolean> 去除头信息。默认 false
    • cdata <boolean> 当文本节点包含非法 xml 字符时,用<![CDATA[……]]> 包含。默认 false

buildObject

  • obj <Object> 要构建 xml 的对象。
const xmlBuilder = new Builder()
const obj = { name: 'xiaobai' }
const xml = xmlBuilder.buildObject(obj)
/**
<root>
  <name>xiaobai</name>
</root>
*/