[Solved] Export xml scheme file

Creating a macro - Writing a Script - Using the API

[Solved] Export xml scheme file

Postby sevalav » Tue Dec 25, 2012 11:50 pm

Hello
Is it possible to export some data from Calc in XML scheme file, with Basic?
Last edited by sevalav on Sun Feb 24, 2013 2:31 am, edited 1 time in total.
Apache OpenOffice 4.1.0, Win
sevalav
 
Posts: 91
Joined: Mon Apr 16, 2012 12:37 am

Re: export xml scheme file

Postby Villeroy » Wed Dec 26, 2012 1:55 am

Why that complicated? It is zipped xml and content.xml has all the data.
Under Linux:
Code: Select all   Expand viewCollapse view
unzip MySpreadsheet.ods content.xml
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.4
User avatar
Villeroy
Volunteer
 
Posts: 28448
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Export xml scheme file

Postby sevalav » Wed Dec 26, 2012 4:25 pm

When making invoice I need to sent via internet that invoice in xml fajl, also zipped. Here is how it must look like.
Attachments
05-N-82012-55156611-12-01-26-15-01-28.xml.zip
(2.24 KiB) Downloaded 291 times
Apache OpenOffice 4.1.0, Win
sevalav
 
Posts: 91
Joined: Mon Apr 16, 2012 12:37 am

Re: Export xml scheme file

Postby JohnSUN-Pensioner » Wed Dec 26, 2012 4:37 pm

Wow, sevalav! And which form of this data you have in Calc workbook?
I may not have a lot to give but what I got I'll give to you...
Apache OpenOffice 4.1.5, LibreOffice 6.4.4.2 (x64) on Windows 7
If you think that I did not answer your question, make allowances for my imperfect English
User avatar
JohnSUN-Pensioner
Volunteer
 
Posts: 824
Joined: Fri Jan 14, 2011 1:21 pm
Location: Kyiv, Ukraine

Re: Export xml scheme file

Postby sevalav » Wed Dec 26, 2012 10:03 pm

I have not, yet. This example (attached file in my previously post) is from another program, but I need the same result. I am looking for some solution that I can this create in calc, with Basic. It is not problem for me to create calc spreadsheet where i will insert new invoice, sheet where this invoice will be saved, and sheet where I can see this invoice again, with all data (all with Basic). Problem for me is how to create file like this example. It's OK if the solution is not elegant.
Any help? Idea?
Apache OpenOffice 4.1.0, Win
sevalav
 
Posts: 91
Joined: Mon Apr 16, 2012 12:37 am

Re: Export xml scheme file

Postby JohnSUN-Pensioner » Thu Dec 27, 2012 11:03 am

Small hint... Or here...
I may not have a lot to give but what I got I'll give to you...
Apache OpenOffice 4.1.5, LibreOffice 6.4.4.2 (x64) on Windows 7
If you think that I did not answer your question, make allowances for my imperfect English
User avatar
JohnSUN-Pensioner
Volunteer
 
Posts: 824
Joined: Fri Jan 14, 2011 1:21 pm
Location: Kyiv, Ukraine

Re: Export xml scheme file

Postby sevalav » Fri Dec 28, 2012 1:40 am

I will try. Thanks.
Apache OpenOffice 4.1.0, Win
sevalav
 
Posts: 91
Joined: Mon Apr 16, 2012 12:37 am

Re: Export xml scheme file

Postby sevalav » Mon Jan 28, 2013 9:04 pm

Well, I am close to finish this work. Everything works good, but there is a little problem. When data for output has characters like "č" or "ž" or "ć", in xml file I get something different. How to solve this?
Apache OpenOffice 4.1.0, Win
sevalav
 
Posts: 91
Joined: Mon Apr 16, 2012 12:37 am

Re: Export xml scheme file

Postby rudolfo » Tue Jan 29, 2013 2:33 am

sevalav wrote:I get something different. How to solve this?


Hey, think twice. From the context we can only guess that you followed one of the recipes that JohnSUN gave you. But that's only a guess, and we surely can't guess which one you followed. Likewise we can't guess, what something different is. If you don't describe what you have done and how your results look like, nobody will be able to help you.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
rudolfo
Volunteer
 
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Export xml scheme file

Postby sevalav » Wed Jan 30, 2013 12:18 am

Hi Rudolfo! With code (basic) I put one sort of data in one cell (in column B), for example, <Adresa>Konjovića</Adresa>. In second cell, bellow, my code put next tag, and so on. After that, another code (with VBA support) doing next step (look part of code bellow):
Code: Select all   Expand viewCollapse view

Doc = StarDesktop.CurrentComponent
Sheet = Doc.Sheets.getByName("Priprema")
n = 1
B1 = Sheet.getCellRangeByName("B" & n)
C = B1.string
Set oWorkSheet = ThisWorkbook.Sheets("Priprema")
iFileNum = FreeFile
Open ofire For Output As #iFileNum

Do While B1.string <> ""
   Print #iFileNum, C
   n = n + 1
   B1 = Sheet.getCellRangeByName("B" & n)
   C = B1.string
Loop

After that, another code will open dialog ("Save as") for saving document and seted XML type of file:
Code: Select all   Expand viewCollapse view
oFileDialog = CreateUnoService("com.sun.star.ui.dialogs.FilePicker")
  oFileDialog.Initialize ( Array(com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_AUTOEXTENSION ) )
  oFileDialog.AppendFilter( "XML files (*.xml)", "*.xml" )

After that, I must give a name of XML document manual, and press button to save this file.
As I can see, the problem is in character which are not part of English language. Everything else is good. To be more specific, character like "ć", "č", in xml file are presented with "?" character, "Ž" with ΀, and little "ž" with "ހ".
If you need some other information, just tell me.
Apache OpenOffice 4.1.0, Win
sevalav
 
Posts: 91
Joined: Mon Apr 16, 2012 12:37 am

Re: Export xml scheme file

Postby rudolfo » Wed Jan 30, 2013 2:01 am

This seems like a problem with the character encoding. If you have a well formed XML file at the end of the process, its first line should either be:
<?xml version="1.0" encoding="UTF-8"?>
or
<?xml version="1.0" encoding="ISO-8859-1"?>

If you see UTF-8 you will be on the save side, as in this case no conversion from the internally used character encoding in OpenOffice and the encoding in the output XML file is needed/used. But of course you need an Text Editor that is UTF-8 aware. The best thing in this case is to view the XML document in the browser. All receent browsers (not older than 5 years) can process any character code that is specified on the first line of the XML file.

It's not really clear what you are doing in your code. The first piece that you are showing makes me think that you are going through the column B from line 1 to N and write the string contents of the cell B1, B2, etc.
into a file with a filename that is stored in the string variable ofire. You write to the file with the function Print #filehandle aString. The Print statement is a Basic function that dates back to the first Basic steps starting with MS DOS. It might be aware of 8bit encoding like ansi-1252 or iso-8859-1 (also known as latin-1), but it is surely not Unicode (UTF-8) aware.

If you are lucky it just writes a sequence of Bytes which would give a correct UTF-8 encoding.
Try to write an additional first "header" line into your file: Just before your while loop, add a line:
Code: Select all   Expand viewCollapse view
Print #iFileNum, "<?xml version='1.0' encoding='UTF-8'?>"


And see if this changes the results when you open your file with some Text editor, XML Editor or browser after the export is finished.

If it does not help, all the bells and whistles should ring in the back of your mind telling you that your are on the wrong track combining a 25 year old Print technology with modern XML character encoding. Check out the OpenOffice Basic Wiki to learn how to work with storeAsURL or storeToURL to save a complete Calc Spreadsheet to an XML file.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
rudolfo
Volunteer
 
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Export xml scheme file

Postby karolus » Wed Jan 30, 2013 2:39 am

Hallo

Plain (40 y) old Basic commands can't write anything outside ascii, use API with 'simpleFileAccess'
or Python instead Basic

Karolus
AOO4, Libreoffice - 5.1 … 5.3.2.2 on Linux Mint17
User avatar
karolus
Volunteer
 
Posts: 852
Joined: Sat Jul 02, 2011 9:47 am

Re: Export xml scheme file

Postby sevalav » Thu Jan 31, 2013 2:04 am

its first line should either be:
<?xml version="1.0" encoding="UTF-8"?>

I already have this line in my Xml file, but the problem is still here.
Plain (40 y) old Basic commands can't write anything outside ascii, use API with 'simpleFileAccess'
or Python instead Basic

Well, I do not know other language. I know something about Basic, but I never used simpleFileAccess, so...eny simple example will be helpful.
Apache OpenOffice 4.1.0, Win
sevalav
 
Posts: 91
Joined: Mon Apr 16, 2012 12:37 am

Re: Export xml scheme file

Postby rudolfo » Thu Jan 31, 2013 3:12 am

OpenOffice has an reference site for the UNO API. Go to this site and type the phrase simpleFileAccess in the searchbox in the right upper corner. A Google search restricted to the OOo API site will be started ... and you can simply inspect the results.
Well, yes, I have to admit, the pages there are rather technical and not like a tutorial with samples how to use it. But on the other hand I am sure, I have seen some sample code with simpleFileAccess somewhere in the Macro forum. And suprise ... in the upper right corner of this forum is a search box. simpleFileAccess is a rather specific phrase, so you will not be flooded with results, but those results that are listed are most probably posts that know what they are talking about.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
rudolfo
Volunteer
 
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Export xml scheme file

Postby sevalav » Thu Jan 31, 2013 4:05 pm

Thanks, I will search around the net. A little question, to clarify whole idea:do you thinking that I need to open existing xml file and export my data from column B to this xml file, or to create a new xml file and export my data to this new file?
Apache OpenOffice 4.1.0, Win
sevalav
 
Posts: 91
Joined: Mon Apr 16, 2012 12:37 am

Re: Export xml scheme file

Postby Villeroy » Fri Feb 01, 2013 9:26 am

XML had been designed to make things easier for programmers. There must be some software which fits perfectly into the structure of your <FakturaZaNalog>.
I'm 100% certain that it is not supposed to be loaded into spreadsheets. Simply ask the the people who produce such files about availlable software to load and edit this particular flavour of XML. Otherwise you may waste time and money.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.4
User avatar
Villeroy
Volunteer
 
Posts: 28448
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Export xml scheme file

Postby sevalav » Fri Feb 01, 2013 5:17 pm

Well, there is First object xml editor (link: http://www.firstobject.com/dn_editor.htm). If I simple select and copy cell range in column B (where is stored whole data for my xml) and past into this XML editor, I can get nice XML file, with all local characters. That solution works, but it is ... well, it is not comfortable, because I must this XML file zipped. I must using onother program for that...
Apache OpenOffice 4.1.0, Win
sevalav
 
Posts: 91
Joined: Mon Apr 16, 2012 12:37 am

Re: Export xml scheme file

Postby Villeroy » Fri Feb 01, 2013 5:44 pm

There must be some accounting software able to import that stuff. Which software has exported the XML?
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.4
User avatar
Villeroy
Volunteer
 
Posts: 28448
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Export xml scheme file

Postby sevalav » Mon Feb 04, 2013 4:36 pm

Well, now is to late for that option. I done everything else, my spreadsheet working as a little base of data, I made some macros and working with this document now is comfortable. As I can see, only problem is copy data from column B to this new document!
Apache OpenOffice 4.1.0, Win
sevalav
 
Posts: 91
Joined: Mon Apr 16, 2012 12:37 am

Re: Export xml scheme file

Postby Villeroy » Mon Feb 04, 2013 5:14 pm

But there must be some answer! Somebody writes a software which exports your <FakturaZaNalog> nodes. All this makes no sense if there is no tool to handle this particular data structure. Where does this XML come from? What is its intention?
This is like producing a cog wheel without bothering about a gear box where it fits into.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.4
User avatar
Villeroy
Volunteer
 
Posts: 28448
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Export xml scheme file

Postby sevalav » Wed Feb 06, 2013 12:37 am

Where does this XML come from? What is its intention?

If this is not only rhetorical question, well, and if I understand you correctly, I must tell again: my code generating content of that what would be one day content of XML file. it looks like:
column B
<?xml version="1.0" encoding="UTF-8"?> ' b1
<FakturaZaNaloge xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 'b2
<Info> 'b3
<Sifra>555555</Sifra>
<Naziv>SZTR Optika Bukovc</Naziv>
<Adresa>Zeleznicka 16, Odzaci</Adresa>
<Telefon>0255777221</Telefon>
<BrZR>350-2522-20</BrZR>
<PIB>252525252</PIB>
<XMLverzija>1.0</XMLverzija>
</Info>
<FakturaInfo>
....

This is only a part of content, an how it looks like, visually, in my spreadsheet. This content is in fact my invoice. Length of this content is not fixed. Some invoices in this format can be larger, or shorter. Some part will be repeat, if it is necessary. But, this is not problem, because my code in basic can handle with this. All what problem now is how to open some file, put this content for my spreadsheet from column B in that file, and make it an xml file, zipped. If you are asked me what I am doing with this file after all, well, I send this file to one institution for health Insurance. They have some filter, to check is this XML file correct, and that is all story. I thought that function Print will be good for me, but I do not count that I am using local character. For all other who want to create an XML file, who are using English language, this solution with function Print will be working excellent.
I was really surprised that nobody makes some improvement inside Print function, that all world in all languages can using this, with no restrictions.
All of you are truly wonderful people, after all, you help me with ideas to create some useful document for me. If nobody have some practical advice or solution for making XML file, I will looking some advice for some local programers in my country.
Making some filter maybe will be solution for me...I do not know... But that is something what I do not know how to create and deal with this.
Until finally solution, I will manually coping/pasting content from my column B to the XML editor and making an XML file. After that, I will manually using another program to zipped this XML file. Even that is better than nothing.

Here you can see output XML file what create code for my spreadsheet.
File with no local characters:
https://www.dropbox.com/s/vz9xv4qflsupp ... racter.xml
File with local characters (look <Naziv> and <Adresa>:
https://www.dropbox.com/s/wzjbp8ys6milk ... racter.xml
Apache OpenOffice 4.1.0, Win
sevalav
 
Posts: 91
Joined: Mon Apr 16, 2012 12:37 am

Re: Export xml scheme file

Postby rudolfo » Thu Feb 07, 2013 12:43 am

sevalav wrote:
Where does this XML come from? What is its intention?

[...]
They have some filter, to check is this XML file correct, and that is all story.


XML is a Markup Language and the X is for eXtensable. It is used very often as an interchange format for data, because it is well documented, can be checked on well formedness and it can be validated. If you say that "they have some filter" you are basically answer the question that Villeroy brought up.
Checking if the XML is correct first means to check if an XML file (or string) is well formed. Firefox or IE can do this for you, if you tell them that that content type of the file they load is application/xml. But there are several other parsers and tools.
The second pass is called validation and it is either done against the DTD (document type definition) or an xsd schema file. A DTD and a XSD define what child elements a certain XML element (also known as tag or node) can have. One example is the DTD for HTML which says that a <head> element can have a <title> element, but it cannot have a <body> element. <head><body></body></head> would be well-formed, but it won't validate, because the DTD doesn't allow body inside of head. A XSD schema file can even specify some additonal things apart from what can be specified in a DTD ... like ranges of allowed values.

In other words someone out there must have specified either a DTD or a XSD file for your <FakturaZaNaloge>. Normally such a DTD or xsd file is made plublically available. You might recall the DOCTYPE of a html document, they typically end in a URL like www.w3.org/TR/...dtd, which is nothing else then the location where you can download this DTD to validate the HTML document. If your XML doesn't contain such a reference to a DTD doesn't mean that there not such a DTD or XSD. This DTD/XSD is the key to understanding the XML document/structure.

And really better forget about Basic for this task. Your Basic parser depends on lines and/or linebreaks, but XML doesn't care about linebreaks, mostly (with a few exceptions) it is just whitespace. One element or tag per line in XML is essentially the same as having all the XML content in a single line. Use Python and the xml.dom.minidom module to do this if you don't want to use an XSLT transformation. Frankly it is a bit stubborn to say "I use Basic, because this is the language I already know". I am pretty sure that the macro that you have written contains a for or while loop and these print statements that are actually/natively Basic. Most of your code will be a OpenOffice UNO-Object that calls some method returning another UNO object. This has nothing to do with Basic but with the UNO interface/bridge of OpenOffice -- and consequently it is the same for Python and any other language that has a UNO bridge (Java, C++, C#, Delphi, ...)
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
rudolfo
Volunteer
 
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Export xml scheme file

Postby sevalav » Sun Feb 17, 2013 11:33 am

I found some way to create xml file with UTF8 encoding. It is ugly way, but, in the end, I have what I need. In new writer, macro past all data from column B from my spreadsheet, then saving this file as Text encoding (utf8) with extension xml. In that way a have in xml file all my local characters.

Code: Select all   Expand viewCollapse view
REM create the DispatcherService
oDisp = createUnoService("com.sun.star.frame.DispatchHelper")
REM select source range
octl.Select(oSourceRange)
REM copy the current selection to the clipboard.
oDisp.executeDispatch(octl, ".uno:Copy", "", 0, NoArg())

REM open new spreadsheet.
sURL = "private:factory/swriter"
oTargetDoc = Stardesktop.loadComponentFromURL(sURL, "_blank", 0, NoArg())

oTargetDoc.getCurrentController()
REM paste from the clipboard to your current location.
oTargetframe = oTargetDoc.getCurrentController().getFrame()

'Past only unformated text
dim args4(0) as new com.sun.star.beans.PropertyValue
args4(0).Name = "SelectedFormat"
args4(0).Value = 1

oDisp.executeDispatch(oTargetFrame, ".uno:ClipboardFormatItems", "", 0, args4())

rem -------------- Pretvori fajl u txt format, enkodiran sa UTF8, i dodaj ekstenziju xml -------------------
   
  Dim MySaveOptions(1) as New com.sun.star.beans.PropertyValue
       
       'set the options to save the file as text file with UTF8 encoding
       MySaveOptions(0).Name = "FilterName"
       MySaveOptions(0).Value = "Text (encoded)"
        MySaveOptions(1).Name = "FilterFlags"
       MySaveOptions(1).Value = "UTF8"
       
       'store file on location:     
        sSavePath =  "C:\Users\MyDoc\" & ImeFajla & ".xml" ' get ".xml" extension;         ' ImeFajla = Name of my file
       'build the link of the file to get
       sSaveLink = ConvertToURL(sSavePath)

       'save the CRM script file
       oTargetDoc.storeAsURL(sSaveLink , MySaveOptions())

       'close the CRM script file
       oTargetDoc.Close(True)



There is one problem. How I can get zipped xml file with code, after all this save options (look code)?
Apache OpenOffice 4.1.0, Win
sevalav
 
Posts: 91
Joined: Mon Apr 16, 2012 12:37 am

Re: Export xml scheme file

Postby rudolfo » Sun Feb 17, 2013 6:39 pm

Again my answer repeats what others have said before: Basic is the wrong language for this. If you had used a real programming language like Java or Python you would have simply looked up the documentation for the zip/unzip library in the Library documentation for that languages (Python's zipfile module), copied the sample code that you can find there, made some modifications and that's it. Basic lacks all this kind of out-of-the-box support. If you want to do fancier things in Basic you either need to call external programs with shell, in this case InfoZip's zip.exe utility or you fall back on the UNO objects of OpenOffice that build a framework that is quite complete.

Here is some sample code how you can do this with the SimpleFileAccess ZipPackages UNO services. Compare this 30 lines of code with the three lines that you need in Python:
Code: Select all   Expand viewCollapse view
  zip = zipfile.ZipFile('archive.zip', 'w', zipfile.ZIP_STORED)
  zip.write('inputfile.xml')
  zip.close()


If you want to add this to your existing code note that sSaveLink is the URL that you have used in your macro. And the UNO Package adds some extra manifest or meta file to the zip archive. You need to make sure that the other side is not irritated by this additional file.

Code: Select all   Expand viewCollapse view
  Dim oSimpleFileAccess As Object, oZipPackage As Object
  Dim oInputStream As Object, oZipPackageFolder As Object
  Dim oZipPackageStream As Object
  Dim sBaseUrl As String
  Dim sFilename As String
  Dim sParts() As String

  ' Split at path separators (always forward slash, because we have URL syntax)
  sParts = Split(sSaveLink,"/")
  n = Ubound(sParts)         ' Get the index of the last part (the filename)
  sFilename = sParts(n)
  ' Use everything up to where the filename part begins including the final slash
  sBaseUrl = Left( sSaveLink, Len(sSaveLink) - Len(sFilename) )

  oSimpleFileAccess = createUnoService( "com.sun.star.ucb.SimpleFileAccess" )
  ' Open the exported file for reading
  oInputStream = oSimpleFileAccess.OpenFileRead( sBaseUrl & sFilename )

  ' Use the zip packaging UNO service
  oZipPackage = createUnoService( "com.sun.star.packages.Package" )
  oZipPackage.initialize( Array(sBaseUrl & ".zip") )
  ' Parameter is empty to retrieve the root of the archive content
  oZipPackageFolder = oZipPackage.getByHierarchicalName("")

  oZipPackageStream = oZipPackage.createInstance()
  ' Connect it to the Package stream
  oZipPackageStream.setInputStream( oInputStream )
  ' Specify the filename in the zip archive
  oZipPackageFolder.insertByName( sFilename, oZipPackageStream )
  oZipPackage.commitChanges()   ' actually write the zip file to disk

  ' closing the stream helps us to re-use the object variable
  oInputStream.closeInput()
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
rudolfo
Volunteer
 
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Export xml scheme file

Postby sevalav » Sun Feb 24, 2013 2:30 am

Thanks Rudolfo.
Apache OpenOffice 4.1.0, Win
sevalav
 
Posts: 91
Joined: Mon Apr 16, 2012 12:37 am

Re: [Solved] Export xml scheme file

Postby Villeroy » Wed May 01, 2013 12:24 pm

Thank you very much, indeed.
A few remarks from somebody who had a very hard time to write many files into a package using UNO and Basic because the whole project was in a stage too advanced to be rewritten in a true programming language.

rudolfo wrote:
Code: Select all   Expand viewCollapse view
  oZipPackage.commitChanges()   ' actually write the zip file to disk

  ' closing the stream helps us to re-use the object variable
  oInputStream.closeInput()


1) Indeed, you've got to commitChanges every time before you close the inputStream. If you think that you can write your stuff into the package and finally commit all changes to the package, you are wrong.
2) You have no full access to the folders of your zip-package. You can read them, you can write to them, but you can not create nor delete any folders. In order to put some files in a sub-folder, regardless if it exists or not, refer to the root folder as in Rudolfo's example and use a folder prefix together with the file name:
Code: Select all   Expand viewCollapse view
oZipPackageFolder.insertByName( "subfolder/"& sFilename, oZipPackageStream )

3) Each new file requires a new InputStream and a new OutputStream.
You can declare the oSimpleFileAccess and the oZipPackageFolder once, but looping through your file-URLs, each new file requires a new pair of oZipPackageStream, oInputStream:
Code: Select all   Expand viewCollapse view
      sReadLink = someFileURL
      sSaveLink = "subfolder/"& someFileName
      oZipPackageStream = oZipPackage.createInstance()
      oInputStream = oSimpleFileAccess.OpenFileRead(sReadLink)
      oZipPackageStream.setInputStream( oInputStream )
      oZipPackageFolder.insertByName( sSaveLink, oZipPackageStream )
        oZipPackage.commitChanges() ' BEFORE oInputStream.closeInput()
      oInputStream.closeInput() ' AFTER oZipPackage.commitChanges()


4) oZipPackage.initialize( Array(sBaseUrl & ".zip") ) should include some file name between the BaseURL (path) and the .zip. Anyway, method initialize is marked as deprecated (since years) but the alternatives are even more mind blowing and completely undocumented. Why bother? Let's declare the whole UNO bullshit as deprecated.

rudolfo wrote:Here is some sample code how you can do this with the SimpleFileAccess ZipPackages UNO services. Compare this 30 lines of code with the three lines that you need in Python:

Indeed, using a true programming languages with bullet-proof libraries makes a huge difference. If I only knew how to parse XML files in Python which leads back to the origin of this topic.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.4
User avatar
Villeroy
Volunteer
 
Posts: 28448
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] Export xml scheme file

Postby sevalav » Wed May 08, 2013 12:57 am

Villeroy wrote:2) You have no full access to the folders of your zip-package. You can read them, you can write to them, but you can not create nor delete any folders.


Well, this is what I now need, to access to my zipped file and delete extra folder ("META-INF") and extra file ("mimetype") what is created by OpenOffice. Is it really true that there is absolutely no way to manage this zip-package procedure and avoid getting this extra file/folder (or somehow to access to zipped folder and delete this extra file/folder)?
Apache OpenOffice 4.1.0, Win
sevalav
 
Posts: 91
Joined: Mon Apr 16, 2012 12:37 am

Re: [Solved] Export xml scheme file

Postby sevalav » Thu May 09, 2013 4:15 pm

deleted
Last edited by sevalav on Sat May 11, 2013 2:26 am, edited 1 time in total.
Apache OpenOffice 4.1.0, Win
sevalav
 
Posts: 91
Joined: Mon Apr 16, 2012 12:37 am

Re: [Solved] Export xml scheme file

Postby sevalav » Sat May 11, 2013 2:25 am

I am searching, and searching for solution about making zip file without meta files, and now I am focusing on command line python (python.exe) as possible solution. But, I need some help. Because I am working in Basic, is it possible to call python.exe with basic (that part is not problem) and to insert commands on python's command line (this is a problem) and execute python to do something for me?

I want that python take .xml file what is stored on D:\testfile\test.xml and create Zip file, and store this compress file in zipped folder.
I try to implement code what Rudolf suggested in some previous post, and I tested this code with python.exe command line.
Code: Select all   Expand viewCollapse view
zip = zipfile.ZipFile(r'D:\2.zip', 'w', zipfile.ZIP_STORED)
zip.write(r'D:\testfile2\test.xml')
zip.close()

First line create zip folder (2.zip) where will be stored xml file, and that is Ok; second line put both folder "testfile2" (what I do not wont) and file "test.xml" (what I want) in zipped folder but uncompressed (what I do not want).
That are problems about that.
Second, how can I copy this 3 lines of code to python's command line with basic? I tried to figure but without success.
With next code I can open python's command line, but I do not know how to insert this 3 line of code above.

Code: Select all   Expand viewCollapse view
     
