Reputation: 1505
I have the following XML structure (simplified version):
<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="LMSDacSolutionReport.xsl" ?>
<LMS>
<DurabilityTaskDefinition>
<Material IdRef="Material_AlSi9Cu3" />
<Tasks>
<Task>
<Material IdRef="AlSi9Cu3">
<Parameter Name="Temperature" Value="293.15"/>
</Material>
</Task>
</Tasks>
</DurabilityTaskDefinition>
<Material>
<Node>
<Parameter Name="Temperature" Value="293.15"/>
<Parameter Name="SigMeanHat" Value="0"/>
<Parameter Name="R_Ratio" Value="-1"/>
</Node>
</Material>
</LMS>
I need to change the value of the Parameter Temperature to 300 (Material - Node - Parameter('Temperature')(Line 16).
The problem is that I have the tag <Material>
2 times before that, and this position will vary each time I run the code. I want to tell MATLAB to change the value of the Parameter with attribute: Name 'Temperature', only if the Material tag is on the second level after <LMS>
, or if the material tag is after the end of the tag: 'DurabilityTaskDefinition' .
So far I am sure just how to open and read the file:
xDoc = xmlread('C:\TEST\Durability Strain Life Analysis Solution.12.xml');
allListItems=xDoc.getElementsByTagName('Material');
Material=allListItems.item(2);
...
xmlwrite('test2.xml',xDoc);
Upvotes: 1
Views: 413
Reputation: 1505
Based on the comments, here is my working code:
% Import the XPath classes
import javax.xml.xpath.*
% Construct the DOM.
doc = xmlread('C:\TEST\Durability Strain Life Analysis Solution.12.xml');
factory = javax.xml.xpath.XPathFactory.newInstance();
xpath = factory.newXPath();
expr = xpath.compile('/LMS/Material/Node/Parameter[@Name="Temperature"]');
result = expr.evaluate(doc, XPathConstants.NODESET);
result = result.item(0);
result.setAttribute('Value','363696369')
xmlwrite('Final.xml',doc);
Would anyone suggest an improvement?
Upvotes: 2