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

3 次查看(过去 30 天)
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

回答(1 个)

Benjamin Thompson
Benjamin Thompson 2022-4-11
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'

类别

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