Extracting values from .xml files

46 ビュー (過去 30 日間)
Auwal
Auwal 2025 年 1 月 27 日 5:51
コメント済み: Auwal 2025 年 1 月 27 日 22:23
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 日 6: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 日 22:23
Thanks KSSV and Anton. The code by Prabhat works perfectly.

サインインしてコメントする。

採用された回答

prabhat kumar sharma
prabhat kumar sharma 2025 年 1 月 27 日 10:39
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 日 22:16
編集済み: Auwal 2025 年 1 月 27 日 22:21
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 Center および File ExchangeStructured 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