How to remove specific node in XML file?
조회 수: 7 (최근 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!