1

I'm having trouble figuring out how to work with xml-js. When I run the code, I see the output. But when I try to get a value from a particular key, it give me either a undefined or cannot read property of undefined error. I added my code below. What am I doing wrong?

var convert = require('xml-js');
var xml =
    '<?xml version="1.0" encoding="utf-8"?>' +
    '<note importance="high" logged="true">' +
    '    <title>Happy</title>' +
    '    <todo>Work</todo>' +
    '    <todo>Play</todo>' +
    '</note>';
var result2 = convert.xml2json(xml, {compact: true, spaces: 4});
console.log(result2);

Let's say I want the value for title which is 'happy'. I tried doing it this way but it didn't work... result2.title; even tried result2._text; but nothing works. Any help would be appreciated. Thanks!!

6
  • Haven't worked with xml-js, but logically, wouldn't your title element be result.note.title? As an aside, this chunk of code isn't even close to an minimal reproducible example Commented May 28, 2018 at 22:24
  • That chunk of code is more than a Minimal, Complete, and Verifiable example, not only It was perfectly clear, but allowed me to provide an answer. @Tibrogargan Commented May 28, 2018 at 22:28
  • @MarcosCasagrande I beg to differ. "it didn't work" is not at all verifiable Commented May 28, 2018 at 22:34
  • That snippet is clear enough to understand the problem, he's using the wrong method, and even using the right one, he's accessing the properties incorrectly. If you don't know the answer, it doesn't mean the question is not good. You just don't know the answer. Commented May 28, 2018 at 22:35
  • @MarcosCasagrande yes, the problem is easy enough to understand, but that does not make it an MCVE. He does not even include the line of code that "does not work", let alone describe the expected results and what he's actually seeing. It devalues the question. Commented May 28, 2018 at 22:47

1 Answer 1

1

<title> is inside <note>, so in order to access it, you should do:

result2.note.title

But your main problem is that you're using .xml2json method instead of .xml2js which will yield a JSON string instead of an object.

From the docs:

To convert XML text to JavaScript object, use xml2js(). To convert XML text to JSON text, use xml2json().

var convert = require('xml-js');
var xml =
    '<?xml version="1.0" encoding="utf-8"?>' +
    '<note importance="high" logged="true">' +
    '    <title>Happy</title>' +
    '    <todo>Work</todo>' +
    '    <todo>Play</todo>' +
    '</note>';
var result2 = convert.xml2js(xml, {compact: true});
console.log(result2.note.title); // { _text: 'Happy' }
console.log(result2.note.title._text); // 'Happy'

The spaces option is ownly applicable to xml2json, so you don't need it.

When using .xml2json you were trying to access the title or note property of a string, which of course doesn't exists.

''.title; // undefined
''.note; // undefined
''.note.title // TypeError cannot read property 'title' of undefined

NOTE: If you're using npm 1.6 it means you're using a no longer supported version of Node.js, you should update to v8.x.x (LTS) or to the latest v10.x.x

Sign up to request clarification or add additional context in comments.

6 Comments

You're welcome! Read my note about updating node! you should do it!
Yes, I will definitely read about updating node. I'm a beginner to it, so I will in order to get better at it. Lol
Which version of node are you using?
I am using v8.11.1.
Well, your node version is perfect, it's weird you only have NPM v1.6 since NPM should be 5, for node 8.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.