How to change values in XML file?

15 ビュー (過去 30 日間)
abhay  kaul
abhay kaul 2012 年 9 月 2 日
コメント済み: Robert Ungi 2022 年 1 月 7 日
I apologize in advance as I have never worked with XML and this is my first try so any assistance will be really appreciated. I am trying to change a value in a XML file using Matlab with the following structure:
<setting name="test1">
<value>0</value>
</setting>
<setting name="test2">
<value>1</value>
</setting>
<setting name="test3">
<value>1</value>
<Air elev="83.0" temp="59.0" pre="29.9247">
</Air>
</setting>
The value I want to change is "0" to "1" in "test1" and also the value of "elev" in "test3". For the first part this is what I have till now:
a = docNode.getElementsByTagName('job');
b = a.item(0);
c = b.getElementsByTagName('setting');
d = c.item(0);
d.getFirstChild.setData('2')
xmlwrite('test1.xml',docNode);
The problem is that this is my output for line3 in the 'test1.xml' file.
<setting name="test1">20
I really have no clue how to deal with the xml when there are attributes and I have no idea how to change the value of attributes. I would really appreciate any help!
Thanks
Abhay

採用された回答

Jarrod Rivituso
Jarrod Rivituso 2012 年 9 月 4 日
Working with XML can definitely be tricky! Fortunately, these kinds of things are pretty well documented...
For your two cases, it'd be something like
d.nodeValue = '2';
and
someNode.setAttribute("elev","83.0");
  2 件のコメント
ANKAN BHATTACHARYYA
ANKAN BHATTACHARYYA 2016 年 6 月 27 日
編集済み: ANKAN BHATTACHARYYA 2016 年 6 月 27 日
How can i copy a full node like <x> 1 2 3 4 5 6 </x> to another node say, <coord> </coord> ?
Robert Ungi
Robert Ungi 2022 年 1 月 7 日
Maybe a more generic way: https://www.thewizz.art/2022/01/06/how-to-modify-an-xml-attribute-value-using-matlab/

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructured Data and XML Documents についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by