Code Monkey home page Code Monkey logo

Comments (10)

euberdeveloper avatar euberdeveloper commented on May 29, 2024 42

It is not completely right: it does not works for not string values

if I have this xml:

<person>
    <age>18</age>
    <name>Mario</name>
    <diplomated>false</diplomated>
<person>

and this options:

var options = {
    compact: true,
    trim: true,
    nativeType: true,
    ignoreDeclaration: true,
    ignoreInstruction: true,
    ignoreAttributes: true,
    ignoreComment: true,
    ignoreCdata: true,
    ignoreDoctype: true,
    textFn: removeJsonTextAttribute
  };

with nativeType set at true values like "true" or "12" will be true and 12, but in the lib this happens before the calling of textFn, wich is made by the external module sax. So textFn will manage only elements with string value if nativeType is set at true.

The result would be:

{
 person: {
  age: { _text: 18; },
  name: "mario",
  diplomated: { _text: false; }
 }
}

if native type would have been false the result would be:

{
 person: {
  age: "18",
  name: "mario",
  diplomated: "false"
 }
}

THE SOLUTION
if you want both nativeType: true and _text removed I found this solution:

function nativeType(value) {
    var nValue = Number(value);
    if (!isNaN(nValue)) {
      return nValue;
    }
    var bValue = value.toLowerCase();
    if (bValue === 'true') {
      return true;
    } else if (bValue === 'false') {
      return false;
    }
    return value;
  }

var removeJsonTextAttribute = function(value, parentElement) {
    try {
      var keyNo = Object.keys(parentElement._parent).length;
      var keyName = Object.keys(parentElement._parent)[keyNo - 1];
      parentElement._parent[keyName] = nativeType(value);
    } catch (e) {}
  }

var options = {
    compact: true,
    trim: true,
    ignoreDeclaration: true,
    ignoreInstruction: true,
    ignoreAttributes: true,
    ignoreComment: true,
    ignoreCdata: true,
    ignoreDoctype: true,
    textFn: removeJsonTextAttribute
  };

//Then use the xml2js function or xml2json function

explained: I copied the function that convert string in its native type from the lib, I copied the remove text function from gpender but I added the nativeType function at the value. I removed the nativeType: true option. So, because nativeType is false, the not string types won't be ignored by the textFn functions and _text will be removed for them too. The textFn function will convert the string to its native type so the result is:

{
 person: {
  age: 18,
  name: "mario",
  diplomated: false
 }
}

from xml-js.

gpender avatar gpender commented on May 29, 2024 25

Try adding a textFn callback

convert.xml2json(xmlMsg,{textFn:RemoveJsonTextAttribute});

function RemoveJsonTextAttribute(value,parentElement){
try{
var keyNo = Object.keys(parentElement._parent).length;
var keyName = Object.keys(parentElement._parent)[keyNo-1];
parentElement._parent[keyName] = value;
}
catch(e){}
}

from xml-js.

egoarka avatar egoarka commented on May 29, 2024 15

@mickadoua

this snippet solve this problem

const removeJsonTextAttribute = function(value, parentElement) {
  try {
    const parentOfParent = parentElement._parent;
    const pOpKeys = Object.keys(parentElement._parent);
    const keyNo = pOpKeys.length;
    const keyName = pOpKeys[keyNo - 1];
    const arrOfKey = parentElement._parent[keyName];
    const arrOfKeyLen = arrOfKey.length;
    if (arrOfKeyLen > 0) {
      const arr = arrOfKey;
      const arrIndex = arrOfKey.length - 1;
      arr[arrIndex] = value;
    } else {
      parentElement._parent[keyName] = value;
    }
  } catch (e) {}
};

from xml-js.

JamesCoyle avatar JamesCoyle commented on May 29, 2024 3

