PC SOFT

FOROS PROFESIONALES
WINDEVWEBDEV y WINDEV Mobile

Inicio → WINDEV 2024 → generating a Xml from an XSD
generating a Xml from an XSD
Iniciado por guest, 05,ago. 2015 00:00 - 9 respuestas
Publicado el 05,agosto 2015 - 00:00
Hi I am working on an xml file. I need to generate a xml from an xsd The xsd is availeble so I imported that. OK

Now I need so generate the xml. That works ok. But not when looping through something that needs to be added several times isnot saved in the xml with xmlsave()

I need to generate an xml file called xml-audit file.

My code:


sDatumcreated is string
sDatumcreated = DateToString(Today(),"YYYY-MM-DD")
sGeselecteerdBoekjaar is string
sTartDatBoekjaar is string
sEindDatBoekjaar is string


IF HReadSeekFirst(Boekjaar,BoekjaarID,COMBO_Boekjaar) THEN
sGeselecteerdBoekjaar = Boekjaar.Jaar
sTartDatBoekjaar = Boekjaar.Jaar + "-01-01"
sEindDatBoekjaar = Boekjaar.Jaar + "-12-31"
END

//sStr is string = "xmldoc.auditfile."
xmldoc is xmlDocument , description = "XmlAuditfileFinancieel32"
xmldoc = XMLOpen("XmlAuditfileFinancieel32.xml",fromFile)


IF XMLValidDocument(xmldoc) = True THEN

Filter is string = "XML files (*.xml)" + TAB + "*.xml" + CR + "All files (*.*)" + TAB + "*.*"
OutputFile is string = fSelect("", "", "Maak XML file...", Filter, "xml", fselCreate)
// Vullen header
xmldoc.auditfile.header.curCode = "EUR"
xmldoc.auditfile.header.softwareDesc = "Administratie"
xmldoc.auditfile.header.softwareVersion = ExeInfo(exeVersion)
xmldoc.auditfile.header.dateCreated = sDatumcreated
xmldoc.auditfile.header.fiscalYear = sGeselecteerdBoekjaar
xmldoc.auditfile.header.startDate = sTartDatBoekjaar
xmldoc.auditfile.header.endDate = sEindDatBoekjaar

IF HReadFirst(Bedrijf,BedrijfID) THEN
//{sstr + "company.companyName" } = bedrijf.naam
//vullen company eerst nodes zonder child nodes
xmldoc.auditfile.company.companyName = Bedrijf.Naam
xmldoc.auditfile.company.companyIdent = gsadministratienaam
xmldoc.auditfile.company.taxRegistrationCountry ="NL"
xmldoc.auditfile.company.taxRegIdent = Bedrijf.BTW_nummer
// vullen company.streetAddress node
xmldoc.auditfile.company.streetAddress.streetname = Bedrijf.Adres
xmldoc.auditfile.company.streetAddress.city = Bedrijf.Woonplaats
xmldoc.auditfile.company.streetAddress.country = "NL"
xmldoc.auditfile.company.streetAddress.postalCode = Bedrijf.Postcode
xmldoc.auditfile.company.streetAddress.number = Bedrijf.huisnummer
xmldoc.auditfile.company.streetAddress.numberExtension = Bedrijf.huisnummertoevogsel
// vullen company.postalAddress node
xmldoc.auditfile.company.postalAddress.city = Bedrijf.Woonplaats
xmldoc.auditfile.company.postalAddress.postalCode = Bedrijf.Postcode
xmldoc.auditfile.company.postalAddress.streetname = Bedrijf.Adres
xmldoc.auditfile.company.postalAddress.number = Bedrijf.huisnummer
xmldoc.auditfile.company.postalAddress.numberExtension = Bedrijf.huisnummertoevogsel
xmldoc.auditfile.company.postalAddress.country = "NL"
END
// vullen cutomer supplier. Eert de klanten dan de leveranciers in de customer supplier type geef ik aan
// of het om een klant of Leverancier gaat. klant = C Leverancier = S Als unike id geef ik het id van het bestand
FOR ALL Klanten //where klanten.NietActief = 0
xmldoc.auditfile.company.customersSuppliers.customerSupplier.custSupID = Klanten.KlantenID
xmldoc.auditfile.company.customersSuppliers.customerSupplier.custSupName = Klanten.FirmaNaam
// Here is it where it goes wrong. The loop is run but only the last item in the loop is saved. If I place XMLSAVE()
in this section the same happens. The values are over written and only the last value is saved in the xml
END


XMLSave(xmldoc, OutputFile,XMLDocumentDefault)
IF ErrorOccurred = True THEN
Error("Unable to save the XML document", ErrorInfo())
ELSE
Info("OK", OutputFile)
END
ELSE
Error("The XML document is invalid", ErrorInfo())
END

Anybody An Idea what Iam doing wrong. It seems to modify I need the loop items to be added. Do I need to add this to a buffer or something?

Thanks

Allard
Publicado el 05,agosto 2015 - 07:15
Hi Allard,

you can try to use an index for this part:

i is int = 1
FOR ALL Klanten //where klanten.NietActief = 0
xmldoc.auditfile.company.customersSuppliers.customerSupplier.custSupID[ i ] = Klanten.KlantenID
xmldoc.auditfile.company.customersSuppliers.customerSupplier.custSupName[ i] = Klanten.FirmaNaam
i++
END
Publicado el 05,agosto 2015 - 07:33
Did you try the following syntax in your FOR loop?

