MATLAB - XML查找兄弟元素

时间:2015-05-12 12:24:34

标签: xml matlab xpath siblings

我有以下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>的值。到目前为止,我只能根据它的属性来区分元素,比如Name或Value,但在这里我不能。如何基于兄弟元素的值来区分元素?

以下是代码的起始部分:

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);

到目前为止,我只知道如何根据它的价值找到兄弟姐妹。

1 个答案:

答案 0 :(得分:2)

你可以找到以下这样的兄弟:

/LMS/StressGradientCorrection/Gradients/Gradient/Curve/Point/NormalizedGradient[@Value="0.01"]/following-sibling::ReductionFactor/@Value