how to find lines and update values in an XML file and then export the updated XML file?

4 views (last 30 days)
shron ikims
shron ikims on 11 Apr 2022
Answered: Benjamin Thompson on 11 Apr 2022
I have an XML file(attached here as text file , but I'll be using an XML file in MATLAB) and in this file I want to find the following lines:
<Ptr Name="DG.GSA[0]" Value="5"/>
<Ptr Name="DG.RIT[0]" Value="9"/>
<Ptr Name="P.M[4]" Value="X=62,Y=24"/>
and update it as (bold are the updated values)
<Ptr Name="DG.GSA[0]" Value="7"/>
<Ptr Name="DG.RIT[0]" Value="6"/>
<Ptr Name="P.M[4]" Value="X=45,Y=20"/>
after the updating is done I want to export the new XML file as Ptr.valout

Answers (1)

Benjamin Thompson
Benjamin Thompson on 11 Apr 2022
The easiest thing (other than just your own text editor), is xml2struct and struct2xml from Wouter Falkena from the File Exchange:
Your example is missing a square bracket on line 7 but that should not interfere with XML parsing. For more complicated XML problems use the MATLAB XML Parser MAXP. See the article "Import an XML File into a Document Object Model" in the help documentation.
>> S = xml2struct('Ptr.val.txt')
S =
struct with fields:
PtrVal: [1×1 struct]
>> S
S =
struct with fields:
PtrVal: [1×1 struct]
>> S.PtrVal
ans =
struct with fields:
Ptr: [1×1 struct]
Attributes: [1×1 struct]
>> S.PtrVal.Ptr
ans =
struct with fields:
Ptr: {1×19 cell}
>> S.PtrVal.Ptr{1}
Brace indexing is not supported for variables of this type.
>> S.PtrVal.Ptr.Ptr{1}
ans =
struct with fields:
Text: [1×0 char]
Attributes: [1×1 struct]
>> S.PtrVal.Ptr.Ptr{1}.Attributes
ans =
struct with fields:
Name: 'DG.RO[0]'
Value: 'Default'

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!