Is there a way to get this to completely remove tags like <example/> where there is never anything in the object? I don't care about reproducing the XML as I'm just converting a third party XML API into JSON so it is easier to work with. The API sends empty tags when there is no information for that datapoint but that leaves my JSON full of those keys like example: {} but I'd rather have example: null or just not have it at all.

from xml-js.

moldypenguins avatar moldypenguins commented on May 29, 2024 2

Yes, removeJsonTextAttribute function does not work when nativeType: true.
Is there any solution?

Set nativeType in options to false and let the nativeType function handle converting the values

var options = {
  compact: true,
  nativeType: false,
  textFn: removeJsonTextAttribute
};

const nativeType = function(value) {
  let nValue = Number(value);
  if (!isNaN(nValue)) {
    return nValue;
  }
  let bValue = value.toLowerCase();
  if (bValue === 'true') {
    return true; 
  } else if (bValue === 'false') {
    return false;
  }
  return value;
}

const removeJsonTextAttribute = function(value, parentElement) {
  try {
    const parentOfParent = parentElement._parent;
    const pOpKeys = Object.keys(parentElement._parent);
    const keyNo = pOpKeys.length;
    const keyName = pOpKeys[keyNo - 1];
    const arrOfKey = parentElement._parent[keyName];
    const arrOfKeyLen = arrOfKey.length;
    if (arrOfKeyLen > 0) {
      const arr = arrOfKey;
      const arrIndex = arrOfKey.length - 1;
      arr[arrIndex] = value;
    } else {
      parentElement._parent[keyName] = nativeType(value);
    }
  } catch (e) {}
};

from xml-js.

JCLedesmaDev avatar JCLedesmaDev commented on May 29, 2024 2

Is there a way to get this to completely remove tags like <example/> where there is never anything in the object? I don't care about reproducing the XML as I'm just converting a third party XML API into JSON so it is easier to work with. The API sends empty tags when there is no information for that datapoint but that leaves my JSON full of those keys like example: {} but I'd rather have example: null or just not have it at all.


I found the solution to solve the 'example: {} ' object problem, by directly modifying the library.
For this, I went to node_modules, "xml-js" > lib > xmljs.js and on line 286, there is the "onEndElement" method. Here, add the following code:

if (typeof currentElement[name] === 'object' && Object.keys(currentElement[name]).length === 0) {
currentElement[name] = options.defaultValue

I define that when a property comes with an empty object, I change the value of said property.
In order to dynamically define the value to change to the options that are passed to them, I add a property called 'defaultValue' and pass the value in question

codigo
options
node_modules

from xml-js.

gpender avatar gpender commented on May 29, 2024

Good call!

from xml-js.

mickadoua avatar mickadoua commented on May 29, 2024

hello, some issues with this method,
<Dossier><Objectif>obj1</Objectif><Objectif>obj2</Objectif><Objectif>obj3</Objectif></Dossier>
will return
{Objectif: 'obj3' }
i think parentElement._parent[keyName] = nativeType(value) erase the last value

from xml-js.

paul-uz avatar paul-uz commented on May 29, 2024

@mickadoua

this snippet solve this problem

const removeJsonTextAttribute = function(value, parentElement) {
  try {
    const parentOfParent = parentElement._parent;
    const pOpKeys = Object.keys(parentElement._parent);
    const keyNo = pOpKeys.length;
    const keyName = pOpKeys[keyNo - 1];
    const arrOfKey = parentElement._parent[keyName];
    const arrOfKeyLen = arrOfKey.length;
    if (arrOfKeyLen > 0) {
      const arr = arrOfKey;
      const arrIndex = arrOfKey.length - 1;
      arr[arrIndex] = value;
    } else {
      parentElement._parent[keyName] = value;
    }
  } catch (e) {}
};

This does not work in combination with nativeType: true

from xml-js.

quyendqmz avatar quyendqmz commented on May 29, 2024

Yes, removeJsonTextAttribute function does not work when nativeType: true.
Is there any solution?

from xml-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.