1

I am trying to read xml file using fs.I am not able to read file using fs.readFileSync after passing path variable as first paramter to this function. Note:this is in windows machine

xmlFile="C:\Users\xyz\AppData\Local\.proxySettings.xml";
function myFunc(xmlFile) {
 let xmlData = fs.readFile(xmlFile);
 alert(xmlData);//doesn't print anything

 parser = new DOMParser();
 xmlDoc = parser.parseFromString(xmlData,"text/xml");
....
....
}

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <proxy_port>2582</proxy_port>


update in the file

 try{
    ffile="jdkdkj";
  fs.readFileSync(ffile); 
}catch(err){
  app.console.log(err);
}

Error { errno: -4058, syscall: 'open', code: 'ENOENT', path: 'jdkdkj' }
9
  • Please post the relevant section of code so we can help you Commented Jun 24, 2019 at 21:37
  • Hi Mark, I have edited the question Commented Jun 24, 2019 at 21:43
  • Your code shows let xmlData = fs.readFile(xmlFile); which will never work as fs.readFile() is asynchronous and requires a callback as the second argument to communicate back the result. Also, your backslashes in the path have to be double escaped. Commented Jun 24, 2019 at 22:09
  • I tried with readFileSync Commented Jun 25, 2019 at 9:40
  • let io="C:\\cygwin64\\xyz\\tmp.txt"; var data=fs.readFileSync(io); alert(data); //doesn't show anything Commented Jun 25, 2019 at 9:41

1 Answer 1

1

In JavaScript the backslash character is used to signal that the character immediately following it should be treated specially. In order to create a literal backslash inside a JavaScript string you need to escape the backslash with another backslash.

var a = "\abc";
console.log(a); // abc
var b = "\\abc";
console.log(b); // \abc
var c = "\"abc\"";
console.log(c); // "abc"

xmlFile="C:\\Users\\xyz\\AppData\\Local\\.proxySettings.xml";
console.log(xmlFile); // C:\Users\xyz\AppData\Local\.proxySettings.xml

So, windows pathnames always need double backslashes in JavaScript

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

6 Comments

I tried this xmlFile = xmlFile.replace(/\\/g,'\\\\'); but still it doesn't print file contents
You should be able to do xmlFile="C:\\Users\\xyz\\AppData\\Local\\.proxySettings.xml"; let xmlData = fs.readFile(xmlFile); Is xmlFile assigned a pathname in your code as a string literal? Or is it read from a database?
Then you don't need the replace(), using double backslashes should work. Let us know how it goes
It didn't read the file when I passed a path varaiable as first parmater to readFile or readFileSync
When I do fs.readFileSync("file-that-doesnt-exist") I get an exception that looks like this: { Error: ENOENT: no such file or directory, open 'foo' at Object.openSync (fs.js:443:3) at Object.readFileSync (fs.js:343:35) errno: -2, syscall: 'open', code: 'ENOENT', path: 'foo' } Are you seeing something like that?
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.