Effacer les filtres
Effacer les filtres

How to structure the data using webread

76 vues (au cours des 30 derniers jours)
Jorge Luis
Jorge Luis le 15 Juil 2024 à 12:12
Modifié(e) : Jorge Luis le 17 Juil 2024 à 18:08
Hi, I will have to automatize a procedure to extract data from different URLs, One example of the URL link is the following:
url='https://earthquake.usgs.gov/fdsnws/event/1/query?format=quakeml&eventid=us6000n8tq',
data=webread(url);
The variable data has a huge amount of information that I would like to structure in order to access and retrieve the data easily without the need of creating a xml file.
I would appreciate the help.

Réponse acceptée

Ayush Aniket
Ayush Aniket le 16 Juil 2024 à 5:25
Modifié(e) : Ayush Aniket le 16 Juil 2024 à 5:55
Hi Jorge,
To retrieve specific data from the XML file, you can use regexp on the string output of webread function. Refer the following MATLAB answer for an example: https://www.mathworks.com/matlabcentral/answers/883153-how-to-extract-the-data-from-webread-output
You may also refer the following link to understand more about the regexp function: https://www.mathworks.com/matlabcentral/answers/402062-help-using-regexp?#answer_321487
  1 commentaire
Jorge Luis
Jorge Luis le 16 Juil 2024 à 7:54
Thank you very much. Yes, that was the other option I was tempted to use but considering all the amount of data, I wanted a way in which all the information is converted in a structure matrix. I managed to do that but you need to save the data and use functions to write a xlm file and then structure the file.

Connectez-vous pour commenter.

Plus de réponses (1)

Anshuman
Anshuman le 16 Juil 2024 à 8:38
First you can parse the data to xmlData:
% Convert the data from char to XML DOM
xmlData = xmlreadstring(data);
You can use XPath queries to extract specific elements from the XML data:
% Create an XPath factory and compile the XPath expression
factory = javax.xml.xpath.XPathFactory.newInstance;
xpath = factory.newXPath;
% Extract magnitude
magnitudeExpression = xpath.compile('//magnitude/mag/value');
magnitudeNode = magnitudeExpression.evaluate(xmlData, javax.xml.xpath.XPathConstants.NODE);
magnitude = str2double(magnitudeNode.getTextContent);
This way you can extract the specific data like magnitude and any other information as needed.
  2 commentaires
Jorge Luis
Jorge Luis le 16 Juil 2024 à 21:54
Much appreciated. I am still looking for the way of saving the files as xlm witouth saving the webread data in order to structure all the information using the following function: https://www.mathworks.com/matlabcentral/fileexchange/28639-struct2xml.
Jorge Luis
Jorge Luis le 17 Juil 2024 à 17:56
The function xmlreadstring does not work.

Connectez-vous pour commenter.

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by