elementtree
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -1,3 +0,8 @@ | ||
elementtree v0.1.5 (in development) | ||
elementtree v0.1.6 (in development) | ||
* Add support for CData elements. (#14) | ||
[hermannpencole] | ||
elementtree v0.1.5 - 2012-11-14 | ||
* Fix a bug in the find() and findtext() method which could manifest itself | ||
@@ -7,3 +12,3 @@ under some conditions. | ||
elementtree v0.1.4 | ||
elementtree v0.1.4 - 2012-10-15 | ||
@@ -13,3 +18,3 @@ * Allow user to use namespaced attributes when using find* functions. | ||
elementtree v0.1.3 | ||
elementtree v0.1.3 - 2012-09-21 | ||
@@ -21,3 +26,3 @@ * Improve the output of text content in the tags (strip unnecessary line break | ||
elementtree v0.1.2 | ||
elementtree v0.1.2 - 2012-09-04 | ||
@@ -24,0 +29,0 @@ * Allow user to pass 'indent' option to ElementTree.write method. If this |
@@ -106,3 +106,3 @@ /** | ||
Element.prototype.remove = function(index, element) | ||
Element.prototype.remove = function(element) | ||
{ | ||
@@ -223,2 +223,10 @@ this._children = this._children.filter(function(e) { | ||
function CData(text) { | ||
var element = new Element(CData); | ||
if (text) { | ||
element.text = text; | ||
} | ||
return element; | ||
} | ||
function ProcessingInstruction(target, text) | ||
@@ -446,3 +454,3 @@ { | ||
} | ||
else if (tag !== null && tag !== Comment && tag !== ProcessingInstruction) { | ||
else if (tag !== null && tag !== Comment && tag !== CData && tag !== ProcessingInstruction) { | ||
throw new Error('Invalid tag type for serialization: '+ tag); | ||
@@ -489,2 +497,6 @@ } | ||
} | ||
else if (tag === CData) { | ||
text = text || ''; | ||
write(sprintf("<![CDATA[%s]]>", text)); | ||
} | ||
else { | ||
@@ -586,2 +598,3 @@ tag = qnames[tag]; | ||
exports.Comment = Comment; | ||
exports.CData = CData; | ||
exports.ProcessingInstruction = ProcessingInstruction; | ||
@@ -588,0 +601,0 @@ exports.SubElement = SubElement; |
@@ -9,3 +9,3 @@ { | ||
"description": "XML Serialization and Parsing module based on Python's ElementTree.", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"keywords": [ "xml", "sax", "parser", "seralization", "elementtree" ], | ||
@@ -31,3 +31,3 @@ "homepage": "https://github.com/racker/node-elementtree", | ||
"devDependencies": { | ||
"whiskey": "0.6.8" | ||
"whiskey": "0.8.x" | ||
}, | ||
@@ -34,0 +34,0 @@ "licenses": [ |
114
README.md
@@ -18,2 +18,116 @@ node-elementtree | ||
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. | ||
```javascript | ||
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: | ||
```xml | ||
<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. | ||
```javascript | ||
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 | ||
@@ -20,0 +134,0 @@ ==================== |
@@ -300,1 +300,41 @@ /** | ||
}; | ||
exports['test_remove'] = function(test, assert) { | ||
var root = Element('root'); | ||
var node1 = SubElement(root, 'node1'); | ||
var node2 = SubElement(root, 'node2'); | ||
var node3 = SubElement(root, 'node3'); | ||
assert.equal(root.len(), 3); | ||
root.remove(node2); | ||
assert.equal(root.len(), 2); | ||
assert.equal(root.getItem(0).tag, 'node1') | ||
assert.equal(root.getItem(1).tag, 'node3') | ||
test.finish(); | ||
}; | ||
exports['test_cdata_write'] = function(test, assert) { | ||
var root, etree, xml, values, value, i; | ||
values = [ | ||
'if(0>1) then true;', | ||
'<test1>ponies hello</test1>', | ||
'' | ||
]; | ||
for (i = 0; i < values.length; i++) { | ||
value = values[i]; | ||
root = Element('root'); | ||
root.append(et.CData(value)); | ||
etree = new ElementTree(root); | ||
xml = etree.write({'xml_declaration': false}); | ||
assert.equal(xml, sprintf('<root><![CDATA[%s]]></root>', value)); | ||
} | ||
test.finish(); | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58562
1354
142
1