xmldoc.auditfile.company.customersSuppliers.customerSupplier.custSupID[i] =...

with I incrementing of course ;-)
Publicado el 05,agosto 2015 - 09:43
He Stefan,

Thanks. I did the following and it works. Had do do this change to pass the validation of the xsd.

xmldoc.auditfile.company.customersSuppliers.customerSupplier.custSupID = Klanten.KlantenID
xmldoc.auditfile.company.customersSuppliers.customerSupplier .custSupName= Klanten.FirmaNaam
i++

Hmm I cannot past my code it seems. I Added the "" after the second customerSupplier

Thanks you saved my day!!

regards
Allard
Publicado el 05,agosto 2015 - 09:48
Allard

You need to declare an 'xmlNode' variable.

Look in the Help - there is a very good example.
Publicado el 05,agosto 2015 - 11:52
Hi Derek,

Indeed I did that earlier on.I use windev 18 and there seems to be a bug on that. So I use the whole path everytime. This does work

The bug was odd in the debugger I got results but in the generated xml I got nothing. After a day of strugling I found out that the nodes don't work . That is why I use the complete path.

regards

Allard

PS I dragged the nodes from the project explorer so there wher no typing issuses. regarding case sensitivity.
Publicado el 06,agosto 2015 - 13:24
Any one an Idea how to get this date time format?

2001-12-17T09:30:47-05:00

Thanks
Allard
Publicado el 07,agosto 2015 - 14:38
Hi Allard,

The date time format might be achieveable by something like this...

DateToString(DateTimeLocalToUTC(DateSys + TimeSys),"YYYY-MM-DD" + "T" + DateToString(DateTimeLocalToUTC(DateSys + TimeSys),"HH:mm:SS")) + "Z"

I assume the Format is W3C datetime, which means (I think) you could use the Z to indicate UTC.

Thanks
Ned!
Publicado el 07,agosto 2015 - 18:27
Allard

This procedure will return a date time stamp in ISO 8601 format. Input is a local date and time (defaults to the current date and time). Output is either in UTC, or local time with an UTC offset (as per your request). The output format is either basic (no punctuation marks), or extended (punctuation marks).

Christopher



// Summary: return a {basic, expanded} date-time stamp of the current date and time in {UTC, local with UTC offset}. (ISO 8601) // Syntax: //[ = ] DateTimeStampUTC ( [ is string [, is boolean [, is boolean]]]) // // Parameters: // psInDate (UNICODE string - default value=""): optional input date, in YYYYMMDDHHMMSSCC format; empty string defaults to current local date and time // pbBasic (boolean - default value=1): T: return basic ISO 8601 format (default). i.e. yyyymmddThhmmssZ; F: return extended ISO 8601 format. i.e. yyyy-mm-ddThh:mm:ssZ // pbOffset (boolean - default value=0): T: return local date with UTC offset. i.e. yyyymmddThhmmss+hh:mm; F: return UTC date (default). i.e. yyyymmddThhmmssZ // Return Value: // UNICODE string: // ISO 8601 date and time in {yyyymmddThhmmssZ, yyyy-mm-ddThh:mm:ssZ, yyyymmddThhmmss+hh:mm, yyyy-mm-ddThh:mm:ss+hh:mm}; empty string if invalid date // Notes: // 1. default input parameters return UTC datetime stamp of current date and time. i.e. yyyymmddThhmmssZ // FUNCTION DateTimeStampUTC(psInDate is string = "", pbBasic is boolean = True, pbOffset is boolean = False) bValid is boolean = True dtDate is DateTime dtLocal is DateTime dtUTC is DateTime duDiff is Duration sDate is string sDiff is string sHour is string sInDate is string sMin is string sOffset is string sOutput is string sTime is string sInDate = NoSpace(psInDate) IF Length(sInDate) > 0 THEN // use input date dtLocal = sInDate bValid = DateTimeValid(dtLocal) ELSE // default dtLocal = Today() + Now() END IF bValid = True THEN // local date time is valid dtUTC = DateTimeLocalToUTC(dtLocal) // YYYYMMDDHHMMSSCC0 - note 0 at end, as WD bug (still in WD19) IF pbOffset = True THEN // return local date with UTC offset sDiff = DateTimeDifference(dtUTC, dtLocal) // correct: UTC - Local. i.e. '+000000008000000' or '-000000008000000' duDiff = StringToDuration(sDiff, durationCenti) // hour minute: '8 0' or '-8 0' sHour = NumToString(duDiff..Hour, "+03d") // i.e. '+08' or '-08' sMin = NumToString(duDiff..Minute, "02d") IF pbBasic = True THEN sOffset = sHour + sMin // basic. i.e. '+0800' or '-0800' ELSE sOffset = sHour + ":" + sMin // extended. i.e. '+08:00' or '-08:00' END dtDate = dtLocal ELSE // return UTC date (default) sOffset = "Z" dtDate = dtUTC END IF pbBasic = True THEN // basic (default) sDate = DateToString(dtDate, "YYYYMMDD") sTime = TimeToString(dtDate..Time, "HHMMSS") ELSE // extended sDate = DateToString(dtDate, "YYYY-MM-DD") sTime = TimeToString(dtDate..Time, "HH:MM:SS") END sOutput = sDate + "T" + sTime + sOffset END RESULT sOutput
Publicado el 02,julio 2019 - 10:50
ChrisC,

I find your function very useful!
Thanks for publishing it!

Marcin