GithubHelp home page GithubHelp logo

doga / xml Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 14 KB

An XML parser and writer with namespace support. Packaged as a JavaScript ES6 module.

License: Apache License 2.0

JavaScript 100.00%
xml xml-parser xml-serializer runnable-readme

xml's Introduction

XML parser and writer

An XML parser and writer with namespace support. Packaged as a JavaScript ES6 module.

Usage examples

Tip: Run the examples below by typing this in your terminal (requires Deno):

deno run \
  --allow-net --allow-run --allow-env --allow-read \
  https://deno.land/x/[email protected]/mod.ts \
  --dax=false --mode=isolated \
  https://raw.githubusercontent.com/doga/xml/master/README.md
Example: Parse and re-generate an XML string.
description = '''
Running this example is safe, it will not read or write anything to your filesystem.
'''
import { Parser, Writer } from 'https://esm.sh/gh/doga/[email protected]/mod.mjs';

const 
xml = 
`<!-- 
  A Qworum script that calls a login endpoint.
  The Web application uses the Qworum browser extension
  to run the script.
-->
<sequence xmlns='https://qworum.net/ns/v1/instruction/'>
  <try>
    <call href='/login' />
    <catch faults='["* cancelled"]'>
      <goto href='/loginCancelled' />
    </catch>
    <catch faults='["* failed"]'>
      <goto href='/loginFailed' />
    </catch>
  </try>
  <goto href='/account' />
</sequence>`,

doc = new Parser(xml).document, // XmlDocument

// serialise the root element
xml2 = Writer.elementToString(doc.root);

console.info(xml2);

Sample output for the code above:

<sequence xmlns="https://qworum.net/ns/v1/instruction/">
  <try>
    <call href="/login"></call>
    <catch faults="[&quot;* cancelled&quot;]">
      <goto href="/loginCancelled"></goto>
    </catch>
    <catch faults="[&quot;* failed&quot;]">
      <goto href="/loginFailed"></goto>
    </catch>
  </try>
  <goto href="/account"></goto>
</sequence>
Example: Build an XML document programmatically.
description = '''
Running this example is safe, it will not read or write anything to your filesystem.
'''
import { Parser, Writer, XmlDocument, XmlElement, XmlComment } from 'https://esm.sh/gh/doga/[email protected]/mod.mjs';

const 
doc = new XmlDocument([
  new XmlComment(
    `A Qworum script that calls a login endpoint.
    The Web application uses the Qworum browser extension
    to run the script.`
  ),
  new XmlElement(
    'sequence', {xmlns: 'https://qworum.net/ns/v1/instruction/'},
    [
      new XmlElement(
        'try', {},
        [
          new XmlElement('call', {href: '/login'}),
          new XmlElement(
            'catch', {faults: '["* cancelled"]'},
            [new XmlElement('goto', {href: '/loginCancelled'})]
          ),
          new XmlElement(
            'catch', {faults: '["* failed"]'},
            [new XmlElement('goto', {href: '/loginFailed'})]
          ),
        ]
      ),
      new XmlElement('goto', {href: '/account'}),
    ]
  )
]),

// serialise the root element
xml = Writer.elementToString(doc.root);

console.info(xml);

Sample output for the code above:

<sequence xmlns="https://qworum.net/ns/v1/instruction/"><try><call href="/login"></call><catch faults="[&quot;* cancelled&quot;]"><goto href="/loginCancelled"></goto></catch><catch faults="[&quot;* failed&quot;]"><goto href="/loginFailed"></goto></catch></try><goto href="/account"></goto></sequence>

โˆŽ

xml's People

Contributors

doga avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.