1
我有以下XML結構:MATLAB - XML查找同級元素
<LMS>
<StressGradientCorrection>
<Gradients>
<Gradient>
<Curve>
<Point>
<NormalizedGradient Value="0.01" />
<ReductionFactor Value="1" />
</Point>
<Point>
<NormalizedGradient Value="0.1" />
<ReductionFactor Value="1" />
</Point>
<Point>
<NormalizedGradient Value="1" />
<ReductionFactor Value="1" />
</Point>
<Point>
<NormalizedGradient Value="10" />
<ReductionFactor Value="1" />
</Point>
</Curve>
</Gradient>
</Gradients>
</StressGradientCorrection>
</LMS>
我需要改變每個<ReductionFactor>
的價值的基礎上,<NormalizedGadient>
在同一<Point>
值。到目前爲止,我只能根據它的屬性區分元素,如名稱或值,但在這裏我不能。如何根據兄弟元素的值區分元素?
這裏是代碼的開始部分:
clear all
close all
clc
% Import the XPath classes
import javax.xml.xpath.*
% Construct the DOM.
doc = xmlread('SGC_EXAMPLE.xml');
% Creating an xPath
factory = javax.xml.xpath.XPathFactory.newInstance();
xpath = factory.newXPath();
Path = xpath.compile('/LMS/StressGradientCorrection/Gradients/Gradient/Curve/Point/NormalizedGradient[@Value="0.01"]');
List = Path.evaluate(doc, XPathConstants.NODESET);
Sibling = List.item(0);
% YOUR PROPOSAL HERE
MyNewlyFoundVariable.setAttribute('Value','5')
xmlwrite('Final.xml',doc);
到目前爲止,我只知道如何找到同級基於它的價值。
謝謝!奇蹟般有效。你只是在我的初始文章中發現了其中一個錯誤:「=」而不是「==」 – Trenera
是的,已更正:) – flafoux