Page 1 of 1

XML-path to string

Posted: Fri Aug 21, 2020 10:30 am
by bkromer
Hey there,

I use "job.getVariableAsString(' ')" to get a XML-Path as string in my script. But i only get undefined. Also when I try to select that node directly it says that this node is not available altough I can select it. See the pics
Bildschirmfoto 2020-08-21 um 10.09.04.png
Bildschirmfoto 2020-08-21 um 10.09.04.png (34.14 KiB) Viewed 24908 times
Bildschirmfoto 2020-08-21 um 10.24.45.png[/attachment ][attachment=0]Bildschirmfoto 2020-08-21 um 10.06.11.png
My script so far:

Code: Select all

function jobArrived( s : Switch, job : Job )
{
	var xml = job.getVariableAsString( '[Metadata.Text:Path="/lieferung/",Dataset="Xml",Model="XML"]' );
	job.log(1,"xml: "+xml);
	job.sendToSingle( job.getPath() );
}

How can I get the XML-Path "lieferung" as a string in my script?

Re: XML-path to string

Posted: Fri Aug 21, 2020 10:30 am
by bkromer
gosh something is weird with the pics in this forum... :-/
Bildschirmfoto 2020-08-21 um 10.06.11.png
Bildschirmfoto 2020-08-21 um 10.06.11.png (69.79 KiB) Viewed 24906 times
Bildschirmfoto 2020-08-21 um 10.24.45.png
Bildschirmfoto 2020-08-21 um 10.24.45.png (218.24 KiB) Viewed 24905 times

Re: XML-path to string

Posted: Fri Aug 21, 2020 10:40 am
by sander
Escape your " with \ like this

Code: Select all

var xml = job.getVariableAsString("[Metadata.Text:Path=\"/lieferung/\",Dataset=\"Xml\",Model=\"XML\"]");

Re: XML-path to string

Posted: Fri Aug 21, 2020 10:46 am
by bkromer
sander wrote: Fri Aug 21, 2020 10:40 am Escape your " with \ like this

Code: Select all

var xml = job.getVariableAsString("[Metadata.Text:Path=\"/lieferung/\",Dataset=\"Xml\",Model=\"XML\"]");
Flow is not starting then. Also I think I dont need to escape the double quotes because I wrapped them into single quotes... i guess.

Code: Select all

var xml = job.getVariableAsString( '[Metadata.Text:Path="/lieferung/",Dataset="Xml",Model="XML"]' );
Color Highlighting is looking good
Bildschirmfoto 2020-08-21 um 10.47.09.png
Bildschirmfoto 2020-08-21 um 10.47.09.png (46.45 KiB) Viewed 24901 times

Re: XML-path to string

Posted: Fri Aug 21, 2020 11:36 am
by Padawan
bkromer wrote: Fri Aug 21, 2020 10:30 am Bildschirmfoto 2020-08-21 um 10.24.45.png
Your screenshot shows that \Lieverung\ has an attribute and a subnode, but no simple string content. I guess that is why the variable doesn't return anything.

Which return do you want to get?

Re: XML-path to string

Posted: Fri Aug 21, 2020 11:44 am
by bkromer
Padawan wrote: Fri Aug 21, 2020 11:36 am
bkromer wrote: Fri Aug 21, 2020 10:30 am Bildschirmfoto 2020-08-21 um 10.24.45.png
Your screenshot shows that \Lieverung\ has an attribute and a subnode, but no simple string content. I guess that is why the variable doesn't return anything.

Which return do you want to get?
You are right. When I go down to some sub-nodes and select a value it's working.
Well these xml files are heavily nested. "Lieferung" is the main node and I thought I could iterate over the sub elements in my script.
Bildschirmfoto 2020-08-21 um 11.41.25.png
Bildschirmfoto 2020-08-21 um 11.41.25.png (67.94 KiB) Viewed 24896 times

Re: XML-path to string

Posted: Fri Aug 21, 2020 1:23 pm
by freddyp
Your have multiple eintrag_pflegen nodes but they seem to have attributes that identify them, so you could perhaps build the XPath like this:

