1

I am currently trying to parse an XML through meteor with xml2js and insert it into Mongodb. I do this in server/fixtures.js with the following code:

Iati.insert({
    test: xml2js.parseString(Assets.getText('iati.xml'))
})

Xml2js does seem to convert something but the output seems nonsensical namely an object with teh following attributes:

test: Object
ENTITIES: Object
attribList: Array[0]
attribName: ""
attribValue: ""
bufferCheckPosition: 65536
c: ""
cdata: ""
closed: false
closedRoot: false
column: 0
comment: ""
doctype: ""
entity: ""
error: null
line: 0
looseCase: "toUpperCase"
noscript: true
opt: Object
position: 0
procInstBody: ""
procInstName: ""
q: ""
sawRoot: false
script: ""
sgmlDecl: ""
startTagPosition: 9347
state: 0
strict: true
tag: null
tagName: ""
tags: Array[0]
textNode: ""
trackPosition: true
__proto__: Object
__proto__: Object

How should I change my code to get the actual XML as output? Thank you in advance

Stennie
  • 63,885
  • 14
  • 149
  • 175
Jaspermid
  • 461
  • 3
  • 14

1 Answers1

1

According to the documentation, you need to pass a callback xml2js.parseString to get the actual value:

xml2js.parseString(Assets.getText('iati.xml'), function(err, res) {
  Iati.insert({
    test: res,
  });
});
Hubert OG
  • 19,314
  • 7
  • 45
  • 73
  • the callback might need to run in a fiber. Something like `Meteor.bindEnvironment` or `Meteor._runAsync` or a manual `Fibers(function() {...}).run()` would be needed since the insert is meteor code – Tarang Jul 03 '14 at 08:22
  • @hubertOG the example code in your answers gives the exact same result. – Jaspermid Jul 03 '14 at 08:26
  • @Akshat what are you exactly suggesting :)> – Jaspermid Jul 03 '14 at 08:26
  • @Akshat: good call, I forgot we're on the server side. Still, if the problem was with Fibers, it would raise an exception rather than change the result, right? – Hubert OG Jul 03 '14 at 11:56
  • @Jaspermid: upon closer inspection, are you sure that the results you got are not the one you should be getting? The output looks like a XML node definition. – Hubert OG Jul 03 '14 at 12:00
  • @HubertOG it would, was slightly off topic, I think Jaspermid's issue is not related to this specifically, it looks like the issue is more related to the input xml – Tarang Jul 03 '14 at 12:02
  • @HubertOG The input XML looks has several fields such as Dutch NGO's looking to implement IATI. I Can't seem to find any of these fields in the object. So something is getting read (which is a nice step forwards for me :) ), but I can't seem to get into the actual data I need. Complete xml can be found at http://aidstream.org/files/xml/partos-activities.xml – Jaspermid Jul 03 '14 at 15:07
  • Is the string you're parsing correct? It looks like an empty document was parsed. Do `console.log(Assets.getText('iati.xml'))` just to be sure. Also, try inspecting those `Entities` object, what's inside there? – Hubert OG Jul 03 '14 at 21:31
  • @HubertOG Entities object consist of bogus data as well: AElig: "Æ" Aacute: "Á" Acirc: "Â" Agrave: "À" and so on. Console.log on client side gives me an error ReferenceError: Assets is not defined. Can;t figure out how to do it server side (nothing gets logged :) ). Still a beginner :(. – Jaspermid Jul 05 '14 at 10:51
  • @HubertOG Console.log gives a string of the XML file so that does seem to work. xml2js just doesnt parse it and.or feeds it into the mongodb correctly – Jaspermid Jul 05 '14 at 14:52
  • Strange thing. Perhaps it will be easier with a different library? See [this answer](http://stackoverflow.com/a/14890740/876409) for example. – Hubert OG Jul 05 '14 at 20:39