How to remove specific node in XML file?

33 views (last 30 days)
Mark Golberg
Mark Golberg on 11 Dec 2022
Answered: Shimon Elkin on 18 Dec 2022
Hello,
please see my "xml_sample.txt" file attached. I have 4 IP nodes (4 IP lines). I'd like to remove the last 2, so my new file would have only 2 IP nodes (the first 2).
If possible, I'd like to reduce the empty lines after removal as well. Please see "xml_sample_desiredOutput.txt" file attached.
I've tried to work with "removeChild", but could understand how to call it properly.
Can someone assist please? I'm pretty sure it's only ~3-4 lines of code.
  • I'm using 2021b version.
THANKS.

Answers (1)

Shimon Elkin
Shimon Elkin on 18 Dec 2022
infoFile = fullfile(matlabroot,'toolbox/matlab/general/info.xml');
infoLabel = 'Plot Tools';
infoCbk = '';
itemFound = false;
import matlab.io.xml.dom.*
xDoc = parseFile(Parser,infoFile);
%Load File, create DOM node Object xDoc
ProductInfo = xDoc.Children;
%Create element object. This isn’t strictly necessary, but should increase the readability of the removeChild statement
removeChild(ProductInfo,ProductInfo.Children(10));
removeChild(ProductInfo,ProductInfo.Children(10));
%Remove both the node for the Matlab Icon from the object
xmlwrite(xDoc,’dummy.xml’);
%Write the contents of the object onto a new xml file
----------------------------------------------------------
Best of luck

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!