Extracting values from .xml files

조회 수: 22 (최근 30일)
Auwal
Auwal 2025년 1월 27일
댓글: Auwal 2025년 1월 27일
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
Anton Kogios
Anton Kogios 2025년 1월 27일
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 2025년 1월 27일
Thanks KSSV and Anton. The code by Prabhat works perfectly.

댓글을 달려면 로그인하십시오.

채택된 답변

prabhat kumar sharma
prabhat kumar sharma 2025년 1월 27일
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
Auwal
Auwal 2025년 1월 27일
편집: Auwal 2025년 1월 27일
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

댓글을 달려면 로그인하십시오.

추가 답변 (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