Script to download xml files from a website

8 views (last 30 days)
Matthew Worker
Matthew Worker on 4 Jun 2017
Edited: John Kelly on 12 Jul 2017
I would like to write a script that download xml files from
https://www.treasurydirect.gov/xml/
with the file name that starts from R_ and put into a single excel file. Please advise.

Answers (2)

Walter Roberson
Walter Roberson on 3 Jul 2017
Edited: John Kelly on 12 Jul 2017
MATLAB Answers is not a private consulting service. The "cost" we charge for providing answers is that the posted Questions and Answers are public so that everyone can learn from them. Editing away your question after you have received your answer is rude.
If you want to ask questions without the question and answer becoming public, then you should hire a private consultant.
  1 Comment
Adam
Adam on 5 Jul 2017
It's a shame we can't have a system for flagging users who keep doing this so we know not to waste time answering their questions in future. I know we can search every person's history of questions before answering, but obviously that isn't something anyone wants to do.

Sign in to comment.


Walter Roberson
Walter Roberson on 5 Jun 2017
base = 'https://www.treasurydirect.gov/xml/'
S = urlread(base);
R_files = regexp(S,'(?<=<a href=")R[^"]+', 'match');
num_files = length(R_files);
R_content = cell(num_files, 1);
for K = 1 : num_files
try
R_content{K} = urlread( [base, R_files{K}] );
end
end
fid = fopen('YourOutput.csv', 'wt');
for K = 1 : num_files
fwrite(fid, regexprep(R_content{K}, {'^', '$'}, {'"', '"'}, 'lineanchors') );
end
fclose(fid)
My Magic 8 Ball predicts disappointment in the CSV file created: it predicts that you want to extract particular data from each of the files and write those as columns, instead of what you asked for, which is to write the content of .xml files directly as lines in the csv.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!