How to remove specific node in XML file?

조회 수: 7 (최근 30일)
Mark Golberg
Mark Golberg 2022년 12월 11일
답변: Shimon Elkin 2022년 12월 18일
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.

답변 (1개)

Shimon Elkin
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

카테고리

Help CenterFile Exchange에서 Structured Data and XML Documents에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by