Extracting values from .xml files

30 Ansichten (letzte 30 Tage)
Auwal
Auwal am 27 Jan. 2025
Kommentiert: Auwal am 27 Jan. 2025
Dear MATLAB users,
Kindly assist me on how to extract some data from .xml file here https://drive.google.com/file/d/1Sa1ycUqO4CshesnA_BAXy_BWGbeFb8IQ/view?usp=drive_link
Below is a part copied from the file. I would like specifically extract the values in BOLD
<CouchVrt>-176.51000000000011</CouchVrt>
<CouchLng>911.7700000000001</CouchLng>
<CouchLat>22.300999999999789</CouchLat>
I have been trying to read the file using xmlread without success.
Thanks in advance.
  3 Kommentare
Anton Kogios
Anton Kogios am 27 Jan. 2025
Hi Auwal, I don't seem to be able to access your xml file on Google Drive. Maybe change the file extension to .txt and upload it to MATLAB Answers, otherwise change the permissions on the link. I'd love to try help out.
Auwal
Auwal am 27 Jan. 2025
Thanks KSSV and Anton. The code by Prabhat works perfectly.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

prabhat kumar sharma
prabhat kumar sharma am 27 Jan. 2025
Hi Auwal,
To extract specific data from an XML file in MATLAB, you can use the xmlread function to load the XML content, and then navigate through the XML structure to find the desired elements. Here's a step-by-step guide on how to extract the values from the <CouchVrt>, <CouchLng>, and <CouchLat> elements:Step-by-Step Guide
  1. Read the XML File: Use xmlread to load the XML file into MATLAB.
  2. Navigate the XML Structure: Use the DOM (Document Object Model) functions to traverse the XML nodes and extract the required data.
  3. Extract Values: Identify the nodes corresponding to <CouchVrt>, <CouchLng>, and <CouchLat>, and extract their text content.
You can follow the below code piece for referece.
% Load the XML file
xmlFileName = 'abc.xml'; % Replace with the correct path to your XML file
xmlDoc = xmlread(xmlFileName);
% Get the document element
docElement = xmlDoc.getDocumentElement();
% Extract values from specific tags
couchVrt = getTextContent(docElement, 'CouchVrt');
couchLng = getTextContent(docElement, 'CouchLng');
couchLat = getTextContent(docElement, 'CouchLat');
% Display the extracted values
fprintf('CouchVrt: %s\n', couchVrt);
fprintf('CouchLng: %s\n', couchLng);
fprintf('CouchLat: %s\n', couchLat);
% Function to get text content from a specific tag
function value = getTextContent(docElement, tagName)
% Get elements by tag name
nodeList = docElement.getElementsByTagName(tagName);
% Assume there's only one element of each type
if nodeList.getLength > 0
% Get the first element and its text content
node = nodeList.item(0);
value = char(node.getTextContent());
else
error('Tag %s not found in the XML document.', tagName);
end
end
I hope it helps!
  1 Kommentar
Auwal
Auwal am 27 Jan. 2025
Bearbeitet: Auwal am 27 Jan. 2025
Thanks a million Prabhat for your help. This perfectly worked for me. Please if you are not bothered, may I ask if i can extract this information from multiple similar files in a folder and put them into a matrix of rows (number of files) by 3 columns (CouchVrt, CouchLng, CouchLat).
Many thanks

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by