aboutsummaryrefslogtreecommitdiff
path: root/test/xmlEntities.js
blob: 5a03520701727992e8b8d821bfac1846a0000130 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env node

const fetch = require('node-fetch')
const xml = require('xml-js')

// RAW value is `"Testing & complex < characters > '.`
// JSON value is "name": "\"Testing & complex < characters > '."
fetch(`https://master.apis.dev.openstreetmap.org/api/0.6/node/4327735719`, {
  headers: {
      'Accept': 'application/json'
  }
})
  .then(res => res.json())
  .then(json => {
    const element = json.elements[0]
    const body = xml.json2xml({
      _declaration: {
        _attributes: {
          version: "1.0",
          encoding: "UTF-8"
        }
      },
      osmChange: {
        _attributes: {
          version: '0.6'
        },
        modify: {
          node: [
            {
              _attributes: {
                id: element.id,
                version: element.version + 1,
                lat: element.lat,
                lon: element.lon
              },
              tag: [
                {
                  _attributes: {
                    k: 'name',
                    v: element.tags.name
                  }
                }
              ]
            }
          ]
        }
      }
    }, Object.assign({
      compact: true,
      attributeValueFn: value => {
        return value.replace(/&quot;/g, '"')  // convert quote back before converting amp
          .replace(/&/g, '&amp;')
          .replace(/</g, '&lt;')
          .replace(/>/g, '&gt;')
          .replace(/"/g, '&quot;')
      }
    }, { spaces: 2 }))
    console.log(body)
    // result should match view-source:https://master.apis.dev.openstreetmap.org/api/0.6/node/4327735719
    // <tag k="name" v="&quot;Testing &amp; complex &lt; characters &gt; '."/>
  })