Gathering detailed insights and metrics for xmldoc
Gathering detailed insights and metrics for xmldoc
Gathering detailed insights and metrics for xmldoc
Gathering detailed insights and metrics for xmldoc
npm install xmldoc
99.8
Supply Chain
99.5
Quality
75.9
Maintenance
100
Vulnerability
99.6
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
271 Stars
108 Commits
55 Forks
13 Watching
3 Branches
12 Contributors
Updated on 10 Nov 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-5.3%
283,929
Compared to previous day
Last week
2.3%
1,601,821
Compared to previous week
Last month
5.1%
6,689,676
Compared to previous month
Last year
4.9%
74,461,911
Compared to previous year
xmldoc
lets you parse XML documents with ease. It's a pure-JavaScript, one-file XML document class with a single dependency on the excellent sax
parser.
For more on why I wrote this class, see the blog post.
See CHANGELOG.md for details (built with GitHub Changelog Generator).
npm install xmldoc
Or just download the repository and include it in your node_modules
directly. Or just download the single JS file!
I haven't tested this myself but installing buffer
and stream
separately may be necessary for xmldoc
to work on React Native:
npm install buffer stream xmldoc
1var xmldoc = require('xmldoc'); 2 3var document = new xmldoc.XmlDocument("<some>xml</some>"); 4 5// do things
The primary exported class is XmlDocument
, which you'll use to consume your XML text. XmlDocument
contains a hierarchy of XmlElement
instances representing the XML structure.
Both XmlElement
and XmlDocument
contain the same members and methods you can call to traverse the document or a subtree.
name
- the node name, like "tat" for <tat>
. XML "namespaces" are ignored by the underlying sax-js parser, so you'll simply get "office:body" for <office:body>
.attr
- an object dict containing attribute properties, like bookNode.attr.title
for <book title="...">
.val
- the string "value" of the node, if any, like "world" for <hello>world</hello>
.children
- an array of XmlElement
children of the node.firstChild
, lastChild
- pretty much what it sounds like; null if no childrenline
, column
, position
, startTagPosition
- information about the element's original position in the XML string.Each member defaults to a sensible "empty" value like {}
for attr
, []
for children
, and ""
for val
.
All methods with child
in the name operate only on direct children; they do not do a deep/recursive search.
It's important to note that xmldoc
is designed for when you know exactly what you want from your XML file. For instance, it's great for parsing API responses with known structures, but it's not great at teasing things out of HTML documents from the web.
If you need to do lots of searching through your XML document, I highly recommend trying a different library like node-elementtree.
Similar to underscore's each
method, it will call func(child, index, array)
for each child of the given node.
Pass it the name of a child node and it will search for and return the first one found, or undefined
.
Like childNamed
but returns all matching children in an array, or []
.
Searches for the first child with the given attribute value. You can omit value
to just find the first node with the given attribute defined at all.
Searches for a specific "path" using dot notation. Example:
1<book> 2 <author> 3 <name isProper="true">George R. R. Martin</name> 4 ... 5 </author> 6 ... 7</book>
If you just want the <name>
node and you have the XmlElement
for the <book>
node, you can say:
1var nameNode = bookNode.descendantWithPath("author.name"); // return <name> node
Just like descendantWithPath
, but goes deeper and extracts the val
of the node. Example:
1var authorName = bookNode.valueWithPath("author.name"); // return "George R. R. Martin"
You can also use the @
character to request the value of a particular attribute instead:
1var authorIsProper = bookNode.valueWithPath("author.name@isProper"); // return "true"
This is not XPath! It's just a thing I made up, OK?
This is just an override of the standard JavaScript method, it will give you a string representation of your XML document or element. Note that this is for debugging only! It is not guaranteed to always output valid XML.
The default implementation of toString()
, that is, the one you get when you just console.log("Doc: " + myDoc)
will pretty-print the XML with linebreaks and indents. You can pass a couple options to control the output:
1xml.toString({compressed:true}) // strips indents and linebreaks
2xml.toString({trimmed:true}) // trims long strings for easier debugging
3xml.toString({preserveWhitespace:true}) // prevents whitespace being removed from around element values
Putting it all together:
1var xml = "<author><name>looooooong value</name></author>";
2console.log("My document: \n" + new XmlDocument(xml).toString({trimmed:true}))
Prints:
My Document:
<hello>
loooooooo…
</hello>
Feel free to file issues or hit me up on Twitter.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 8/18 approved changesets -- score normalized to 4
Reason
7 existing vulnerabilities detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More