gianci
12th October 2015, 20:05
Hi all,
I am trying to write a new session that should be able to generate an XML file, with specific format. I know Baan/LN developing tools but for me this is the first time I use XML statements, therefore I need some help.

How can I write a part of the file that looks like this :

(.....)
<PrototypeData Status="Initial">
<DataRecord>
<PartNumberCustomer>AAABBBCCC</PartNumberCustomer>
<ReferenceID CodingScheme="ISO-IEC">1111122222</ReferenceID>
<ReferenceID CodingScheme="ISO-IEC">1111122223</ReferenceID>
<CompanyIdNumber Agency="DUNS">22344</CompanyIdNumber>
<DataRecordTimeStamp>2015-09-30T11:00:36.873985+02:00</DataRecordTimeStamp>
<SupplierName>AAABBBCCCDDD</SupplierName>
<PartNumberSupplier></PartNumberSupplier>
<Comment></Comment>
</DataRecord>
- <PartInformationRecord>
<PartDescription>Part ABC</PartDescription>
<NetWeight UoM="KGM">0.05</NetWeight>
<PartWeightMethod>Calculated</PartWeightMethod>
<Comment></Comment>
<AdditionalInformation />
</PartInformationRecord>
</PrototypeData>

I was able to make the session write most of the file parts, but I have problems with the statements like "PrototypeData" or "ReferenceID.." that include attributes, and with PartWeightMethod that look indented in respect to NetWeight.
Can anybody help me with some suggestions?

Thanks
Gianni

bhushanchanda
13th October 2015, 08:33
Hi,

Its always better to post your code and what you have tried and at which places you are facing issues. It helps to answer the questions by trying out the same code.

Though there may be other and better optimal solutions, here is one way to make it -

function main()
{
long node,node1,node2,node3,i,node4

i = seq.open("test123.txt","w")
node = xmlNewNode("PrototypeData")
xmlsetAttribute(node, "Status", "Initial")
node1 = xmlNewNode("DataRecord", XML_ELEMENT,node)
xmlNewDataElement("PartNumberCustomer", "AAABBBCCC" ,node1)
node2 = xmlNewDataElement("ReferenceID", "1111122222" ,node1)
xmlsetAttribute(node2,"CodingScheme","ISO-IEC")
node2 = xmlNewDataElement("ReferenceID", "1111122223" ,node1)
xmlsetAttribute(node2,"CodingScheme","ISO-IEC")
node2 = xmlNewDataElement("CompanyIdNumber", "22344" ,node1)
xmlsetAttribute(node2,"Agency","DUNS")
node2 = xmlNewDataElement("DataRecordTimeStamp", "2015-09-30T11:00:36.873985+02:00" ,node1)
node2 = xmlNewDataElement("SupplierName", "AAABBBCCCDDD" ,node1)
node2 = xmlNewDataElement("PartNumberSupplier", "" ,node1)
node2 = xmlNewDataElement("Comment", "" ,node1)

node3 = xmlNewNode("PartInformationRecord", XML_ELEMENT,node)
node4 = xmlNewDataElement("PartDescription", "Part ABC" ,node3)
node4 = xmlNewDataElement("NetWeight", "0.05" ,node3)
xmlsetAttribute(node4,"UoM","KGM")
node4 = xmlNewDataElement("PartWeightMethod", "Calculated" ,node3)
node4 = xmlNewDataElement("Comment", "" ,node3)
node4 = xmlNewNode("AdditionalInformation",XML_ELEMENT,node3)

xmlWrite(i,node)
seq.close(i)
}

Attached is the XML output file for this code.

gianci
13th October 2015, 13:30
Hi Bhushan
thanks a lot for the tips.
I had more or less got to that point, and I understand now that the main issue for me could be the handling of end of lines.
If I use the "xmlWrite" statement then the file is written on a single line,
while if I use "xmlWritePretty" then the file looks quite ok. The only issue is that in this case the attributes are not aligned with the node, but listed in the lines that follow.
I attached a file to show the result of the process.

I hope that this should not be a problem of the xml interpreter. I have to send the file to a customer with data related to each delivery, I will send him a sample and see if he has some complains.

I don't know if you have further suggestions to format the file in a different way, or some other comments.
Thanks again
Gianni

bhushanchanda
13th October 2015, 14:42
Yes, that's the standard way how LN creates the XML to make it more human readable. Not sure if there is other way, but there is a simple trick to get it corrected. Now, you can build a generic logic or use the same as below, depends on you.

Instead of using XMLWritePretty(), create a string using xmlWritePrettyToString() and write it to a string. Now, the trick is to simply replace a series of special characters with a space and you are good to go!

Following is the code -

