Structure data obtained from webread

2 次查看(过去 30 天)
Hi, I would like to have to automatize a procedure to extract data from different URLs, One example of one URL link is the following:
% Generating the URL link
url='https://earthquake.usgs.gov/fdsnws/event/1/query?format=quakeml&eventid=us6000n8tq';
% Reading the data
data=webread(url);
The variable “data” contains huge amount of information that I would like to structure in order to access and retrieve data easily without the need of creating a xml file.
I would appreciate the help.

采纳的回答

Vandit
Vandit 2024-7-17
Hi Jorge,
To automate the extraction and structuring of data from URLs like the one provided, the "webread" function in MATLAB can be used to read the data. Subsequently, the "xml2struct" function can convert the XML data into a MATLAB structure for easy access, eliminating the need to create an XML file. Below is the code snippet for reference that reads the data, parses the XML content, and converts it to a MATLAB structure for easy access:
% Generating the URL link
url = 'https://earthquake.usgs.gov/fdsnws/event/1/query?format=quakeml&eventid=us6000n8tq';
% Reading the data
data = webread(url);
% Parsing the XML content
xmlData = java.io.StringReader(data);
xmlInputFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
xmlBuilder = xmlInputFactory.newDocumentBuilder();
xmlDoc = xmlBuilder.parse(org.xml.sax.InputSource(xmlData));
% Converting XML to a MATLAB structure for easy access
xmlStruct = xml2struct(xmlDoc);
% Example access to specific data
eventDescription = xmlStruct.q_colon_quakeml.eventParameters.event.description{1, 1}.text.Text;
disp('Event Description:');
disp(eventDescription);
In the above code snippet, the "xml2struct" function is used to convert the XML data stored in the 'data' variable into a structured MATLAB variable 'xmlStruct'. Specific information from the 'xmlStruct' variable can be accessed and retrieved using dot notation or indexing, depending on the structure of the XML data.
However, please note that the specific structure of the XML data may vary depending on the URL and the data source. It may be necessary to inspect the structure of the 'xmlStruct' variable to determine the appropriate syntax for accessing the desired information, such as the event description in the above code snippet.
Additionally, the "xml2struct" function is not a built-in function in MATLAB. If this function is not available, it can be downloaded from MATLAB File Exchange using the following link:
Hope this helps.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structured Data and XML Documents 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by