tiny.xml 是应用在浏览器里的XML解析器,只有1.6kb大小。
暂时不支持Node.js,如果你想让其支持Node.js,可以参看这里。
安装
npm安装
npm install tiny.xml
直接从CDN获取使用
<!-- Unminified 3.3KB --> <script src="https://cdn.rawgit.com/anvaka/tiny.xml/master/dist/tinyxml.js"></script> <!-- minified 2.6KB --> <script src="https://cdn.rawgit.com/anvaka/tiny.xml/master/dist/tinyxml.min.js"></script>
使用示例
// If you are using a bundler like browserify/webpack. Otherwise it should // be available on the window itself var tinyxml = require('tiny.xml') var xmlString = '<?xml version="1.0" ?>' + '<items xmlns="http://foo.com">' + ' <item>Foo</item>' + ' <item color="green">Bar</item>' + '</items>' var parser = tinyxml(xmlString) var nodes = parser.selectNodes('item') console.log(nodes.length === 2, 'it found two nodes') // Nodes are actual browser elements: https://developer.mozilla.org/en-US/docs/Web/API/Element var firstNode = nodes[0] console.log(firstNode instanceof Element, 'It is instance of Element') // Since we are using standard browser API, powerful Element interface is supported: var root = parser.selectNodes('items')[0] var all = root.querySelectorAll('item') console.log(all.length === 2, 'matches all item elements') var greenNode = root.querySelector('item[color="green"]') console.log(greenNode.textContent === 'Bar', 'picks only one green node'