Subscribe Bookmark RSS Feed

Importing Multiple xml files From a Folder Location

d_barnett

Community Trekker

Joined:

Nov 30, 2011

I have a folder containing lots of xml files that I would like to open, extract information from, save this new information separately ,  and then close both files. I can do this singularly using the 'Import xml' add-in but would like to go through a complete folder automatically to reduce the amount of effort and time I spend on doing this.

I have attached an example folder with xml files in to help if anyone has the time to do this.

Regards

David

5 REPLIES
ian_jmp

Staff

Joined:

Jun 23, 2011

You will need something like this, with 'Open()' instead of 'Print()' of course:

dir = PickDirectory("Select the directory containing your files", "$DESKTOP");

fList = FilesInDirectory(dir);

for(i=1, i<=NItems(fList), i++,

Print(dir||"/"||fList[i]);

);

d_barnett

Community Trekker

Joined:

Nov 30, 2011

Ian,

thanks for the prompt reply but I cannot get this to work with xml files, do I need to parse these or some other technique to open this type of file?

Regards


David

ian_jmp

Staff

Joined:

Jun 23, 2011

Sorry, I thought the issue was the looping, not the reading in. Yes, you will need to parse the XML to pull out what you want once you have used 'LoadTextFile()' to put the contents into a JSL variable. 'ParseXML()' should help.

stan_koprowski

Community Manager

Joined:

Aug 8, 2012

HI David,

Check out the XML addin https://community.jmp.com/docs/DOC-7728.  This in combination with what Ian@JMP​ provided should get you going.

Best,

Stan

ms

Super User

Joined:

Jun 23, 2011

This addin code is very well organized and all its power and functionality can be accessed by running one "master" function. Hence, here it is quite easy to circumvent the addin's GUI and let it work on your xml-files in a loop.

Below is one way to combine the addin with Ian's loop. Note that you may need to set the path variable, i.e. the path to where the addin is installed on your computer. Also, if expanding this code, avoid using variable names that are already used by the included code from the addin, or you may run into problems (e.g. I could not use i as a loop iterator).

// Define path variable (may already be defined)

Set Path Variable("ADDIN_HOME", "Path/to/my/Addins");

// Define all the functions of the Addin but skip the GUI

Include("$ADDIN_HOME/com.jmp.matthew.wolfe.xml.importer/xml parser III.jsl") << close window;

dir = Pick Directory("Select the directory containing your files", "$DESKTOP");

fList = Files In Directory(dir);

myInfo = [];

For(filenr = 1, filenr <= N Items(fList), filenr++,

// Use Function from Addin to parse file into a data table

    dt = runParser(dir || "/" || fList[filenr]);

//Extract your information here! (e.g n rows of dt)

    myInfo |/= N Rows(dt);

//Close the data table

    Close(dt, no save);

);

// Did it work? Summarize in a new data table

New Table("my xml files",

    add rows(N Items(fList)),

    New Column("Sample", character, Values(fList)),

    New Column("n rows", numeric, values(myInfo))

);