0.0.1 • Published 5 years ago

@project-reporter/xml-processor v0.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

Project Reporter - XML Processor

A DOM style XML processor, built based on the W3C Recommendation.

This package is a part of Project Reporter NodeJs implementation, it's open-source for the community, licensed under the MIT license, so, feel free to copy, modify or use at as you need.

Installation

Using NPM.

npm i @project-reporter/xml-processor

Using Yarn.

yarn add @project-reporter/xml-processor

Usage example

const { DocType } = require('../build/src/constants/doc-types');
const { Document } = require('../build/src/document');
const { Element } = require('../build/src/element');

const books = [
  {
    lang: 'en',
    category: 'cooking',
    title: 'Everyday Italian',
    author: 'Giada De Laurentiis',
    year: '2005',
    price: '30.00'
  },
  {
    lang: 'en',
    category: 'children',
    title: 'Harry Potter',
    author: 'J K. Rowling',
    year: '2005',
    price: '29.99'
  },
  {
    lang: 'en',
    category: 'web',
    title: 'Learning XML',
    author: 'Erik T. Ray',
    year: '2003',
    price: '39.95'
  }
];

const document = new Document(DocType.XML);
const bookstoreElement = new Element('bookstore');

books.forEach((book) => {
  bookstoreElement
    .addComment('Book info')
    .addChild(
      new Element('book')
        .addAttribute('category', book.category)
        .setChildren([
          new Element('title').addAttribute('lang', book.lang).setText(book.title),
          new Element('author').setText(book.author),
          new Element('year').setText(book.year),
          new Element('price').setText(book.price)
        ])
    );
});

console.log(document.setRootElement(bookstoreElement).toString());

Output

<?xml version="1.0" encoding="UTF-8"?>
<Bookstore>
  <!--Book info-->
  <Book category="cooking">
    <Title lang="en">Everyday Italian</Title>
    <Author>Giada De Laurentiis</Author>
    <Year>2005</Year>
    <Price>30.00</Price>
  </Book>
  <!--Book info-->
  <Book category="children">
    <Title lang="en">Harry Potter</Title>
    <Author>J K. Rowling</Author>
    <Year>2005</Year>
    <Price>29.99</Price>
  </Book>
  <!--Book info-->
  <Book category="web">
    <Title lang="en">Learning XML</Title>
    <Author>Erik T. Ray</Author>
    <Year>2003</Year>
    <Price>39.95</Price>
  </Book>
</Bookstore>

Generating HTML

const { DocType } = require('../build/src/constants/doc-types');
const { Document } = require('../build/src/document');
const { Element } = require('../build/src/element');

const page = {
  title: 'Employees page',
  style: `table, td, th {
      border: 1px solid black;
    } table {
      border-collapse: collapse;width: 100%;
    } th {
      text-align: left;
    }`,
  employees: [
    {
      id: '16002',
      name: 'My Awesomeness',
      salary: '-22',
      age: '19',
      image: 'http://my-site/images/image.png'
    }
    // ...
  ]
};

const document = new Document(DocType.HTML);
const htmlElement = new Element('html').setAttribute('lang', 'en');

// HTML Head.

htmlElement.addChild(
  new Element('head').setChildren([
    new Element('title').setText(page.title),
    new Element('style').setText(page.style)
  ])
);

// Employees Table.

const tableElement = new Element('table').addChild(
  new Element('tr').setChildren([
    new Element('th').setText('img'),
    new Element('th').setText('id'),
    new Element('th').setText('name'),
    new Element('th').setText('age'),
    new Element('th').setText('salary')
  ])
);

// Table Content.

page.employees.forEach((employee) => {
  tableElement.addChild(
    new Element('tr').setChildren([
      new Element('th').addChild(
        new Element('img')
          .setAttribute('src', employee.image)
          .setAttribute('alt', employee.name)
      ),
      new Element('th').setText(employee.id),
      new Element('th').setText(employee.name),
      new Element('th').setText(employee.age),
      new Element('th').setText(employee.salary)
    ])
  );
});

document.setRootElement(htmlElement.addChild(new Element('body').addChild(tableElement)));

console.log(document.toString({ capitalizeElementNames: false, indentSize: 2 }));

Output

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Employees page</title>
    <style>
      table,
      td,
      th {
        border: 1px solid black;
      }
      table {
        border-collapse: collapse;
        width: 100%;
      }
      th {
        text-align: left;
      }
    </style>
  </head>
  <body>
    <table>
      <tr>
        <th>img</th>
        <th>id</th>
        <th>name</th>
        <th>age</th>
        <th>salary</th>
      </tr>
      <tr>
        <th>
          <img src="http://my-site/images/image.png" alt="My Awesomeness" />
        </th>
        <th>16002</th>
        <th>My Awesomeness</th>
        <th>19</th>
        <th>-22</th>
      </tr>
      <tr>
        <th>
          <img src="http://my-site/images/image.png" alt="9253" />
        </th>
        <th>16003</th>
        <th>9253</th>
        <th>187</th>
        <th>5</th>
      </tr>
      <!-- ... -->
    </table>
  </body>
</html>

State

This processor is ready to use to generate XML documents but not ready yet to parse XML strings.

Contributing

See Contribution Guidelines

0.0.1

5 years ago