• Stars
    star
    129
  • Rank 277,707 (Top 6 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created about 13 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Port of Python's Element Tree module to Node.js

node-elementtree

node-elementtree is a Node.js XML parser and serializer based upon the Python ElementTree v1.3 module.

Installation

$ npm install elementtree

Using the library

For the usage refer to the Python ElementTree library documentation - http://effbot.org/zone/element-index.htm#usage.

Supported XPath expressions in find, findall and findtext methods are listed on http://effbot.org/zone/element-xpath.htm.

Example 1 – Creating An XML Document

This example shows how to build a valid XML document that can be published to Atom Hopper. Atom Hopper is used internally as a bridge from products all the way to collecting revenue, called β€œUsage.” MaaS and other products send similar events to it every time user performs an action on a resource (e.g. creates,updates or deletes). Below is an example of leveraging the API to create a new XML document.

var et = require('elementtree');
var XML = et.XML;
var ElementTree = et.ElementTree;
var element = et.Element;
var subElement = et.SubElement;

var date, root, tenantId, serviceName, eventType, usageId, dataCenter, region,
checks, resourceId, category, startTime, resourceName, etree, xml;

date = new Date();

root = element('entry');
root.set('xmlns', 'http://www.w3.org/2005/Atom');

tenantId = subElement(root, 'TenantId');
tenantId.text = '12345';

serviceName = subElement(root, 'ServiceName');
serviceName.text = 'MaaS';

resourceId = subElement(root, 'ResourceID');
resourceId.text = 'enAAAA';

usageId = subElement(root, 'UsageID');
usageId.text = '550e8400-e29b-41d4-a716-446655440000';

eventType = subElement(root, 'EventType');
eventType.text = 'create';

category = subElement(root, 'category');
category.set('term', 'monitoring.entity.create');

dataCenter = subElement(root, 'DataCenter');
dataCenter.text = 'global';

region = subElement(root, 'Region');
region.text = 'global';

startTime = subElement(root, 'StartTime');
startTime.text = date;

resourceName = subElement(root, 'ResourceName');
resourceName.text = 'entity';

etree = new ElementTree(root);
xml = etree.write({'xml_declaration': false});
console.log(xml);

As you can see, both et.Element and et.SubElement are factory methods which return a new instance of Element and SubElement class, respectively. When you create a new element (tag) you can use set method to set an attribute. To set the tag value, assign a value to the .text attribute.

This example would output a document that looks like this:

<entry xmlns="http://www.w3.org/2005/Atom">
  <TenantId>12345</TenantId>
  <ServiceName>MaaS</ServiceName>
  <ResourceID>enAAAA</ResourceID>
  <UsageID>550e8400-e29b-41d4-a716-446655440000</UsageID>
  <EventType>create</EventType>
  <category term="monitoring.entity.create"/>
  <DataCenter>global</DataCenter>
  <Region>global</Region>
  <StartTime>Sun Apr 29 2012 16:37:32 GMT-0700 (PDT)</StartTime>
  <ResourceName>entity</ResourceName>
</entry>

Example 2 – Parsing An XML Document

This example shows how to parse an XML document and use simple XPath selectors. For demonstration purposes, we will use the XML document located at https://gist.github.com/2554343.

Behind the scenes, node-elementtree uses Isaac’s sax library for parsing XML, but the library has a concept of β€œparsers,” which means it’s pretty simple to add support for a different parser.

var fs = require('fs');

var et = require('elementtree');

var XML = et.XML;
var ElementTree = et.ElementTree;
var element = et.Element;
var subElement = et.SubElement;

var data, etree;

data = fs.readFileSync('document.xml').toString();
etree = et.parse(data);

console.log(etree.findall('./entry/TenantId').length); // 2
console.log(etree.findtext('./entry/ServiceName')); // MaaS
console.log(etree.findall('./entry/category')[0].get('term')); // monitoring.entity.create
console.log(etree.findall('*/category/[@term="monitoring.entity.update"]').length); // 1

Build status

Build Status

License

node-elementtree is distributed under the Apache license.

More Repositories

1

node-cassandra-client

CQL client for Cassandra written in Node.js.
JavaScript
244
star
2

node-swiz

Serialization and Validation Framework for Objects in REST APIs
JavaScript
96
star
3

rackspace-monitoring-agent-plugins-contrib

Contributed Rackspace Monitoring Agent plugins
Python
71
star
4

glimpse.js

A visualization library built on top of d3.
JavaScript
64
star
5

gutsy

internal dashboard bootstrap
JavaScript
34
star
6

docker-ubuntu-with-updates

https://index.docker.io/u/racker/precise-with-updates/
Python
28
star
7

restkin

RESTkin is a REST API for Twitter's Zipkin
Python
24
star
8

rackspace-monitoring

Python client library for Rackspace Cloud Monitoring based on Apache Libcloud.
Python
24
star
9

node-failure-detector

The Phi accrual failure detector.
JavaScript
21
star
10

gorax

A Rackspace Cloud client library written in Go
Go
13
star
11

libtraceroute

BSD traceroute made into a small library
C
12
star
12

perigee

REST client optimized for use with APIs using JSON for request and response bodies, such as OpenStack.
Go
12
star
13

node-rproxy

A reverse proxy for RESTful services
JavaScript
10
star
14

node-keystone-client

Node.js client for the OpenStack Keystone API.
JavaScript
10
star
15

go-proxy-protocol

A Go library to parse the HA Proxy/stud proxy protocol line
Go
10
star
16

node-proxy-protocol

parser for the node-proxy-protocol based on node-strtok
JavaScript
10
star
17

service-registry

API-driven cloud service built for keeping track of your services and storing configuration values in a centralized place.
JavaScript
9
star
18

node-zookeeper-client

A higher-level ZooKeeper client based on node-zookeeper with support for locking and master election.
JavaScript
9
star
19

devopsjson

DevOps JSON defines a well-known URI for DevOps information.
JavaScript
9
star
20

rackspace-monitoring-rb

Ruby bindings to the Rackspace Cloud Monitoring project
Ruby
8
star
21

rackspace-monitoring-gui

Tiny webapp for Rackspace Cloud Monitoring
JavaScript
8
star
22

python-raxcli

Rackspace Open Cloud command line interface
Python
8
star
23

secure-template

Scripts to handle encrypting secrets shared across a team
Python
7
star
24

eom

Emerging OpenStack WSGI Middleware
Python
7
star
25

cloud-init-docker-build

Build cloud-init with docker scripts
Makefile
7
star
26

rackspace-monitoring-poller

Rackspace Monitoring Poller
Go
6
star
27

python-twisted-web

Twisted web
Python
6
star
28

standupbot

An irc bot and simple web page for doing standups remotely
JavaScript
6
star
29

scrivener

Twisted Scribe Client/Server Framework.
Python
6
star
30

torment

A Study in Fixture Based Testing Frameworking
Python
5
star
31

cookbook-cleanup

This cookbook creates a `cleanup` resource to handle the cleanup of arbitrary paths based on number of files or last modified dates.
Ruby
5
star
32

rackspace_cloudmonitoring_migration_tool

Python
5
star
33

python-twisted-core

Twisted core debian packaging
Python
4
star
34

lnup

Scheme
4
star
35

node-rackspace-shared-utils

Shared Node.js utility modules and functions.
JavaScript
4
star
36

glimpse.js-website

The website for glimpse.js
4
star
37

azr-CustomScriptExtensions

This is a public repository which contains a library of Custom Script Extensions created to support Azure.
Shell
3
star
38

node-command-parser

Node.js command line parser.
JavaScript
3
star
39

node-service-registry-client

Node.js client for Rackspace Service Registry.
JavaScript
3
star
40

cloud-init-debian-pkg

Python
3
star
41

salus-docs

Provides overarching documentation for the suite of Salus modules
3
star
42

txzookeeper

Python
3
star
43

salus-telemetry-envoy

Provides the agent manager/relay for Project Salus
Go
3
star
44

rackspace-database

Python
3
star
45

node-moruga

HTTP proxy for API unit-testing and debugging
JavaScript
3
star
46

python-twisted-words

Twisted words
Python
3
star
47

python-service-registry-client

Python client for Rackspace Service Registry.
Python
3
star
48

protobuf-for-node

git fork of protobuf-for-node
C++
3
star
49

node-zk

node.js native port of the zookeeper client and network protocol
JavaScript
3
star
50

python-twisted-keystone-agent

Twisted Python client for Keystone API.
Python
3
star
51

omnibus

Magical chef-server packaging...magic
C
2
star
52

salus-telemetry-bundle

Java
2
star
53

slogger

irc message indexer and user notifier
Python
2
star
54

python-stats

Python
2
star
55

salus-tools

Salus repository for standalone tools that need to be run against multiple different projects
Go
2
star
56

handy

Python
2
star
57

digital-atg

ATG related configurations and source files
Java
2
star
58

java-service-registry-client

Java client for Rackspace Service Registry.
Java
2
star
59

archaius-rsr

Archaius (configuration library) bindings for Rackspace Service Registry
Java
2
star
60

ele-agent-meta-packages

Shell
2
star
61

kumoru-sdk-go

The official golang Kumoru SDK
Go
2
star
62

cloudmonitoring-demo

A demo site for Cloud Monitoring
JavaScript
2
star
63

ko-validation

Knockout Validation Plugin for the Rackspace Control Panel.
JavaScript
2
star
64

salus-event-engine

Consumes metrics, processes matching event tasks, and produce event notifications
Java
1
star
65

salus-common

Java
1
star
66

tf-hcl

Ruby
1
star
67

python-dcs

Dynamic Chef Solo provides an easier to produce the JSON attributes used by Chef Solo.
Python
1
star
68

salus-telemetry-resource-management

Microservice responsible for managing resouce data
Java
1
star
69

salus-telemetry-presence-monitor

Java
1
star
70

ruby-1.9.3-lucid

ruby 1.9.3 for lucid
C
1
star
71

sip4-qt3

C
1
star
72

mriya

Salaesforce data transformation command line tools that built on top of salesforce bulk api. Look into both 'master' and 'mriya_dmt' branches.
Python
1
star
73

rax-jenkins

Fork of autoscale-chef that focuses on Jenkins
Ruby
1
star
74

salus-event-engine-common

Java
1
star
75

libmemcached

libmemcached upstream + debian
Shell
1
star
76

protobuf

C++
1
star
77

node-v1

JavaScript
1
star
78

ruby-chef-gelf

ruby chef-gelf packaged by gem2deb
Ruby
1
star
79

luvlint

Lua
1
star
80

salus-app-base

1
star
81

nodejs-deb-generator

Shell
1
star
82

salus-telemetry-auth-service

Java
1
star
83

managed-cloud-cloudmonitoring

Python
1
star
84

python-twisted-lore

Twisted lore
Python
1
star
85

python-twisted-conch

Twisted conch
Python
1
star
86

keyczar

keyczar + debian packaging
Python
1
star
87

service-registry-docs

Documentation in Markdown format for Rackspace Service Registry.
1
star
88

salus-oracle-agent

Go
1
star
89

python-twisted-news

Twisted news
Python
1
star
90

ruby-gelf

ruby gelf packaged by gem2deb
Ruby
1
star
91

logformatjson

Simple json formatter for standard python logging
Python
1
star
92

node-rackspace-shared-middleware

Shared Node.js Express middleware.
JavaScript
1
star
93

pylibmc

pylibmc debian package
C
1
star
94

salus-telemetry-etcd-adapter

Java
1
star
95

python-twisted-runner

Twisted runner
Python
1
star
96

salus-event-engine-management

Java
1
star
97

python-twisted-names

Twisted names
Python
1
star
98

node-buildbot

nodejs library for interacting with buildbot
JavaScript
1
star
99

salus-telemetry-monitor-management

Java
1
star
100

protobuf-c

Debian package for protobuf-c
Shell
1
star