Code: Select all

//eintrag_anlegen[@nameOfAttribute='value of attribute']
in order to select the one that you need. This can possibly be followed by another node if you have to dig deeper, and if the value of the attribute is not important you can just specify the attribute as well:

Code: Select all

//eintrag_anlegen[@prokom_id]/eintragsversion/nutzung
That is for the job.getVariableAsString method, because that one obviously returns just 1 result.

If you have to iterate over the nodes because you have to use if then to select the correct one, then you have to use the Document class.

Code: Select all

var xml = new Document(pathToXMLFile);
var eintragNodes = xml.evalToNodes("//eintragsaenderungen/eintrag_anlegen");
for (var i=0; i<eintragNodes.length; i++) {
    job.log( 1, eintragNodes.at(i).getBaseName());
}

Re: XML-path to string

Posted: Fri Aug 28, 2020 8:19 am
by bkromer
freddyp wrote: Fri Aug 21, 2020 1:23 pm If you have to iterate over the nodes because you have to use if then to select the correct one, then you have to use the Document class.

Code: Select all

var xml = new Document(pathToXMLFile);
var eintragNodes = xml.evalToNodes("//eintragsaenderungen/eintrag_anlegen");
for (var i=0; i<eintragNodes.length; i++) {
    job.log( 1, eintragNodes.at(i).getBaseName());
}
Thats what I need to do. I need to iterate over all nodes as deep as the tree goes, iterating over all "list" nodes (there are multiple of them).
Then I need to get the values. I thinking of writing a JSON from this.
How can I get an attribute list as a String. Is there a function like stringify for JSON or toString()?

Re: XML-path to string

Posted: Fri Aug 28, 2020 8:47 am
by freddyp

Re: XML-path to string

Posted: Fri Aug 28, 2020 10:17 am
by bkromer

Code: Select all

function jobArrived( s : Switch, job : Job )
{
	var xml = new Document( job.getPath() );
	var eintragPflegenNodes = xml.evalToNodes("//eintragsaenderungen/eintrag_pflegen");
	for (var j=0; j<eintragPflegenNodes.length; j++) {
			job.log( 1,eintragPflegenNodes.at( j ).getBaseName() );
			var attrList= eintragPflegenNodes.at( j ).getAttributes();
			for (var i=0; i<attrList.length; i++){
				job.log(1,""+attrList.at(i).getQualifiedName()+" "+attrList.at(i).getValue() );
			}
	}
	
}
That is how I can get all the attributes under //eintragsaenderungen/eintrag_pflegen with their name and value. Now I need to go deeper and get the underlying nodes and their attribute names and values etc. Would you use getChildNodes() to do this or is there a better way of doing this?

Re: XML-path to string

Posted: Fri Aug 28, 2020 11:46 am
by freddyp
You can use getChildNodes(), but you can also use evalToNodes(xpath). Both return a NodeList object. evalToNodes is more flexible because it is not only capable of getting the direct children but also other nodes further down the tree. It all depends on the XPath, and the XPath depends on the structure of the XML and what you are searching for: https://www.enfocus.com/manuals/Develop ... sions.html.

Re: XML-path to string

Posted: Wed Sep 09, 2020 8:46 am
by bkromer
I am nearly finished with my script. There is only one last issue I am facing.
When I eval a node like
var eintrag_anlegen = xml.evalToNodes("//lieferung/eintragsaenderungen/eintrag_anlegen");
And there is no "eintrag_anlegen" in this particular xml-file, I made a check like
if ( eintrag_anlegen.length > 0)
But I am getting the error
"Trying to access undefined member 'length'"
Checking for it being undefined seems not to work.
Like
if ( eintrag_anlegen !== undefined )
and
if ( eintrag_anlegen !== 'undefined' )
both are "true" altough its undefined. :|
How can I get around this in legacy scripting?

Re: XML-path to string

Posted: Wed Sep 09, 2020 1:07 pm
by Padawan
if you want to check for undefined you have to:

