Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@xmpp/xml

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@xmpp/xml

XMPP XML for JavaScript

  • 0.5.0
  • npm
  • Socket score

Version published
Weekly downloads
12K
decreased by-38.65%
Maintainers
1
Weekly downloads
 
Created
Source

xml

Install

Note, if you're using @xml/client or @xml/component, you don't need to install @xmpp/xml yourself.

npm install @xmpp/xml or yarn add @xmpp/xml

const xml = require('@xmpp/xml')
const {xml} = require('@xmpp/client')
const {xml} = require('@xmpp/component')

Writing

There's 2 methods for writing XML with xmpp.js

factory

const xml = require('@xmpp/xml')

const recipient = 'user@example.com'
const days = ['Monday', 'Tuesday']
const message = xml(
  'message',
  {to: recipient},
  xml('body', {}, 1 + 2),
  xml('days', days.map(day => xml('day', {}, day)))
)

Used in xmpp.js source code.

JSX

/** @jsx xml */

const xml = require('@xmpp/xml')

const recipient = 'user@example.com'
const days = ['Monday', 'Tuesday']
const message = (
  <message to={recipient}>
    <body>{1 + 2}</body>
    <days>
      {days.map(day => (
        <day>${day}</day>
      ))}
    </days>
  </message>
)

Used in xmpp.js tests.

Requires a preprocessor but if you're already using Babel and/or need to write big chunks of XML it's a good choice. See our .babelrc for a configuration example.

Reading

attributes

The attrs properties holds xml attributes for an element.

message.attrs.to // user@example.com

text

Returns the text value of an element

message.getChild('body').text() // '3'

getChild

Get child element by name.

message.getChild('body').toString() // <body>3</body>

getChildren

Get children elements by name.

message.getChild('days').getChildren('day') // [...]

getChildText

Get child element text value.

message.getChildText('body') // '3'

Editing

attributes

The attrs properties holds xml attributes for an element.

message.attrs.type = 'chat'
Object.assign(message.attrs, {type: 'chat'})

text

Set the text value of an element

message.getChild('body').text('Hello world')

append

Adds text or element nodes to the last position. Returns the parent.

message.append(xml('foo'))
message.append('bar')
message.append(days.map(day => xml('day', {}, day)))
// <message>
//   ...
//   <foo/>
//   bar
//   <day>Monday</day>
//   <day>Tuesday</day>
// </message>

prepend

Adds text or element nodes to the first position. Returns the parent.

message.append(xml('foo'))
message.append('bar')
message.append(days.map(day => xml('day', {}, day)))
// <message>
//   <day>Tuesday</day>
//   <day>Monday</day>
//   bar
//   <foo/>
//   ...
// </message>

remove

Removes a child element.

const body = message.getChild('body')
message.remove(body)

JSON

You can embed JSON anywhere but it is recommended to use an appropriate semantic.

/** @jsx xml */

// write
message.append(
  <myevent xmlns="xmpp:example.org">
    <json xmlns="urn:xmpp:json:0">{JSON.stringify(days)}</json>
  </myevent>
)

// read
JSON.parse(
  message
    .getChild('myevent', 'xmpp:example.org')
    .getChildText('json', 'urn:xmpp:json:0')
)

See JSON Containers

Keywords

FAQs

Package last updated on 07 Oct 2018

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc