Deal with XML files: Difference between revisions

From Cuis CookBook
Jump to navigation Jump to search
(importe material)
 
(No difference)

Latest revision as of 20:39, 12 May 2025

TODO. Complete the task and upload an example data file with (prices made up).

Many files today are encoded as XML. Some of them have a quite complex data structure and are hard to read for human eyes. But good news! In Cuis we have a package to deal with XML and Smalltalk makes exploring XML a lot more enjoyable than in any other programming language.

Problem. I have a company price list in several LibreOffice .odt files. I want to update all those prices adding 1.5%.

Solution. Say you have a file priceList1.odt then:

  • Make a directory printList1Dir and copy princeList1.odt in there.
$> mkdir princeList1Dir
$> cp priceList1.odt  priceList1Dir
$> cd priceListdir
  • Unzip the file (yes, .odt file as just zipped document, isn't it great?)
  • Remove the zipped file from the new directory (you will see why later on)
$> unzip princeList1.odt 
$> rm princeList1.odt
  • Observe there is a file called content.xml, tadaaaa, we are going to work on that !

TO FINISH

  • Just started to collect the necessary commands for reading
  • I miss commands for writing
  • I need to work with true files to make this of any use
Feature require: 'YAXO'. 

f1 _ '/home/p/download/test1-odt/content.xml' asFileEntry readStream . 
xdoc _ XMLDOMParser parseDocumentFrom: f1. 
f1 close. 
xdoc class. " XMLDocument "
xdoc elements.   " show all elements "
xdoc explore. 

t1 _ xdoc firstTagNamed: #'table:table'. 
t1 class. " XMLElement "
t1 elements . 

t1 firstTagNamed: #'text:p'. 
" <text:p text:style-name="P2">1A</text:p> "

t1 tagsNamed: #'text:p' do: [ :el | 
	Transcript log: ('	{1}' format: {el. }).  
	Transcript log: ('- contents 	{1}' format: {el contents. }).  
	].