JScript & Helper to extract xml value

The line is incorrect:

var f = file.ReadFromFile('fileName');

You are referencing 'fileName' as a string, but it should be a variable. So replace it to this:

var f = file.ReadFromFile(filename);

Also note variable names are case sensitive.

Then, check your file contents has xml first, use dlg.ShowMessage at various points, for example verify the path that is used, verify your file contains the xml. It can be difficult to debug in the script editor so doing this can help a lot.

Thanks for your answer, I saw it already but do not solve my problem but thanks!

Almost there
File is loaded, it is a xml file.

it stops at xml.Parse (does not show the dlg.ShowMessage but that error.

imagen

Once I have the object

o

How do I get the DigestValue ?

Trying getElement gives:

imagen

I have not been following it closely because it really hasnt interested me but just my first thought is your making something extremely complicated that could be handled with simple actions and rules in SambaPOS. Instead your trying to script everything…

I could be wrong.

1 Like

SFS = program that takes input and sends output (xml) to the tax administration. That program is provided as it is by tax administration.

SambaPOS sends 3 text files to SFS

–> SFS build XML, hashes it, signs it and sends it to Tax Administration

–> Tax administration replies to SFS with XML (accepted/rejected), SFS stores that XML reply in a directory.

–> I would like that SambaPOS reads that XML in that directory and extracts 2 values from it to update 2 Ticket tags.

So which simple actions and rules would you suggest to handle it?

You got that error because o is not a string. change to dlg.ShowMessage(o.toString());

1 Like

imagen

The o object does not accept a lot of methods (nor toSting, nor Get Element, …) … problem is I have no clue on which methods it would accept.

Sorry, I misread your script, o.toString() will not work as o is an XML object, not a string. The error message you posted is a generic one when you try to pass a non string to dlg.ShowMessage().

Try

var s = o.getElementByTagName('ds:DigestValue');

That may return an object, so I think you can try one of these:

s.nodeValue
s[0].nodeValue
s[0].childNodes[0].nodeValue

I am just guessing as it is pretty impossible to do without testing a script myself.

Also have a look here, this is where I got the ideas for above:

https://www.w3schools.com/xml/met_element_getelementsbytagname.asp

1 Like

I tried it but getElement is also no accepted method:

imagen

That works I get the object o, but don’t know to get the value.

imagen

It all points to the fact that may not be XML data there. Did you test the file name and that you have XML returned first? If you don’t do that, there is no point debugging further until you confirm your text from the file is actually loaded into f.

Ok so I was wrong. That is why it’s important to explain what your doing so we see the bigger picture and give you better answers.

Confirmation, it reads the file and it is xml:

Confirmation : return f (file)


xml.Parse is ok also, it’s just afterwards : How to read the values …


Progressing …


Continuing the discussion from :v: V5 Feature Compilation:

In the exemple of emre it is clear …
However in my xml file, I have a lot of:

<ext:xxxxx ...

and using the double-dot : is obviously not working, nor with the ’ caracter :grinning:

 var o = xml.Parse(f);
  
 var r = o.Invoice.'ext:UBLExtensions'.'ext:UBLExtension'.'ext:ExtensionContent'.'ds:Signature'.'ds:SignedInfo'.'ds:Reference'.'ds:DigestValue'.Value;

so …

begining of xml file is:

It’s a bit more confusing than:

var carColor = car.color.Value

I try without success:

o.Invoice.'ext:UBLExtensions'.'ext:UBLExtension'.'ext:ExtensionContent'.'ds:Signature'.'ds:SignedInfo'.'ds:Reference'.'ds:DigestValue'.Value;

o.Invoice.ext:UBLExtensions.ext:UBLExtension.ext:ExtensionContent.ds:Signature.ds:SignedInfo.ds:Reference.ds:DigestValue.Value;

How do I deal with that?


Upd: Not working neighter

o.Invoice.UBLExtensions.UBLExtension.ExtensionContent.Signature.SignedInfo.Reference.DigestValue.Value;

o.Invoice.ext.Extensions.UBLExtension.ExtensionContent.ds.Signature.SignedInfo.Reference.DigestValue.Value;

o.Invoice.Extensions.UBLExtension.ExtensionContent.Signature.SignedInfo.Reference.DigestValue.Value;

See when you don’t know what you are doing, it can take a lot of time …

Yes or you pay someone to do it faster.

To read some string in a text?
cat filename | grep DigestValue | sed -e 's/^..ds:DigestValue\>//' | sed -e 's/\<DigestValue//'
and that’s done.
however if you don’t know how works the sambaPOS helper xml.Parse, you only can try …
There is no information on internet about sambaPOS helper xml.Parse

Actually I try with exemple of emre adding a ext:car in the xml string and cannot get an extract of car.color.value … so I think, I’ll not be able to do it that way.

You probably need to escape the colon.

Surely parsing returns an array the same as parsing json, if parsing is working but calling value is causing issues surely it has been converted to array without issue, unless I’m missing something.

Well, xml parsing is a bit opaque for me, so I used the strings.

function getXMLVal(settingname) {

	var filename = getSettingVal('SFS_Path_gen')+"PARSE\\"+makeSFSFilename()+".xml";

	var f = file.ReadFromFile(filename);

	var r = findWord(f, 'ds:DigestValue');

  return r;
}


//  :)      TextToLook, WordToFind   :)   
function findWord(ttl,wtf) {

	var i, b, e = 0;
	var r = "";
	
	var vwtf = wtf+">";
	i = ttl.indexOf(vwtf);
	b = i + vwtf.length;
	
	var vwtf = "</"+wtf;
	i = ttl.indexOf(vwtf);
	e = i;
	
	var m = dlg.ShowMessage("b = "+b+"   e = "+e+"    i = "+i+"    vwtf : "+vwtf);
	
    for (i = b; i < e; i++) { 
    	r = r + ttl[i];
    	}
 
    return r;
}

And I have my DigestValue (or any other) from an xml file.

xml textfile contains:

<ds:DigestValue>ZZrU7/ViT1/XyPE/OkXDMwpulKs=</ds:DigestValue>

and findWord will extract the value between:

  • ds:DigestValue>
    and
  • </ds:DigestValue

It’s might not be nice or full error proof, but that’s enough for my use.

This isn’t really about the xml.Parse() helper at all. The function reads XML and returns an Object. That’s all it does. There is no other functionality to it.

var carColor = car.color.Value

That ^ “dot” notation is the JScript syntax used to read a value from an Object. Another way is:

var carColor = car["color"]["Value"]

Not sure what to say about the colons (:), but you will get errors because the JScript engine doesn’t understand what to do with them, but maybe try this:

var dv= o["Invoice"]["ext:UBLExtensions"]
1 Like