STx 3.9 Documentation

Up

Introducing File Items

Previous

Status File

Next

Binary Files

XML Files

S_TOOLS-STx file items can also read and write XML files. The following is the content of a simple XML file:

xml_file_example_1.png

XML File Example

The following script demonstrates the use of an XML file item.

[macro xml_file_example] readvar #argv #example';'#argv /Delete if '$#example+0' != '$#example' #example := 1 if '$#example' < 1 || '$#example' > 2 #example := 1 // create the XML file #f := new file * /XML /Write if '$#f[?]' != 'file' then em -1 ; '$#mac::Error - Failed to create the XML file item' end // do the example GoSubX Example_$#example $#argv // delete file item delete $#f exit // // Create some elements and attributes, modify them and display them // Uses the commands: // root | addelement | goto | find | deleteelement | save // Example_1: // create XML data // // create root // $#f root 'rootTag' attr1 attrValue1 /Delete if '$rc' > 0 em $rc ; $#mac - Failed to set root tag // // create elements // $#f goto /In if '$rc' > 0 em $rc ; $#mac - Failed to go into root element for #i := 0 to $#i < 10 step #i := int $#i + 1 $#f addelement 'elemTag' ID $#i if '$rc' > 0 then em $rc ; $#mac - Failed to add XML element "elemTag ID $#i" end $#f setdata '$(eval fill(10,0,$#i))' /Table /Numeric /Vector if '$rc' > 0 then em '$rc ; $#mac - Failed to set the element`'s data' end end // // find and delete the element with the ID 3 // $#f find * 'ID:==:3' /First if '$rc' == '0' $#f deleteelement // // save XML file // $#f save 'xml_file_example_1.xml' if '$rc' > 0 then em $rc ; $#mac - Failed to save "xml_file_example_1.xml" end // // display the file // showitem $#f exit // // Ask user to select a file, check if it is an STx dataset file and // find the longest file. Uses the XML file item commands: // load | goto | extracttable | position // Example_2: readvar #argv #fDatasetPath if '$#fDatasetPath' == '' then // ask user to select dataset file from disk #caption := set 'Select an STx project from disk' #filetype := set 'STx Project File' BUtil FileDialog Open ; $#caption ; $@WORK ; xml=$#fileType readvar result #fDatasetPath if '$#fDatasetPath' == '' exit end // // check if it is a valid dataset file // $#f load '$#fDataSetPath' if '$rc' > 0 then em $rc ; $#mac - Failed to load the file "$#fDatasetPath". end $#f goto '/STXDataSet' if '$rc' > 0 em $rc ; $#mac - Invalid STx project file if '$#f[!xattr,AFile]' == 'Link' then #msg := set 'The project you chose is linked and contains no' #msg := set $#msg ' segments. Please choose an unlinked project.' butil 'msgbox msg ; $#msg' exit 1 set '$(xml_file_example 2)' end // // extract all segments from the dataset into table // #tExt := new table * * /E pos tag id length if '$#tExt[?]' != 'table' then em -1 ; $#mac - Failed to create extended table end $#f extracttable $#tExt pos ASeg tag ID id L length /Recursive if '$rc' > 0 then em $rc ; $#mac - EXTRACTTABLE failed ($EMSG) end showitem $#tExt ; All segments in the project "$#fDataSetPath" // // find and select the longest segment // #max := 0 for #i := 0 to $#i < $#tExt[] step #i := int $#i + 1 #len := $#tExt[$#i,length] if '$#len' > '$#max' then #max := $#len #iMax := $#i end end $#f position $#tExt[$#iMax,pos] /Goto if '$rc' > 0 then em $rc ; $#mac - Failed to select the longest segment end // show file showitem $#f // clean up delete $#tExt exit

© 2009 The Austrian Academy of Sciences Acoustics Research Institute