mime-content
v0.0.9
Published
A simple mime-type based string parser
Downloads
520
Maintainers
Keywords
Readme
mime-content
A node.js module for parsing strings based on MIME Type.
Usage
This module supports parsing strings for a small handful of common MIME Types. Pull requests for adding additional types are welcome.
HTML
After parsing a HTML string, CSS selectors can be used to query the parsed content. The cheerio module is used to query the DOM. Selectors supported by JQuery can be used.
Use the toString()
function to retrieve the un-parsed content.
var content = require('mime-content');
var html = content('<html><body><h1 class="big">Header!</h1></body></html>', 'text/html');
html('h1.big').text();
=> 'Header!'
JSON
After parsing a JSON string, a regular object is returned. Use the toString()
function on that object to retrieve
the un-parsed content.
var content = require('mime-content');
var json = content('{"foo":"bar"}', 'application/json');
json.foo
=> 'bar'
XML
Both the application/xml and text/xml MIME Types are supported.
After parsing a XML string, the xmldom document is available. Also, XPath support is provided by the xpath module. All DOM 3 XPath expressions are supported.
By default, the XML parser assumes your XPath expression is looking for a single DOM node so it returns the first match
in the document. If you'd like all matching nodes instead, use the second optional boolean parameter with your query. Pass
true
to return all matching nodes.
var content = require('mime-content');
var xml = content('<people><person id="123"><name>Bob Smith</name></person><person id="456"><name>Jimmy Dean</name></person></people>', 'text/xml');
xml.xpath('/people/person/name/text()').data;
=> 'Bob Smith'
xml.xpath('/people/person/name/text()', true).map(function(text) {
return text.data;
});
=> ['Bob Smith', 'Jimmy Dean']
xml.xpath('/people/person/@id', true).map(function(attr) {
return attr.value;
});
=> ['123', '456']
The toObject()
function uses xml2js to return an object representation
of the XML. This function accepts an options object that is passed directly to the xml2js Parser. See the xml2js Readme for
the supported options.
var xml = content('<people><person id="123"><name>Bob Smith</name></person><person id="456"><name>Jimmy Dean</name></person></people>', 'text/xml');
xml.toObject({explicitArray: false, explicitRoot: false, mergeAttrs: true});
=>
{
person: [
{ id: '123', name: 'Bob Smith' },
{ id: '456', name: 'Jimmy Dean' }
]
}
URL Encoding
After parsing a application/x-www-form-urlencoded
string, a regular object is returned.
var content = require('mime-content');
var qs = content('foo=bar&foo=baz&baz=bip', 'application/x-www-form-urlencoded');
qs.foo
=> ['bar', 'baz']
qs.baz
=> 'bip'