How to remove specific node in XML file?
15 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
Shimon Elkin
2022-12-18
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
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!