Page 1 of 1

replacechild

Posted: Mon Nov 06, 2017 9:38 pm
by rschipper
Hello,

I´m having a problem replacing a child value in my incoming xml.
I got the appending to work but now i have a doubble node in the xml.
Can somebody help me to change the append to replace (or remove existing node (DeliveryDate). The problem begins at "var = text2"

The script maybe a bit strange but i'm learning :? . every tip is welcome.

Thanks..

Code: Select all



// Is invoked each time a new job arrives in one of the input folders for the flow element.
// The newly arrived job is passed as the second parameter.
function jobArrived( s : Switch, job : Job )
{
	var dataset = s.getPropertyValue('dataset', job);
   	var xpath = s.getPropertyValue('xpath', job);
	
	var orderaantal = s.getPropertyValue('orderaantal');
	var order2 = orderaantal * 1;
	var order3 = (order2 - 1);
	var reset = s.getPropertyValue('reset');
	var num = 1;	
	var num2 = 0;
	
	var databaseDataset = job.getDataset(dataset);
  
   
    var theXMLDoc = new Document(databaseDataset.getPath());   
    var nodeByPath = theXMLDoc.evalToNodes("/Klantorders/Klantorder");
	
	
	var theGlobalDataScope = "/be/gradual/www/" + s.getScriptName();
	
	const theUniqueIDKey = "LabelWithUniqueID_UniqueID";
		
	s.lockGlobalData( theGlobalDataScope );
	
	var theUniqueID = s.getGlobalData( theGlobalDataScope, theUniqueIDKey );
	var unid = theUniqueID;
	var nummer = order3;
	var totaal = "";
	
	if (reset == "reset"){
			theUniqueID = "0";}
	else {
		
		if (unid < order3) {	   
			var theUniqueIDNumber = parseInt(theUniqueID) + 1;
			theUniqueID = theUniqueIDNumber.toString();
			nummer = num2.toString();
			totaal = ((theUniqueID * 1) + (nummer * 1));
			} 
		
		else if (unid == order3) {	   
			nummer = (order2);
			theUniqueID = num2.toString();
			totaal = ((theUniqueID * 1) + (nummer * 1));
		}
		
		else {
			theUniqueID = num.toString();
			 } 
		   }

			   
			   
			   // Save the new Unique ID value
	s.setGlobalData( theGlobalDataScope, theUniqueIDKey, theUniqueID );
	
	// Don't forget to unlock global data again
	s.unlockGlobalData();
	
    var subt = (((order2 + 1) - totaal)); 
	
	
	
const mSInDay = 600000;

	var milliSecsInDay = ((mSInDay * subt) + 518400000)

	var tmwMilliSecs = new Date().getTime() + milliSecsInDay;

	var tmw = new Date(tmwMilliSecs);

	tmw.toString();
	
	job.setPrivateData('tijd', tmw);

	var text2 = theXMLDoc.createText(tmw);
	
	
	var Node = theXMLDoc.createElement("DeliveryDate")
   	
	Node.appendChild(text2);			 
	//replace all of node with new text
	for(var i=0; i<nodeByPath.length; i++){
		nodeByPath.at(i).appendChild(Node);
		
	}     
  
   
// Save the file
   theXMLDoc.save(databaseDataset.getPath());
  
	
   // And send the job to the next output
      job.sendToSingle(job.getPath(), job.getName());

}


function timerFired( s : Switch )
{
}

Re: replacechild

Posted: Mon Nov 06, 2017 9:57 pm
by gabrielp
Maybe you should give the replaceChild method a try

http://www.enfocus.com/manuals/Develope ... class.html
replaceChild( newChild : Node, oldChild : Node ) : Node
Replaces the specified child of this element with another node and returns the removed node.

Re: replacechild

Posted: Tue Nov 07, 2017 1:07 pm
by rschipper
Hello,

I played around with the replaceChild but i only got it to work for the first node.

Code: Select all


var text2 = theXMLDoc.createText(tmw);
	
	
	var Node = theXMLDoc.createElement("DeliveryDate")
   	
	Node.appendChild(text2);			 
	//replace all of node with new text
	for(var i=0; i<nodeByPath.length; i++){
		nodeByPath.at(i).replaceChild(Node, nodeByPath);
		
	}     
  
How can I replace all DeliveryDate nodes in my xml.

thanks,

Robert

Re: replacechild

Posted: Tue Nov 07, 2017 3:32 pm
by gabrielp
Could you post your XML file (or its structure)?

Re: replacechild

Posted: Tue Nov 07, 2017 3:42 pm
by rschipper
Hi Gabriel,

here's my xml.

Code: Select all

<?xml version="1.0" encoding="utf-8" ?> 
- <Klantorders>
- <Klantorder>
  <ContactID>002</ContactID> 
  <CustomerID>101891</CustomerID> 
  <CustomerOrderNumber>107_3341</CustomerOrderNumber> 
  <CustomerProduct>Kerkblad Titus Brandsma parochie</CustomerProduct> 
  <CustomerProductName>48 pag selfcover 17x24</CustomerProductName> 
  <DeliveryContactLanguage>1</DeliveryContactLanguage> 
  <DeliveryLocationName>Titus Brandsma parochie</DeliveryLocationName> 
  <DeliveryContactTitle>1</DeliveryContactTitle> 
  <DeliveryDate>2017-10-23T:21:30:00</DeliveryDate> 
  <DeliveryContactFirstName>Marcel</DeliveryContactFirstName> 
  <DeliveryContactLastName>Holl</DeliveryContactLastName> 
  <DeliveryLocationStreet>Smedenstraat 248</DeliveryLocationStreet> 
  <DeliveryLocationCity>7411 RC</DeliveryLocationCity> 
  <DeliveryLocationCity2>Deventer</DeliveryLocationCity2> 
  <DeliveryLocationCountry>NL</DeliveryLocationCountry> 
  <DeliveryComment>Modellen per week verzamelen</DeliveryComment> 
  <InvoiceCustomerID>101891</InvoiceCustomerID> 
  <InvoiceContactID>001</InvoiceContactID> 
  <OrderDate>2017-10-17T12:36:39</OrderDate> 
  <ProductID /> 
  <Quantity>2</Quantity> 
  <UnitPrice /> 
  <UrlName>https://portal.parochiemagazin.nl/pdffiles/107_3341.pdf</UrlName> 
  </Klantorder>
- <Klantorder>
  <ContactID>002</ContactID> 
  <CustomerID>101891</CustomerID> 
  <CustomerOrderNumber>107_3341</CustomerOrderNumber> 
  <CustomerProduct>Kerkblad - Titus Brandsma parochie</CustomerProduct> 
  <CustomerProductName>48 pag - POST seal</CustomerProductName> 
  <DeliveryContactLanguage>1</DeliveryContactLanguage> 
  <DeliveryDate>2017-10-23T:21:30:00</DeliveryDate> 
  <DeliveryLocationID>101870</DeliveryLocationID> 
  <DeliveryComment>sealen met adresdrager</DeliveryComment> 
  <InvoiceCustomerID>101891</InvoiceCustomerID> 
  <InvoiceContactID>003</InvoiceContactID> 
  <OrderDate>2017-10-17T12:36:39</OrderDate> 
  <ProductID /> 
  <Quantity>1446</Quantity> 
  <UnitPrice /> 
  <UrlName /> 
  </Klantorder>
- <Klantorder>
  <ContactID>002</ContactID> 
  <CustomerID>101891</CustomerID> 
  <CustomerOrderNumber>107_3341</CustomerOrderNumber> 
  <CustomerProduct>Kerkblad - Titus Brandsma parochie</CustomerProduct> 
  <CustomerProductName>48 pag - DHL doos</CustomerProductName> 
  <DeliveryContactLanguage>1</DeliveryContactLanguage> 
  <DeliveryDate>2017-10-23T:12:00:00</DeliveryDate> 
  <DeliveryLocationID>101871</DeliveryLocationID> 
  <DeliveryComment>DHL etiket uit systeem klant DOOS</DeliveryComment> 
  <InvoiceCustomerID>101891</InvoiceCustomerID> 
  <InvoiceContactID>003</InvoiceContactID> 
  <OrderDate>2017-10-17T12:36:39</OrderDate> 
  <ProductID /> 
  <Quantity>124</Quantity> 
  <UnitPrice /> 
  </Klantorder>
  </Klantorders>

Re: replacechild

Posted: Tue Nov 07, 2017 4:53 pm
by gabrielp
It looks like that would work, but your code is a little confusing.

I would examine the behavior of your for loop. Print some log messages there and discover if you are looping correctly through each klantorder. replaceChild returns the node that was removed, print that to the log and compare what is being replaced each time to the node you're looping through.

I usually use this helper function when I need to do for loops: viewtopic.php?f=13&t=1452&p=5408&hilit=foreach#p5408

Re: replacechild

Posted: Tue Nov 07, 2017 6:02 pm
by cstevens
I think this line is incorrect as well:

Code: Select all

nodeByPath.at(i).replaceChild(Node, nodeByPath);
nodeByPath appears to be a NodeList, and the second parameter in the replaceChild function is expecting a Node. I think it needs to look something like this:

Code: Select all

nodeByPath.at(i).replaceChild(Node, nodeByPath.at(i).evalToNode("./DeliveryDate"));

Re: replacechild

Posted: Wed Nov 08, 2017 11:42 am
by rschipper
Hi all,

I also think it has to be a problem in the second parameter of the replaceChild.

When I use: nodeByPath.at(i).replaceChild(Node, nodeByPath.at(i).evalToNode("./DeliveryDate")); It replaces the <DeliveryDate> in the the last part (3) of the xml but only remove it from the first and second part.

Im still playing around with it.

Re: replacechild

Posted: Wed Nov 08, 2017 7:17 pm
by cstevens
I'm not sure about his, but I think this is because you're using the same node to replace each of the 3 existing nodes, which effectively is moving the node to the last replacement you do.

I think if you move the Node definition element inside your for loop then it will create a new node each time through the loop and it should replace each element.

Like this:

Code: Select all

   //replace all of node with new text
   for(var i=0; i<nodeByPath.length; i++){
     var deliveryNode = theXMLDoc.createElement("DeliveryDate");
     var text2 = theXMLDoc.createText(tmw);
     deliveryNode.appendChild(text2); 
     nodeByPath.at(i).replaceChild(deliveryNode, nodeByPath.at(i).evalToNode("./DeliveryDate"));
   }
Also, this is more of a stylistic thing, but it's a little confusing when you name a variable the same as a DataType/Object. I changed your "Node" to "deliveryNode".

I didn't test this, so I'm not sure that's valid code or not.

Re: replacechild

Posted: Wed Nov 08, 2017 9:04 pm
by rschipper
It's a working *&*/#$%@ :D
Thank you very much. Learned a lot from this approach.
Also thanks to Gabrielp your debug tool learned me a lot.