xmldom icon indicating copy to clipboard operation
xmldom copied to clipboard

Error: source.substring is not a function

Open shinux opened this issue 9 years ago • 7 comments

Happened in file sax.js with appendText function.

shinux avatar Jan 27 '16 09:01 shinux

same here...

error: TypeError: source.substring is not a function
    at appendText (E:\Source\jt6-server.unstable\node_modules\xmldom\sax.js:62:20)
    at parse (E:\Source\jt6-server.unstable\node_modules\xmldom\sax.js:170:4)
    at Object.XMLReader.parse (E:\Source\jt6-server.unstable\node_modules\xmldom\sax.js:30:3)
    at DOMParser.parseFromString (E:\Source\jt6-server.unstable\node_modules\xmldom\dom-parser.js:26:7)

MeTaNoV avatar Feb 04 '16 12:02 MeTaNoV

@shnode check the type of your input, it should be a string, in my case, I just discovered that fs.readFile return a buffer which is of type object...

MeTaNoV avatar Feb 04 '16 15:02 MeTaNoV

@MeTaNoV Thanks a lot, after read your feedback, i went to check those arguments' type, but they are string indeed...

shinux avatar Feb 05 '16 02:02 shinux

please put your source code of xml content

jindw avatar Feb 15 '16 15:02 jindw

I have the same problem.

My code is

const xpath = require('xpath');
const dom = require('xmldom').DOMParser

const fs = require("fs");
const xmlContents = fs.readFileSync("gtk-stack.glade");

const doc = new dom().parseFromString(xmlContents);
const nodes = xpath.select("//object", doc);
console.log(nodes[0].localName + ": " + nodes[0].firstChild.data);
console.log("node: " + nodes[0].toString());

The contents of gtk-stack.glade file

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow" id="window">
    <property name="can_focus">False</property>
    <property name="default_width">300</property>
    <property name="default_height">200</property>
    <signal name="destroy" handler="on_window_destroy" swapped="no"/>
    <child>
      <placeholder/>
    </child>
    <child>
      <object class="GtkBox">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkScrolledWindow">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="hscrollbar_policy">never</property>
            <property name="shadow_type">in</property>
            <child>
              <object class="GtkViewport">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="margin_left">10</property>
                <property name="margin_right">10</property>
                <property name="margin_top">10</property>
                <property name="margin_bottom">10</property>
                <property name="shadow_type">none</property>
                <child>
                  <object class="GtkListBox" id="listboxContent">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                  </object>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkGrid">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="margin_left">10</property>
            <property name="margin_right">10</property>
            <property name="margin_top">10</property>
            <property name="margin_bottom">10</property>
            <property name="column_spacing">15</property>
            <property name="column_homogeneous">True</property>
            <child>
              <object class="GtkButton" id="buttonPush">
                <property name="label" translatable="yes">Push</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <signal name="clicked" handler="on_buttonPush_clicked" swapped="no"/>
              </object>
              <packing>
                <property name="left_attach">0</property>
                <property name="top_attach">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton" id="buttonPop">
                <property name="label" translatable="yes">Pop</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <signal name="clicked" handler="on_buttonPop_clicked" swapped="no"/>
              </object>
              <packing>
                <property name="left_attach">1</property>
                <property name="top_attach">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton" id="buttonExit">
                <property name="label" translatable="yes">Exit</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <signal name="clicked" handler="on_buttonExit_clicked" swapped="no"/>
              </object>
              <packing>
                <property name="left_attach">2</property>
                <property name="top_attach">0</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

Then it gave me the following error message.

[xmldom error]	element parse error: TypeError: source.charAt is not a function 
dom-parser.js:179
@#[line:0,col:undefined]
TypeError: source.substring is not a function
sax.js:62
    at appendText (c:\Users\Liu.D.H\Desktop\gtk-stack\glade-ui-parse\node_modules\xmldom\sax.js:62:20)
    at parse (c:\Users\Liu.D.H\Desktop\gtk-stack\glade-ui-parse\node_modules\xmldom\sax.js:199:4)
    at XMLReader.parse (c:\Users\Liu.D.H\Desktop\gtk-stack\glade-ui-parse\node_modules\xmldom\sax.js:30:3)
    at DOMParser.parseFromString (c:\Users\Liu.D.H\Desktop\gtk-stack\glade-ui-parse\node_modules\xmldom\dom-parser.js:26:7)
    at Object.<anonymous> (c:\Users\Liu.D.H\Desktop\gtk-stack\glade-ui-parse\index.js:17:23)
    at Module._compile (internal/modules/cjs/loader.js:675:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10)
    at Module.load (internal/modules/cjs/loader.js:589:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:528:12)
    at Function.Module._load (internal/modules/cjs/loader.js:520:3)

liudonghua123 avatar May 24 '18 07:05 liudonghua123

@MeTaNoV @shinux I find the problem is fs.readFileSync of fs.readFile return buffer not string, so I must invoke toString(), So I changed from

const doc = new dom().parseFromString(xmlContents);

to

const doc = new dom().parseFromString(xmlContents.toString());

Then it worked as expected.

liudonghua123 avatar May 24 '18 07:05 liudonghua123

Thank you for your help.

@MeTaNoV @shinux I find the problem is fs.readFileSync of fs.readFile return buffer not string, so I must invoke toString(), So I changed from

const doc = new dom().parseFromString(xmlContents);

to

const doc = new dom().parseFromString(xmlContents.toString());

Then it worked as expected.

maratnek avatar Oct 17 '18 13:10 maratnek