Git Product home page Git Product logo

xmldom-js's Introduction

Build Status

xmldom-js

Lightweight schema-based XML data extraction to plain objects (JSON)

Example

To read the data from the following XML

<?xml version='1.0' encoding='UTF-8'?>
  <rss xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
      <title>MRSS Title</title>

      <item>
        <title>Item 1</title>
        <media:content url="http://example.com/1.mp4" medium="video"/>
        <media:comment>Item 1 Comment</media:comment>
      </item>

      <item>
        <title>Item 2</title>
        <media:content url="http://example.com/2.mp4" medium="video"/>
        <media:comment>Item 2 Comment</media:comment>
      </item>

    </channel>
  </rss>

Data extraction schema:

import { attribute, content, string, readXML, prefixNamespace } from "xmldom-js";

var rssSchema = {
 rss: {
   channel: {
     title: content(string),
     item: [{
       title: content(string),
       "media:content": { url: attribute() },
       "media:comment": content(string)
     }]
   }
 }
};

First xml is parsed using the browser DOMParser and then read into plain JS objects using the schema:

  var rssXml = new DOMParser().parseFromString(rssString, "text/xml");

  var rssJSON = readXML(rssXml, rssSchema, prefixNamespace);

The resulting JSON:

{
  "rss": {
    "channel": {
      "title": "MRSS Title",
      "item": [
        {
          "title": "Item 1",
          "media:content": {
            "url": "http://example.com/1.mp4"
          },
          "media:comment": "Item 1 Comment"
        },
        {
          "title": "Item 2",
          "media:content": {
            "url": "http://example.com/2.mp4"
          },
          "media:comment": "Item 2 Comment"
        }
      ]
    }
  }
}

Namespace handling

A few modes for handling of the namespace are available:

  • ignoreNamespace - namespace and prefixes are ignored
  • prefixNamespace - use namespace prefixes as is ignoring namespaceURI. Should not be used if XML is provided by third-party as prefixes are not supposed to be stable, but quite safe for in-house XMLs.
  • namespaces(defaultURI, { prefix: URI }) - provide map of supported namespace with URIs.

Usage

Npm compatible packager (webpack) is required. CommonJS and ES6 modules are provided, transpiled to es5.

xmldom-js's People

Contributors

alexdrel avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

markbjerke

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.