How to replace data in an existing XML file?

Hi,
I have an XML file. It looks like in the attached pic.
As you can see, in action Name "m1" there are 2 cols: "fish_1" & "fish_2".
I also have a list with new col names: "moose_42" , "moose_7" , "moose_19"
I'd like to replace the existing list (2 col names) with the new list (3 col names). I know how to "dive" into action "m1" and replace attributes values. But because it's 3 new vs. 2 existing, I'd need to add one NEW node. How this can be done?
Also, what if in my new list there is only 1 name, then how can I DELETE the second node?
THANKS !!!

6 comentarios

Geoff Hayes
Geoff Hayes el 17 de Jun. de 2022
@Mark Golberg - are you using xmlread to read the xml data into a DOM node?
Mark Golberg
Mark Golberg el 17 de Jun. de 2022
yes. but i'm open to suggestiones if there is a better alternative that could help with my task.
Geoff Hayes
Geoff Hayes el 17 de Jun. de 2022
Once you have the DOM object, then I think you can use APIs from here to add or remove nodes as you see fit.
Mark Golberg
Mark Golberg el 17 de Jun. de 2022
@Geoff Hayes Thank you.
I'm familiar with the "appendChild" method. I just don't know how to make it append my child node in the middle of the xml file.
for examle, I have "col Name=fish_1" & "col Name=fish_2"... how do I append node "fish_3" right beneath them...?
I assume it's 1-2 lines of code, but I struggle to find the correct way to do it.
Geoff Hayes
Geoff Hayes el 17 de Jun. de 2022
@Mark Golberg - yeah, that isn't all that clear on how to easily manipulate the xml object. You may want to consider, from the File Exchange, xml2struct and then struct2xml. You could perhaps manipulate the struct to add the new data (or remove the old) and then write back to the xml file. I think that would be easier.
Geoff Hayes
Geoff Hayes el 17 de Jun. de 2022
Or you may want to try the MAXP parser example found at https://www.mathworks.com/help/matlab/import_export/importing-xml-documents.html.

Iniciar sesión para comentar.

Respuestas (0)

Productos

Versión

R2021b

Etiquetas

Preguntada:

el 16 de Jun. de 2022

Comentada:

el 17 de Jun. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by