oSvc = createUnoService("com.sun.star.system.SystemShellExecute")
oSvc.execute(("C:\Program Files (x86)\OpenOffice.org 3\program\python.exe"), "", 0)

If somebody know even part of solution, please, post here.
Last edited by sevalav on Sat May 11, 2013 1:37 pm, edited 1 time in total.
Apache OpenOffice 4.1.0, Win
sevalav
 
Posts: 91
Joined: Mon Apr 16, 2012 12:37 am

Re: [Solved] Export xml scheme file

Postby rudolfo » Sat May 11, 2013 3:06 am

If you don't want to switch from Basic to Python, it is better to call a tool that is meant to be used from the command line instead of building a python script and calling this script with parameters from Basic (or to be more precise from the shell that is used by Basic when it issues a call to SystemShellExecute). InfoZip is such a tool. The following is a part of its online help. I guess the -j switch is
what you need:

Code: Select all   Expand viewCollapse view
Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license.
Zip 3.0 (July 5th 2008). Usage:
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
  The default action is to add or replace zipfile entries from list, which
  can include the special name - to compress standard input.
  If zipfile and list are omitted, zip compresses stdin to stdout.
  -f   freshen: only changed files  -u   update: only changed or new files
  -d   delete entries in zipfile    -m   move into zipfile (delete OS files)
  -r   recurse into directories     -j   junk (don't record) directory names
  -0   store only                   -l   convert LF to CR LF (-ll CR LF to LF)
  -1   compress faster              -9   compress better
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
rudolfo
Volunteer
 
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Next

Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 8 guests