Home > Blockchain >  empty xml value in node.js with xml-reader and map children
empty xml value in node.js with xml-reader and map children

Time:10-31

I'm not able to get value of parameters like aldScanningInterval = 30 sec

Here is the sample_file.xml:

<raml><cmData>
<managedObject  distName="MRBTS-Template/EQM-1/APEQM-1" version="EQM21A_2012_002" operation="create">
  <p name="aldScanningInterval">30 sec</p>
  <p name="berMajorAlarmThreshold">-12</p>
  <p name="berMinorAlarmThreshold">-13</p>
</managedObject>

And this is the code:

const XmlReader = require('xml-reader');
const xml = fs.readFileSync("./publish/DATA/A2G/templates/sample_file.xml", "utf8");

const xmlr = XmlReader.parseSync(xml);

const xmlQuery = require('xml-query');

xmlQuery(xmlr).children().children().map(node => console.log(node.attributes.distName   "\n Params:\n  "   node.children.map(child => child.attributes.name   "="   child.value   "\n")));

What I get in console is:

Okt 31 13:30:54 S5-VPN a2gc[2835315]: MRBTS-Template/EQM-1/APEQM-1
Okt 31 13:30:54 S5-VPN a2gc[2835315]:  Params:
Okt 31 13:30:54 S5-VPN a2gc[2835315]:   aldScanningInterval=
Okt 31 13:30:54 S5-VPN a2gc[2835315]: ,berMajorAlarmThreshold=
Okt 31 13:30:54 S5-VPN a2gc[2835315]: ,berMinorAlarmThreshold=

The value is not coming. Why? I tried also with .text and becomes undefined.

This is what comes from:

xmlQuery(xmlr).children().children().map(node => console.log(node.children));

Okt 31 13:41:47 S5-VPN a2gc[2838984]: [ { name: 'p',
Okt 31 13:41:47 S5-VPN a2gc[2838984]:     type: 'element',
Okt 31 13:41:47 S5-VPN a2gc[2838984]:     value: '',
Okt 31 13:41:47 S5-VPN a2gc[2838984]:     parent:
Okt 31 13:41:47 S5-VPN a2gc[2838984]:      { name: 'managedObject',
Okt 31 13:41:47 S5-VPN a2gc[2838984]:        type: 'element',
Okt 31 13:41:47 S5-VPN a2gc[2838984]:        value: '',
Okt 31 13:41:47 S5-VPN a2gc[2838984]:        parent: [Object],
Okt 31 13:41:47 S5-VPN a2gc[2838984]:        attributes: [Object],
Okt 31 13:41:47 S5-VPN a2gc[2838984]:        children: [Circular] },
Okt 31 13:41:47 S5-VPN a2gc[2838984]:     attributes: { name:    'aldScanningInterval' },
Okt 31 13:41:47 S5-VPN a2gc[2838984]:     children: [ [Object] ] },

Thank you in advance.

CodePudding user response:

The library is considering values as child elements again, try using child.children[0].value if you're sure the value is available in the format you've mentioned:

import XmlReader = require('xml-reader');
import xmlQuery = require('xml-query');

const xmlr = XmlReader.parseSync(`<raml><cmData>
  <managedObject  distName="MRBTS-Template/EQM-1/APEQM-1" version="EQM21A_2012_002" operation="create">
    <p name="aldScanningInterval">30 sec</p>
    <p name="berMajorAlarmThreshold">-12</p>
    <p name="berMinorAlarmThreshold">-13</p>
  </managedObject></cmData></raml>`);
xmlQuery(xmlr)
  .children()
  .children()
  .map((node) =>
    console.log(
      node.attributes.distName  
        '\n Params:\n  '  
        node.children.map(
          (child) =>
            child.attributes.name   '='   child.children[0].value   '\n'
        )
    )
  );

You could also go a level deeper using .children() again to get the value, observe the change in how variables are accessed:

import XmlReader = require('xml-reader');
import xmlQuery = require('xml-query');

const xmlr = XmlReader.parseSync(`<raml><cmData>
  <managedObject  distName="MRBTS-Template/EQM-1/APEQM-1" version="EQM21A_2012_002" operation="create">
    <p name="aldScanningInterval">30 sec</p>
    <p name="berMajorAlarmThreshold">-12</p>
    <p name="berMinorAlarmThreshold">-13</p>
  </managedObject></cmData></raml>`);
xmlQuery(xmlr)
  .children()
  .children()
  .children()
  .map((node) =>
    console.log(
      node.parent.attributes.distName  
        '\n Params:\n  '  
        node.children.map(
          (child) => child.parent.attributes.name   '='   child.value   '\n'
        )
    )
  );

CodePudding user response:

solved with xmlQuery(child).text()

  • Related