XML EDIT attribute value
5 views (last 30 days)
Show older comments
My question might seem dumb but I can't seem to find the answer: How can I edit certain values on an xml file that already exists?
I want to edit all the attributes values with values coming from an excel sheet. Is that possible?
Thanks!
0 Comments
Answers (1)
Deepak
on 8 Jan 2025
To update an XML file using data from an Excel sheet in MATLAB, first, import the Excel data using the "readtable" or "xlsread" function, which allows us to work with the data in MATLAB. Next, parse the XML file using the "xmlread" function, which loads it as a DOM (Document Object Model) node, enabling structured access to its elements. Then, traverse the DOM tree to locate and modify the desired attributes using methods like "getElementsByTagName" and "getAttribute", updating them with corresponding values from the Excel data. Finally, save the modified XML structure back to a file using the "xmlwrite" function, ensuring that your changes are preserved in the output XML file.
Below is the sample MATLAB code to achieve the same:
% Read Excel file
excelData = readtable('data.xlsx');
% Read and parse the XML file
xmlFile = 'file.xml';
dom = xmlread(xmlFile);
% Modify the XML data
% Suppose we want to update attributes named 'attributeName' with values from the Excel sheet
elements = dom.getElementsByTagName('YourElementName');
for i = 0:elements.getLength-1
element = elements.item(i);
% Assuming excelData has a column named 'NewValue'
newValue = excelData.NewValue(i+1); % Adjust indexing as needed
element.setAttribute('attributeName', num2str(newValue));
end
% Write the modified XML back to a file
xmlwrite('modified_file.xml', dom); % Adjust the filename and path as needed
Please find attached the documentation of functions used for reference:
I hope this helps in resolving the issue.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!