Skip to content

Commit

Permalink
test: Add executable example for NodeJs
Browse files Browse the repository at this point in the history
  • Loading branch information
karfau committed Sep 4, 2021
1 parent 53563a1 commit 7a14f94
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 17 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This workflow will run the examples provided in the examples folder
name: examples

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
nodejs:

runs-on: ubuntu-latest

strategy:
matrix:
node-version:
- 10.x
- 12.x
- 14.x
- 16.x

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
working-directory: examples/nodejs
- run: npm run test
working-directory: examples/nodejs
2 changes: 2 additions & 0 deletions examples/nodejs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
package-lock.json
1 change: 1 addition & 0 deletions examples/nodejs/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
19 changes: 19 additions & 0 deletions examples/nodejs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@xmldom/xmldom-example-nodejs",
"private": true,
"description": "Show how to use xmldom in a nodejs project",
"main": "src/index.js",
"scripts": {
"test": "node src/index.js"
},
"keywords": [
"test",
"commonjs",
"nodejs"
],
"author": "",
"license": "MIT",
"dependencies": {
"@xmldom/xmldom": "file:../.."
}
}
15 changes: 15 additions & 0 deletions examples/nodejs/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { DOMParser, XMLSerializer } = require('@xmldom/xmldom')

const source = `<xml xmlns="a">
<child>test</child>
<child/>
</xml>`

const doc = new DOMParser().parseFromString(source, 'text/xml')

const serialized = new XMLSerializer().serializeToString(doc)

if (source !== serialized) {
console.error(`expected\n${source}\nbut was\n${serialized}`)
process.exit(1);
}
29 changes: 12 additions & 17 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,21 @@ This project was forked from it's [original source](https://github.com/jindw/xml
### Example:

[In NodeJS](examples/nodejs/src/index.js)
```javascript
const { DOMParser } = require('@xmldom/xmldom')

const doc = new DOMParser().parseFromString(
'<xml xmlns="a" xmlns:c="./lite">\n' +
'\t<child>test</child>\n' +
'\t<child></child>\n' +
'\t<child/>\n' +
'</xml>',
'text/xml'
)
doc.documentElement.setAttribute('x', 'y')
doc.documentElement.setAttributeNS('./lite', 'c:x', 'y2')
console.info(doc)

const nsAttr = doc.documentElement.getAttributeNS('./lite', 'x')
console.info(nsAttr)
const { DOMParser, XMLSerializer } = require('@xmldom/xmldom')

const source = `<xml xmlns="a">
<child>test</child>
<child/>
</xml>`

const doc = new DOMParser().parseFromString(source, 'text/xml')

const serialized = new XMLSerializer().serializeToString(doc)
```

Note: in Typescript and ES6 you can use the import approach, as follows:
Note: in Typescript ~and ES6~(see #316) you can use the `import` approach, as follows:

```javascript
import { DOMParser } from '@xmldom/xmldom'
Expand Down

0 comments on commit 7a14f94

Please sign in to comment.