xmldom icon indicating copy to clipboard operation
xmldom copied to clipboard

Cannot get code working

Open Delagen opened this issue 11 years ago • 6 comments
trafficstars

The simple string is falled to exception var string="123" console.log((new DOMParser()).parseFromString(string, "text/xml"));

error "end tag name: root is not match the current start tagName:undefined"

Delagen avatar Apr 24 '14 11:04 Delagen

The string 123, is not actually valid XML though.

But, I have the same problem here for actual valid XML:

var doc = parser.parseFromString('xml', 'text/xml'); element parse error: TypeError: parseStack is undefined @#[line:0,col:1]end tag name: my is not match the current start tagName:undefined @#[line:1,col:2]============================================================

(This is loading the modules in either SpiderMonkey or Rhino, not in node.js - I have no idea how to load the current head in node.js).

Is there a mailing list for this project?

mbd-dbc-dk avatar May 13 '14 10:05 mbd-dbc-dk

More information about my troubles

This script:

load('dom.js'); load('dom-parser.js'); load('sax.js');

var doc = new DOMParser({ errorHandler:function(key,msg){ console.error(key, msg); } }).parseFromString('xml', 'text/xml');

Returns this under Rhino:

error element parse error: TypeError: Cannot read property "length" from undefined @#[line:undefined,col:undefined]fatalError end tag name: some is not match the current start tagName:undefined @#[line:undefined,col:undefined]Cannot read property "tagName" from undefined

And this under SpiderMonkey

error element parse error: TypeError: parseStack is undefined @#[line:undefined,col:undefined]fatalError end tag name: some is not match the current start tagName:undefined @#[line:undefined,col:undefined]============================================================ with this error stack: Script engine error. Place: 'Object::callFunction()' ErrorReport: 13.16: TypeError: current is undefined Error in 'dom-parser.js:113'

Stack: ((void 0),(void 0),"some")@dom-parser.js:113 parse("xml",[object Object],[object Object],[object Object],[object Object])@sax.js:107 ("xml",[object Object],[object Object])@sax.js:31 ("xml","text/xml")@dom-parser.js:25 test1()@dbc.js:22 @(interactive input):0

It would seem, that for some reason "parseStack" is not correctly set up.

Any clues from anyone?

mbd-dbc-dk avatar May 13 '14 10:05 mbd-dbc-dk

OK, I digged a bit further into it. It turns out, that you need to encapsulate each file when loading them. I was led by the conditional support of node.js (based on the presence of a require function) to think that you could just load them, and use them.

I think that is not actually possibly in the current form: both the file sax.js and the file dom-parser.js declares a global method called appendElement. No matter which order you load them in, one will overwrite the other, and you will get problems.

The error we see here, is because the sax file is loaded last. When its "appendElement" function is called, it will call stuff on the domBuilder, and the domBuilder will try to call its appendElement function, which takes two elements, but will hit the appendElement of the sax.js file, which requires three elements. The last argument is the parseStack, and it will be undefined, and the error comes from that.

I have added some information about how to fix this problem to the wiki, here: https://github.com/jindw/xmldom/wiki/How-to-use-xmldom-in-non-node.js-JavaScript-platforms-like-Rhino-or-SpiderMonkey

mbd-dbc-dk avatar May 13 '14 14:05 mbd-dbc-dk

The workaround doesn't work in Nashorn.

I replaced loadObject with loadWithNewGlobal in the XMLDOM function, but then the dpObj will not load correctly,

var dpObj = loadWithNewGlobal(basePath + "/xmldom/dom-parser.js");
var dp_exports = {};
print('dpObj ' + dpObj + ', dp_exports ' + dp_exports);

prints

dpObj undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined, dp_exports [object Object]

but the dp_exports object seems to be empty.

madjic avatar Apr 01 '15 13:04 madjic

problems in running node.js

var doc = new dom().parseFromString(xml) [xmldom error] element parse error: Error: invalid tagName: @#[line:1,col:3] [xmldom error] element parse error: Error: invalid tagName: @#[line:13,col:2] [xmldom error] element parse error: Error: invalid tagName: @#[line:15,col:2] [xmldom error] element parse error: Error: invalid tagName: @#[line:17,col:2] [xmldom error] element parse error: Error: invalid tagName:

...

the xml string is from an html file generated from an email

thanks for the help. Love and peace, Joew

JoeCodeswell avatar Oct 25 '17 19:10 JoeCodeswell

@JoeCodeswell make sure that you have the file in the correct encoding (Most likely UTF-8)

sergey-martynov avatar Jan 12 '18 18:01 sergey-martynov