Code: Select all

if (typeof myVariable == "undefined") {
    job.log(3, "myVariable is undefined");
}

Re: XML-path to string

Posted: Fri Sep 11, 2020 4:44 pm
by bkromer
Well in the for..loop it seems to work now. But I have a similar issue now with an object.
I have this object:

Code: Select all

{
		prokom_id: '',
		liefergrund: '',
		eintragsversion: {
			bundesweit: '',
			stichwort: '',
			tb: '',
			vab: '',
			lieferanten_id: '',
			gueltig_von: '',
			gueltig_bis: '',
			widerspruch: '',
			nutzung: {
				auskunft: '',
				emedien: '',
				pmedien: ''
			},
			einzeleintrag: {
				eintragsdaten: {
					eintragsdaten_id: '',
					kz_neu_geaendert: '',
					redak_key: '',
					teilnehmerart: '',
					gd_gt_zuordnung: '',
					person_liste: {},
					adresse: {
						strasse: '',
						hausnr: '',
						hausnr_zusatz: '',
						postfach: '',
						plz: '',
						postort: '',
						gd_gt_nummer: '',
						gemeinde: '',
						gemeindeteil: '',
						kuerzel: '',
						geodaten: {
							wgs84_x: '',
							wgs84_y: '',
							lcc_tao_x: '',
							lcc_tao_y: '',
							stadtteil: '',
							qualitaet: ''
						}
					},
					kommnr_liste: {},
					zusatzdaten_infos: {
						typ: '',
						typ_text: '',
						id_datenlieferant: '',
						ergaenzungstext1_name: '',
						ergaenzungstext2_name: '',
						suchworte: '',
						branding: '',
						oeffnungszeiten_liste: {},
						produkt_freigabe: {
							nutzung_tb: '',
							nutzung_gs: '',
							nutzung_oetb: ''
						},
						emedien_freigabe: {
							web: '',
							mobil: '',
							cd: '',
							tv: ''
						},
						verlags_ids: {
							tb: '',
							oetb: '',
							gs: ''
						},
						info_liste: {},
						link_liste: {},
						bewertung_liste: {}
					}
				}
			}
		}
	} 
In the first part of my script I read in all the lines of the XML and push the information into this blank object. At the end I have an Array of Objects.
Now I go through this Array and gather the informations to make API Calls. Before I do so i wanna check if the specific information is even there.

So in one case

Code: Select all

JSON.stringify(  x[ i ]["eintragsversion"]["einzeleintrag"]["eintragsdaten"]["zusatzdaten_infos"]["produkt_freigabe"] );
gives me this object:

Code: Select all

{"nutzung_gs":"","nutzung_oetb":"","nutzung_tb":""} 

Code: Select all

typeof x[ i ]["eintragsversion"]["einzeleintrag"]["eintragsdaten"]["zusatzdaten_infos"]["produkt_freigabe"] 
gives me "object"

but when I try to get the length

Code: Select all

x[ i ]["eintragsversion"]["einzeleintrag"]["eintragsdaten"]["zusatzdaten_infos"]["produkt_freigabe"].length 
I get the same error Trying to access undefined member 'length'

but when I check for undefined like

Code: Select all

typeof x[ i ]["eintragsversion"]["einzeleintrag"]["eintragsdaten"]["zusatzdaten_infos"]["produkt_freigabe"] == "undefined"
its false

So how can I make this work?

Re: XML-path to string

Posted: Mon Sep 14, 2020 12:36 pm
by Padawan
bkromer wrote: Fri Sep 11, 2020 4:44 pm but when I try to get the length

Code: Select all

x[ i ]["eintragsversion"]["einzeleintrag"]["eintragsdaten"]["zusatzdaten_infos"]["produkt_freigabe"].length 
I get the same error Trying to access undefined member 'length'
This seems normal since the "produkt_freigabe" object is an object (not an array) and it doesn't have a length.
bkromer wrote: Fri Sep 11, 2020 4:44 pm So how can I make this work?
At this point I'm not sure what you are trying to achieve?