1

I am trying to create a custom json by extracting data with xml2parsing. So far I have this:

function createCustomJson(d{ 

    let dataFromXml = ""; 
    parseString(d, {trim: true}, function (err, result) {
        dataFromXml = JSON.stringify(result);
    });
    let dataJson = { data: [] };
    let dataObj = JSON.parse(dataFromXml);
    let dataForJson = dataObj.dataset.data[0];
    let metadataForJson = dataObj.dataset.metadata[0];         

    let pom = {};
    for (var i = 0; i < dataForJson.row.length; i++) {
        for (var j = 0; j < dataForJson.row[0].value.length; j++) {

            pom["METADATA-ATTR-NAME"] = dataForJson.row[i].value[j]; 
        }
        dataJson.data.push(pom); 
    } 

    let json = JSON.stringify(dataJson);
} 

xml:

<?xml version="1.0" encoding="utf-8"?>
<dataset  xmlns="http://example.com"  xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
    <metadata>
          <item name="DATA_1" type="xs:string" length="2102"/>
          <item name="DATA_2" type="xs:string" length="24"/>
    </metadata>
    <data>
        <row>
            <value>active</value>
            <value>whatever</value>
        </row>
    </data>
</dataset>

I was able to extract all the values from "data", but don't know how to get the metadata item name (DATA_1 and DATA_2).

In console.log, for

metadataForJson.item[0]

I get

{ '$': { name: 'DATA_1', type: 'xs:string', length:'2102'} }

Don't know how to get value of '$' out, it always gives me sintax error. Any ideas? Thanx!

akrelj
  • 151
  • 4
  • 18
  • What's your method to "get value of '$' out"? What kind of syntax error do you get? Can you provide more information – shaochuancs Sep 30 '16 at 13:22

1 Answers1

2

just try

metadataForJson.item[0].$.name

OR

metadataForJson.item[0]['$'].name

This related post may help https://stackoverflow.com/a/22028956/730733

Community
  • 1
  • 1
DavidKahnt
  • 452
  • 1
  • 8
  • 13
  • Wow... I was pretty shure I already tried the first option, but I checked again just to be shure, and it works. – akrelj Oct 03 '16 at 06:21