function main()
{
long node,node1,node2,node3,i,node4
string str(1500)
string output(1) based
long p

i = seq.open("test123.txt","w")
node = xmlNewNode("PrototypeData",XML_ELEMENT)
xmlsetAttribute(node, "Status", "Initial")
node1 = xmlNewNode("DataRecord", XML_ELEMENT,node)
xmlNewDataElement("PartNumberCustomer", "AAABBBCCC" ,node1)
node2 = xmlNewDataElement("ReferenceID", "1111122222" ,node1)
xmlsetAttribute(node2,"CodingScheme","ISO-IEC")
node2 = xmlNewDataElement("ReferenceID", "1111122223" ,node1)
xmlsetAttribute(node2,"CodingScheme","ISO-IEC")
node2 = xmlNewDataElement("CompanyIdNumber", "22344" ,node1)
xmlsetAttribute(node2,"Agency","DUNS")
node2 = xmlNewDataElement("DataRecordTimeStamp", "2015-09-30T11:00:36.873985+02:00" ,node1)
node2 = xmlNewDataElement("SupplierName", "AAABBBCCCDDD" ,node1)
node2 = xmlNewDataElement("PartNumberSupplier", "" ,node1)
node2 = xmlNewDataElement("Comment", "" ,node1)

node3 = xmlNewNode("PartInformationRecord", XML_ELEMENT,node)
node4 = xmlNewDataElement("PartDescription", "Part ABC" ,node3)
node4 = xmlNewDataElement("NetWeight", "0.05" ,node3)
xmlsetAttribute(node4,"UoM","KGM")
node4 = xmlNewDataElement("PartWeightMethod", "Calculated" ,node3)
node4 = xmlNewDataElement("Comment", "" ,node3)
node4 = xmlNewNode("AdditionalInformation",XML_ELEMENT,node3)

xmlWritePrettyToString(str,node,0)

str.replace(str,chr$(10)&chr$(9)&"S"," S",output) |# To Take care of attribute of the Parent Node
str = output

str.replace(str,chr$(10)&chr$(9)&chr$(9)&chr$(9)," ",output) |# To take care of the other attributes
str = output

seq.puts(str,i)
seq.close(i)
}

Attached is the XML output of the same.

gianci
14th October 2015, 14:09
Hello Bhushan,
it worked.
Following your suggestion, I added some special string at the beginning of each attributes, written the XML file with xmlWritePrettyToString, then replaced each special string preceded by LF and tabs with a space.

Only a last issue, if you still have patience:

there is a line on the sample XML which looks like

<AdditionalInformation></AdditionalInformation>

but it should look like
<AdditionalInformation />

is there a statement that can perform a line registration with this format, or do I have to apply the same trick as for the attribute lines formatting?

Thanks
Gianni

bhushanchanda
14th October 2015, 16:10
Hi,

Did you check the XML file I generated? It has the same element which you require -

<?xml version="1.0"?>
<PrototypeData Status="Initial">
<DataRecord>
<PartNumberCustomer>AAABBBCCC</PartNumberCustomer>
<ReferenceID CodingScheme="ISO-IEC">1111122222</ReferenceID>
<ReferenceID CodingScheme="ISO-IEC">1111122223</ReferenceID>
<CompanyIdNumber Agency="DUNS">22344</CompanyIdNumber>
<DataRecordTimeStamp>2015-09-30T11:00:36.873985+02:00</DataRecordTimeStamp>
<SupplierName>AAABBBCCCDDD</SupplierName>
<PartNumberSupplier></PartNumberSupplier>
<Comment></Comment>
</DataRecord>
<PartInformationRecord>
<PartDescription>Part ABC</PartDescription>
<NetWeight UoM="KGM">0.05</NetWeight>
<PartWeightMethod>Calculated</PartWeightMethod>
<Comment></Comment>
<AdditionalInformation/>
</PartInformationRecord>
</PrototypeData>



Here's the code (Repost)

function main()
{
long node,node1,node2,node3,i,node4
string str(1500)
string output(1) based
long p

i = seq.open("test123.txt","w")
node = xmlNewNode("PrototypeData",XML_ELEMENT)
xmlsetAttribute(node, "Status", "Initial")
node1 = xmlNewNode("DataRecord", XML_ELEMENT,node)
xmlNewDataElement("PartNumberCustomer", "AAABBBCCC" ,node1)
node2 = xmlNewDataElement("ReferenceID", "1111122222" ,node1)
xmlsetAttribute(node2,"CodingScheme","ISO-IEC")
node2 = xmlNewDataElement("ReferenceID", "1111122223" ,node1)
xmlsetAttribute(node2,"CodingScheme","ISO-IEC")
node2 = xmlNewDataElement("CompanyIdNumber", "22344" ,node1)
xmlsetAttribute(node2,"Agency","DUNS")
node2 = xmlNewDataElement("DataRecordTimeStamp", "2015-09-30T11:00:36.873985+02:00" ,node1)
node2 = xmlNewDataElement("SupplierName", "AAABBBCCCDDD" ,node1)
node2 = xmlNewDataElement("PartNumberSupplier", "" ,node1)
node2 = xmlNewDataElement("Comment", "" ,node1)

node3 = xmlNewNode("PartInformationRecord", XML_ELEMENT,node)
node4 = xmlNewDataElement("PartDescription", "Part ABC" ,node3)
node4 = xmlNewDataElement("NetWeight", "0.05" ,node3)
xmlsetAttribute(node4,"UoM","KGM")
node4 = xmlNewDataElement("PartWeightMethod", "Calculated" ,node3)
node4 = xmlNewDataElement("Comment", "" ,node3)
node4 = xmlNewNode("AdditionalInformation",XML_ELEMENT,node3)

xmlWritePrettyToString(str,node,0)

str.replace(str,chr$(10)&chr$(9)&"S"," S",output) |# To Take care of attribute of the Parent Node
str = output

str.replace(str,chr$(10)&chr$(9)&chr$(9)&chr$(9)," ",output) |# To take care of the other attributes
str = output

seq.puts(str,i)
seq.close(i)
}

This line creates that tag
node4 = xmlNewNode("AdditionalInformation",XML_ELEMENT,node3)

Basically, that's a node and not an element.

gianci
14th October 2015, 19:52
Hi Bhushan,
ops. I did really miss that statement.
I changed that part of the script, and of course it worked.

Now the file is definitely complete and aligned with the requirements.
Thanks a lot for your help, and best regards
Gianni